Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / Java

XML: Generic File Writing and Loading Library

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
16 May 2011CPOL8 min read 22.8K   350   14  
Generic XML Tool to save and load XML files in a defined XML Format - useful for application settings or data
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.2-7 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2010.07.15 at 01:24:48 PM MESZ 
//


package jhtools.xml.generic.modelbinding;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import jhtools.xml.generic.IGeneric;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element ref="{}tag" maxOccurs="unbounded" minOccurs="0"/>
 *       &lt;/sequence>     
 *     	 &lt;attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
 *       &lt;attribute name="key" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
 *       &lt;attribute name="value" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "tag"
})
@XmlRootElement(name = "data")
public class Data implements IGeneric, Cloneable {

	protected List<Tag> tag;
    @XmlAttribute(name = "type")
    protected String type;
    @XmlAttribute(name = "key", required = true)
    protected String key;
    @XmlAttribute(name = "value", required = true)
    protected String value;

    /**
     * Gets the value of the tag property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the tag property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getTag().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link Tag }
     * 
     * 
     */
    public List<Tag> getTag() {
        if (tag == null) {
            tag = new ArrayList<Tag>();
        }
        return this.tag;
    }

    
    /**
     * Gets the value of the type property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getType() {
        return type;
    }

    /**
     * Sets the value of the type property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setType(String value) {
        this.type = value;
    }
    
    /**
     * Gets the value of the key property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getKey() {
        return key;
    }

    /**
     * Sets the value of the key property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setKey(String value) {
        this.key = value;
    }

    /**
     * Gets the value of the value property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getValue() {
        return value;
    }

    /**
     * Sets the value of the value property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setValue(String value) {
        this.value = value;
    }
    
    @Override
    public IGeneric[] getChildren () {
    	if (this.tag!=null && !this.tag.isEmpty()) {
    		IGeneric[] result = new IGeneric[this.tag.size()];
    		this.tag.toArray(result);
    		return result;
    	}
    	return new IGeneric[0];
    }
    
    
    @Override
    public String getGenericName() {
    	return value;
    }
    
    @Override
    public void setGenericName(String name) {
    	if (name != null)
    		this.value=name;    	
    }
    
    @Override
    public void setGenericKey(String key) {
    	if (key != null)
    		this.key=key;    	
    }
    
    
    @Override
    public String getGenericKey() {
    	return key;
    }

    @Override
    public IGeneric[] getGenericData() {
    	return new IGeneric[0];
    }
    
    @Override
    public boolean isCategory() {
    	return false;
    }
    @Override
    public boolean isEntry() {    
    	return false;
    }
	@Override
	public boolean isItem() {	
		return false;
	}
	@Override
	public boolean isTag() {
		return false;
	}
	@Override
	public boolean isData() {
		return true;
	}
	
	@Override
	public List<IGeneric> matches(String regex) {
		// return value List
		List<IGeneric> result = new ArrayList<IGeneric>();
		
		// wenn Key oder Wert stimmen - aktuelles Objekt hinzuf�gen
		if (this.key.matches(regex) || this.value.matches(regex)) 
			result.add(this);
		
		IGeneric[] children  = this.getChildren();
		for (int i=0; i<children.length; i++) 
			result.addAll (children[i].matches(regex));
		
		
		return result;
	}
	
	@Override
	public Type getGenericType() {
		if (this.type != null) {
			if (this.type.equals("text")) {
				return IGeneric.Type.TEXT;
			}
			else if (this.type.equals("number")) {
				return IGeneric.Type.NUMBER;
			}
			else if (this.type.equals("image")) {
				return IGeneric.Type.IMAGE;
			}
			else return IGeneric.Type.NONE;
			
		}
		else return IGeneric.Type.NONE;
	}
	
	@Override
	public void setGenericType(Type type) {
		if (type != Type.NONE) {
			this.type = type.name().toLowerCase();
		}
	}
	
	@Override
	public IGeneric findGenericImage() {	
		return null;
	}
	
	@Override
	public IGeneric getImageByKey(String key) {
		return null;
	}
	
	@Override
	public boolean delete(IGeneric object) {
		if (this.equals(object)) return false;
		if (tag != null) {
			if (tag.contains(object)) {
				tag.remove(object);
				return true;
			}	
		}

		return false;
	}
	
	@Override
	public Object clone() throws CloneNotSupportedException {
		
		Data newData = new Data ();
		
		if (this.tag != null) {
			List<Tag> ld = new ArrayList<Tag> (this.tag.size()); //(this.data);
			for (int i=0; i<this.tag.size(); i++) {
				ld.add((Tag)this.tag.get(i).clone());
			}
			newData.tag = ld;
		}
		
		if (this.key != null)
			newData.key = new String(this.key);
				
		if (this.value != null)
			newData.value = new String(this.value);
		
		if (this.type != null)
			newData.type = new String (this.type);
		
		return newData;
	}
	
	@Override
	public void add(IGeneric item) {
		if (item != null) {			
			if (item instanceof Tag) {
				if (this.tag != null)	this.tag.add((Tag)item);
				else {
					tag = new ArrayList<Tag>();
					tag.add((Tag)item);
				}
					
			}
		}
	}
	
    @Override
    public String toString (boolean recursive) {
    	// properties
    	String result = "Data: key="+this.key+"/value="+this.value+"/type="+this.type+"\n";
    	if (!recursive) return result;
    	// children    	
    	IGeneric[] children = this.getChildren();
    	for (int i=0; i<children.length; i++) {
    		result += children[i].toString();
    	}
    	return result;
    }
    
    @Override
    public String toString() {
    	return this.toString(true);
    }
    
    @Override
    public List<IGeneric> findByTags(String[] tags) {    	
    	List<IGeneric> result = new ArrayList<IGeneric> ();
    	
    	if (this.tag != null) {
    		Iterator<Tag> i = this.tag.iterator();
    		while (i.hasNext()) {
    			if (i.next().isTaggedByString(tags)) result.add(this);
    		}
    	}
    	return result;
    }
    
    @Override
    public String[] getTags() {
    	if (this.tag != null) {
    		if (this.tag.isEmpty()) return new String[0];
    		String[] result = new String[this.tag.size()];
    		for(int i=0;i<this.tag.size();i++) {
    			result[i]=this.tag.get(i).value;
    		}
    		return result;
    	}
    	return new String[0];
    }
    
    @Override
    public List<IGeneric> findChildrenByKey(String key, boolean recursive) {
    	List<IGeneric> result = new ArrayList<IGeneric> ();
    	if (key != null) {    		
    		IGeneric[] children = this.getChildren();
    		for (int i=0; i<children.length; i++) {
    			if (children[i].getGenericKey().matches(key))
    				result.add(children[i]);
    		}
    	}
    	
    	return result;
    }
    
    @Override
    public List<IGeneric> getAllChildren() {
    	List<IGeneric> result = new ArrayList<IGeneric> ();
    	
    	IGeneric[] children = this.getChildren();
    	for (int i=0; i<children.length; i++) {
    		result.add(children[i]);
    	}
    	
    	return result;
    }
    
    @Override
	public List<IGeneric> deleteChildrenWithKey(String key, boolean recursive) {
		List<IGeneric> result = new ArrayList<IGeneric> ();
		if (key!=null) {
			IGeneric[] children = this.getChildren();
			for (int i=0; i<children.length; i++) {
				if (children[i].getGenericKey().matches(key)) {
					result.add(children[i]);
					this.delete(children[i]);
				}
			}	
		}
		return result;
	}
    
    @Override
    public List<IGeneric> deleteTags() {
    	List<IGeneric> tags = new ArrayList<IGeneric>();
    	tags.addAll(this.tag);
    	this.tag = new ArrayList<Tag>();
    	return tags;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Database Developer
Germany Germany
I am programming more or less since I was 15 years.
It started with BASIC and during school I did
a lot Pascal programs for MS DOS.
After that I came to visual Windows programming and coded a lot in Delphi and later in C# .NET.
I made also some excursions in C and C++.

I also like Perl, Python and other scripting languages.
After a long break not programming anything, the last approx. 5-6 years I am only working with Java and SQL.

New for me is Objective-C - I am starting to get involved into Mac Programming Smile | :)

Comments and Discussions