diff --git a/src/fr/devinsy/util/xml/XMLAttribute.java b/src/fr/devinsy/util/xml/XMLAttribute.java
new file mode 100644
index 0000000..8d39c1b
--- /dev/null
+++ b/src/fr/devinsy/util/xml/XMLAttribute.java
@@ -0,0 +1,175 @@
+/*
+ * Copyright (C) 2013-2014,2017 Christian Pierre MOMON
+ *
+ * This file is part of Devinsy-xml.
+ *
+ * Devinsy-xml is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Devinsy-xml is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Devinsy-xml. If not, see
+ */
+package fr.devinsy.util.xml;
+
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * The Class XMLAttribute.
+ *
+ * @author Christian Pierre MOMON (christian.momon@devinsy.fr)
+ */
+public class XMLAttribute
+{
+ private String label;
+ private String value;
+
+ /**
+ * Instantiates a new XML attribute.
+ */
+ public XMLAttribute()
+ {
+ this.label = null;
+ this.value = null;
+ }
+
+ /**
+ * Instantiates a new XML attribute.
+ *
+ * @param label
+ * the label
+ * @param value
+ * the value
+ */
+ public XMLAttribute(final String label, final String value)
+ {
+ this.label = label;
+ this.value = value;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#clone()
+ */
+ @Override
+ public XMLAttribute clone()
+ {
+ XMLAttribute result;
+
+ result = new XMLAttribute(this.label, this.value);
+
+ //
+ return result;
+ }
+
+ /**
+ * Gets the label.
+ *
+ * @return the label
+ */
+ public String getLabel()
+ {
+ return this.label;
+ }
+
+ /**
+ * Gets the value.
+ *
+ * @return the value
+ */
+ public String getValue()
+ {
+ return this.value;
+ }
+
+ /**
+ * Checks for equal value.
+ *
+ * @param other
+ * the other
+ * @return true, if successful
+ */
+ public boolean hasEqualValue(final Object other)
+ {
+ boolean result;
+
+ if (other == null)
+ {
+ result = false;
+ }
+ else if ((StringUtils.equals(this.label, ((XMLAttribute) other).label)) && (StringUtils.equals(this.value, ((XMLAttribute) other).value)))
+ {
+ result = true;
+ }
+ else
+ {
+ result = false;
+ }
+
+ //
+ return result;
+ }
+
+ /**
+ * Checks if is blank.
+ *
+ * @return true, if is blank
+ */
+ public boolean isBlank()
+ {
+ boolean result;
+
+ if ((StringUtils.isBlank(this.label)) || (StringUtils.isBlank(this.value)))
+ {
+ result = true;
+ }
+ else
+ {
+ result = false;
+ }
+
+ //
+ return result;
+ }
+
+ /**
+ * Sets the label.
+ *
+ * @param label
+ * the new label
+ */
+ public void setLabel(final String label)
+ {
+ this.label = label;
+ }
+
+ /**
+ * Sets the value.
+ *
+ * @param value
+ * the new value
+ */
+ public void setValue(final String value)
+ {
+ this.value = value;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString()
+ {
+ String result;
+
+ result = String.format("%s=\"%s\"", this.label, this.value);
+
+ //
+ return result;
+ }
+}
diff --git a/src/fr/devinsy/util/xml/XMLAttributes.java b/src/fr/devinsy/util/xml/XMLAttributes.java
index a3cf61f..4c36166 100644
--- a/src/fr/devinsy/util/xml/XMLAttributes.java
+++ b/src/fr/devinsy/util/xml/XMLAttributes.java
@@ -31,8 +31,7 @@ import javax.xml.stream.events.Attribute;
*
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/
-
-public class XMLAttributes extends HashMap implements Iterable
+public class XMLAttributes extends HashMap implements Iterable
{
private static final long serialVersionUID = 8456469741805779474L;
@@ -64,13 +63,41 @@ public class XMLAttributes extends HashMap implements Iterabl
public XMLAttributes(final Iterator source)
{
super();
+
if (source != null)
{
while (source.hasNext())
{
Attribute attribute = source.next();
- add(attribute);
+ add(new XMLAttribute(attribute.getName().getLocalPart(), attribute.getValue()));
+ }
+ }
+ }
+
+ /**
+ * Instantiates a new XML attributes from a string array. Strings are series
+ * of label and value. This constructor is a helper.
+ *
+ * @param source
+ * the source
+ */
+ public XMLAttributes(final String... attributes)
+ {
+ super();
+
+ if (attributes != null)
+ {
+ if (attributes.length % 2 == 0)
+ {
+ for (int count = 0; count < attributes.length; count += 2)
+ {
+ add(new XMLAttribute(attributes[count], attributes[count + 1]));
+ }
+ }
+ else
+ {
+ throw new IllegalArgumentException("Parameter count is odd.");
}
}
}
@@ -84,6 +111,7 @@ public class XMLAttributes extends HashMap implements Iterabl
public XMLAttributes(final XMLAttributes source)
{
super();
+
addAll(source);
}
@@ -93,11 +121,11 @@ public class XMLAttributes extends HashMap implements Iterabl
* @param attribute
* the attribute
*/
- public void add(final Attribute attribute)
+ public void add(final XMLAttribute attribute)
{
if (attribute != null)
{
- put(attribute.getName().getLocalPart(), attribute);
+ put(attribute.getLabel(), attribute);
}
}
@@ -109,7 +137,7 @@ public class XMLAttributes extends HashMap implements Iterabl
*/
public void addAll(final XMLAttributes source)
{
- for (Attribute attribute : source)
+ for (XMLAttribute attribute : source)
{
this.add(attribute);
}
@@ -122,9 +150,9 @@ public class XMLAttributes extends HashMap implements Iterabl
* the label
* @return the by label
*/
- public Attribute getByLabel(final String label)
+ public XMLAttribute getByLabel(final String label)
{
- Attribute result;
+ XMLAttribute result;
result = get(label);
@@ -136,9 +164,9 @@ public class XMLAttributes extends HashMap implements Iterabl
* @see java.lang.Iterable#iterator()
*/
@Override
- public Iterator iterator()
+ public Iterator iterator()
{
- Iterator result;
+ Iterator result;
result = this.values().iterator();
@@ -161,16 +189,38 @@ public class XMLAttributes extends HashMap implements Iterabl
return result;
}
+ /**
+ * To array.
+ *
+ * @return the XML attribute[]
+ */
+ public XMLAttribute[] toArray()
+ {
+ XMLAttribute[] result;
+
+ result = new XMLAttribute[size()];
+
+ int count = 0;
+ for (String key : this.keySet())
+ {
+ result[count] = get(key);
+ count += 1;
+ }
+
+ //
+ return result;
+ }
+
/**
* To list.
*
* @return the list
*/
- public List toList()
+ public List toList()
{
- List result;
+ List result;
- result = new ArrayList(values());
+ result = new ArrayList(values());
//
return result;
diff --git a/src/fr/devinsy/util/xml/XMLReader.java b/src/fr/devinsy/util/xml/XMLReader.java
index 58253d6..dd250b4 100644
--- a/src/fr/devinsy/util/xml/XMLReader.java
+++ b/src/fr/devinsy/util/xml/XMLReader.java
@@ -18,10 +18,10 @@
*/
package fr.devinsy.util.xml;
+import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
-import java.io.FileReader;
import java.io.InputStream;
import java.io.Reader;
@@ -62,18 +62,25 @@ public class XMLReader
/**
* Instantiates a new XML reader.
*
- * @param file
+ * @param source
* the file
* @throws FileNotFoundException
* the file not found exception
* @throws XMLStreamException
* the XML stream exception
*/
- public XMLReader(final File file) throws FileNotFoundException, XMLStreamException
+ public XMLReader(final File source) throws FileNotFoundException, XMLStreamException
{
- this.nextEvent = null;
- XMLInputFactory factory = XMLInputFactory.newInstance();
- this.in = factory.createXMLEventReader(new FileInputStream(file), "UTF-8");
+ if (source == null)
+ {
+ throw new IllegalArgumentException("Null parameter.");
+ }
+ else
+ {
+ this.nextEvent = null;
+ XMLInputFactory factory = XMLInputFactory.newInstance();
+ this.in = factory.createXMLEventReader(new FileInputStream(source), "UTF-8");
+ }
}
/**
@@ -86,9 +93,16 @@ public class XMLReader
*/
public XMLReader(final InputStream source) throws XMLStreamException
{
- this.nextEvent = null;
- XMLInputFactory factory = XMLInputFactory.newInstance();
- this.in = factory.createXMLEventReader(source);
+ if (source == null)
+ {
+ throw new IllegalArgumentException("Null parameter.");
+ }
+ else
+ {
+ this.nextEvent = null;
+ XMLInputFactory factory = XMLInputFactory.newInstance();
+ this.in = factory.createXMLEventReader(source);
+ }
}
/**
@@ -101,9 +115,34 @@ public class XMLReader
*/
public XMLReader(final Reader source) throws XMLStreamException
{
- this.nextEvent = null;
- XMLInputFactory factory = XMLInputFactory.newInstance();
- this.in = factory.createXMLEventReader(source);
+ if (source == null)
+ {
+ throw new IllegalArgumentException("Null parameter.");
+ }
+ else
+ {
+ this.nextEvent = null;
+ XMLInputFactory factory = XMLInputFactory.newInstance();
+ this.in = factory.createXMLEventReader(source);
+ }
+ }
+
+ /**
+ * @param source
+ * @throws XMLStreamException
+ */
+ public XMLReader(final String source) throws XMLStreamException
+ {
+ if (source == null)
+ {
+ throw new IllegalArgumentException("Null parameter.");
+ }
+ else
+ {
+ this.nextEvent = null;
+ XMLInputFactory factory = XMLInputFactory.newInstance();
+ this.in = factory.createXMLEventReader(new ByteArrayInputStream(source.getBytes()));
+ }
}
/**
diff --git a/src/fr/devinsy/util/xml/XMLTools.java b/src/fr/devinsy/util/xml/XMLTools.java
index a852915..a0519e4 100644
--- a/src/fr/devinsy/util/xml/XMLTools.java
+++ b/src/fr/devinsy/util/xml/XMLTools.java
@@ -27,6 +27,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipInputStream;
+import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
@@ -39,6 +40,8 @@ import org.apache.commons.lang3.StringUtils;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
+import fr.devinsy.util.strings.StringList;
+
/**
* The Class XMLTools.
*
@@ -70,6 +73,80 @@ public class XMLTools
return result;
}
+ /**
+ * Indent.
+ *
+ * @param source
+ * the source
+ * @return the string
+ * @throws XMLStreamException
+ * @throws XMLBadFormatException
+ * @throws IOException
+ */
+ public static String indent(final String source) throws XMLStreamException, XMLBadFormatException, IOException
+ {
+ String result;
+
+ XMLReader in = new XMLReader(source);
+ StringList buffer = new StringList();
+ XMLWriter out = new XMLWriter(buffer);
+
+ boolean ended = false;
+ int level = 0;
+ while (!ended)
+ {
+ XMLTag tag = in.readTag();
+
+ if (tag == null)
+ {
+ ended = true;
+ }
+ else
+ {
+ switch (tag.getType())
+ {
+ case HEADER:
+ out.writeXMLHeader(tag.attributes());
+ out.flush();
+ buffer.appendln();
+ break;
+ case START:
+ out.flush();
+ buffer.append(StringUtils.repeat('\t', level));
+ out.writeStartTag(tag.getLabel(), tag.attributes());
+ out.flush();
+ buffer.appendln();
+ level += 1;
+ break;
+ case CONTENT:
+ out.flush();
+ buffer.append(StringUtils.repeat('\t', level));
+ out.writeTag(tag.getLabel(), tag.getContent(), tag.attributes());
+ out.flush();
+ buffer.appendln();
+ break;
+ case END:
+ level -= 1;
+ out.flush();
+ buffer.append(StringUtils.repeat('\t', level));
+ out.writeEndTag(tag.getLabel());
+ out.flush();
+ buffer.appendln();
+ break;
+ case EMPTY:
+ break;
+ case FOOTER:
+ break;
+ }
+ }
+ }
+
+ result = buffer.toString();
+
+ //
+ return result;
+ }
+
/**
* Checks if is valid.
*
diff --git a/src/fr/devinsy/util/xml/XMLWriter.java b/src/fr/devinsy/util/xml/XMLWriter.java
index 192cb0c..a76c9c1 100644
--- a/src/fr/devinsy/util/xml/XMLWriter.java
+++ b/src/fr/devinsy/util/xml/XMLWriter.java
@@ -28,7 +28,8 @@ import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
-import org.apache.commons.lang3.ArrayUtils;
+import fr.devinsy.util.strings.StringList;
+import fr.devinsy.util.strings.StringListWriter;
/**
* The Class XMLWriter.
@@ -51,16 +52,23 @@ public class XMLWriter
/**
* Initialize a XML Writer to a file.
*
- * @param file
+ * @param target
* Where write the XML data.
* @throws UnsupportedEncodingException
* the unsupported encoding exception
* @throws FileNotFoundException
* the file not found exception
*/
- public XMLWriter(final File file) throws UnsupportedEncodingException, FileNotFoundException
+ public XMLWriter(final File target) throws UnsupportedEncodingException, FileNotFoundException
{
- this.out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
+ if (target == null)
+ {
+ throw new IllegalArgumentException("Null parameter.");
+ }
+ else
+ {
+ this.out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(target), "UTF-8"));
+ }
}
/**
@@ -73,7 +81,34 @@ public class XMLWriter
*/
public XMLWriter(final OutputStream target) throws UnsupportedEncodingException
{
- this.out = new PrintWriter(new OutputStreamWriter(target, "UTF-8"));
+ if (target == null)
+ {
+ throw new IllegalArgumentException("Null parameter.");
+ }
+ else
+ {
+ this.out = new PrintWriter(new OutputStreamWriter(target, "UTF-8"));
+ }
+ }
+
+ /**
+ * Instantiates a new XML writer.
+ *
+ * @param target
+ * the target
+ * @throws UnsupportedEncodingException
+ * the unsupported encoding exception
+ */
+ public XMLWriter(final StringList target) throws UnsupportedEncodingException
+ {
+ if (target == null)
+ {
+ throw new IllegalArgumentException("Null parameter.");
+ }
+ else
+ {
+ this.out = new PrintWriter(new StringListWriter(target));
+ }
}
/**
@@ -86,7 +121,15 @@ public class XMLWriter
*/
public XMLWriter(final Writer target) throws UnsupportedEncodingException
{
- this.out = new PrintWriter(target);
+ if (target == null)
+ {
+ throw new IllegalArgumentException("Null parameter.");
+ }
+ else
+ {
+
+ this.out = new PrintWriter(target);
+ }
}
/**
@@ -143,6 +186,19 @@ public class XMLWriter
* the attributes
*/
public void writeEmptyTag(final String label, final String... attributes)
+ {
+ writeEmptyTag(label, new XMLAttributes(attributes));
+ }
+
+ /**
+ * Write empty tag.
+ *
+ * @param label
+ * the label
+ * @param attributes
+ * the attributes
+ */
+ public void writeEmptyTag(final String label, final XMLAttributes attributes)
{
this.out.print("<");
this.out.print(label);
@@ -172,6 +228,19 @@ public class XMLWriter
* the attributes
*/
public void writeStartTag(final String label, final String... attributes)
+ {
+ writeStartTag(label, new XMLAttributes(attributes));
+ }
+
+ /**
+ * Write start tag.
+ *
+ * @param label
+ * the label
+ * @param attributes
+ * the attributes
+ */
+ public void writeStartTag(final String label, final XMLAttributes attributes)
{
this.out.print("<");
this.out.print(label);
@@ -179,6 +248,17 @@ public class XMLWriter
this.out.print(">");
}
+ /**
+ * Write start tag.
+ *
+ * @param tag
+ * the tag
+ */
+ public void writeStartTag(final XMLTag tag)
+ {
+ writeStartTag(tag.getLabel(), tag.attributes());
+ }
+
/**
* This method write a XML tag with attributes and boolean content data.
*
@@ -190,6 +270,21 @@ public class XMLWriter
* the attributes
*/
public void writeTag(final String label, final boolean content, final String... attributes)
+ {
+ writeTag(label, content, new XMLAttributes(attributes));
+ }
+
+ /**
+ * Write tag.
+ *
+ * @param label
+ * the label
+ * @param content
+ * the content
+ * @param attributes
+ * the attributes
+ */
+ public void writeTag(final String label, final boolean content, final XMLAttributes attributes)
{
writeStartTag(label, attributes);
writeTagContent(String.valueOf(content));
@@ -207,6 +302,21 @@ public class XMLWriter
* the attributes
*/
public void writeTag(final String label, final long content, final String... attributes)
+ {
+ writeTag(label, content, new XMLAttributes(attributes));
+ }
+
+ /**
+ * Write tag.
+ *
+ * @param label
+ * the label
+ * @param content
+ * the content
+ * @param attributes
+ * the attributes
+ */
+ public void writeTag(final String label, final long content, final XMLAttributes attributes)
{
writeStartTag(label, attributes);
writeTagContent(String.valueOf(content));
@@ -225,6 +335,21 @@ public class XMLWriter
* the attributes
*/
public void writeTag(final String label, final String content, final String... attributes)
+ {
+ writeTag(label, content, new XMLAttributes(attributes));
+ }
+
+ /**
+ * Write tag.
+ *
+ * @param label
+ * the label
+ * @param content
+ * the content
+ * @param attributes
+ * the attributes
+ */
+ public void writeTag(final String label, final String content, final XMLAttributes attributes)
{
if (content == null)
{
@@ -244,16 +369,16 @@ public class XMLWriter
* @param attributes
* the attributes
*/
- private void writeTagAttributes(final String... attributes)
+ private void writeTagAttributes(final XMLAttributes attributes)
{
- if ((attributes != null) && (attributes.length > 0))
+ if ((attributes != null) && (!attributes.isEmpty()))
{
- for (int count = 0; count < attributes.length; count += 2)
+ for (XMLAttribute attribute : attributes)
{
this.out.print(" ");
- this.out.print(attributes[count]);
+ this.out.print(attribute.getLabel());
this.out.print("=\"");
- this.out.print(attributes[count + 1]);
+ this.out.print(attribute.getValue());
this.out.print("\"");
}
}
@@ -302,24 +427,34 @@ public class XMLWriter
*/
public void writeXMLHeader(final String... attributes)
{
+ writeXMLHeader(new XMLAttributes(attributes));
+ }
+ /**
+ * This method writes a XML header with attributes.
+ *
+ * @param attributes
+ * the attributes
+ */
+ public void writeXMLHeader(final XMLAttributes attributes)
+ {
//
this.out.print("