From a65197d100c1a250fdae78d93b37f0d24018d3e3 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sun, 7 May 2017 18:40:13 +0200 Subject: [PATCH] Improved demo. Fixed some string building. --- src/fr/devinsy/util/xml/XMLTools.java | 10 +- src/fr/devinsy/util/xml/demo/XMLDemo.java | 116 ++++++++++++++++++++++ src/fr/devinsy/util/xml/demo/XmlDemo.java | 56 ----------- 3 files changed, 121 insertions(+), 61 deletions(-) create mode 100644 src/fr/devinsy/util/xml/demo/XMLDemo.java delete mode 100644 src/fr/devinsy/util/xml/demo/XmlDemo.java diff --git a/src/fr/devinsy/util/xml/XMLTools.java b/src/fr/devinsy/util/xml/XMLTools.java index 003975d..a852915 100644 --- a/src/fr/devinsy/util/xml/XMLTools.java +++ b/src/fr/devinsy/util/xml/XMLTools.java @@ -327,7 +327,7 @@ public class XMLTools result = "CDATA"; break; case XMLEvent.CHARACTERS: - result = "CHARACTERS [" + source.asCharacters().getData() + "]"; + result = String.format("CHARACTERS [%s]", source.asCharacters().getData()); break; case XMLEvent.COMMENT: result = "COMMENT"; @@ -339,7 +339,7 @@ public class XMLTools result = "END_DOCUMENT"; break; case XMLEvent.END_ELEMENT: - result = "END_ELEMENT " + source.asEndElement().getName(); + result = String.format("END_ELEMENT [%s]", source.asEndElement().getName()); break; case XMLEvent.ENTITY_DECLARATION: result = "ENTITY_DECLARATION"; @@ -363,8 +363,8 @@ public class XMLTools result = "START_DOCUMENT"; break; case XMLEvent.START_ELEMENT: - result = "START_ELEMENT [name=" + source.asStartElement().getName() + "][namespaceURI=" + source.asStartElement().getName().getNamespaceURI() + "][prefix=" - + source.asStartElement().getName().getPrefix() + "][localPart=" + source.asStartElement().getName().getLocalPart() + "]"; + result = String.format("START_ELEMENT [name=%s][namespaceURI=%s][prefix=%s][localPart=%s]", source.asStartElement().getName(), source.asStartElement().getName().getNamespaceURI(), + source.asStartElement().getName().getPrefix(), source.asStartElement().getName().getLocalPart()); break; default: result = null; @@ -391,7 +391,7 @@ public class XMLTools } else { - result = "[label=" + source.getLabel() + "][type=" + source.getType().toString() + "][content=" + source.getContent() + "]"; + result = String.format("[label=%s][type=%s][content=%s]", source.getLabel(), source.getType().toString(), source.getContent()); } // diff --git a/src/fr/devinsy/util/xml/demo/XMLDemo.java b/src/fr/devinsy/util/xml/demo/XMLDemo.java new file mode 100644 index 0000000..cefa87c --- /dev/null +++ b/src/fr/devinsy/util/xml/demo/XMLDemo.java @@ -0,0 +1,116 @@ +/* + * Copyright (C) 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.demo; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import javax.xml.stream.XMLStreamException; + +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; + +import fr.devinsy.util.xml.XMLBadFormatException; +import fr.devinsy.util.xml.XMLReader; +import fr.devinsy.util.xml.XMLTag; +import fr.devinsy.util.xml.XMLWriter; + +/** + * The Class XmlDemo. + */ +public class XMLDemo +{ + + /** + * The main method of XMLDemo. + * + * @param args + * the arguments + * @throws IOException + * Signals that an I/O exception has occurred. + * @throws XMLStreamException + * the XML stream exception + * @throws XMLBadFormatException + * the XML bad format exception + */ + public static void main(final String[] args) throws IOException, XMLStreamException, XMLBadFormatException + { + // + BasicConfigurator.configure(); + Logger.getRootLogger().setLevel(Level.ERROR); + + // + String xml; + { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + + XMLWriter out = new XMLWriter(stream); + + out.writeXMLHeader(); + + // + out.writeStartTag("account", "id", String.valueOf(77)); + { + out.writeTag("email", "christian.momon@devinsy.fr"); + out.writeTag("first_names", "Christian Pierre"); + out.writeTag("last_name", "MOMON"); + out.writeTag("comment", "Foo character \" \'"); + } + out.writeEndTag("account"); + + // + out.close(); + xml = stream.toString(); + } + + System.out.println("XML generated:"); + System.out.println(xml); + System.out.println(); + + { + ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes()); + XMLReader in = new XMLReader(stream); + + in.readXMLHeader(); + + // + XMLTag tag = in.readStartTag("account"); + long id = Long.parseLong(tag.attributes().getByLabel("id").getValue()); + { + String email = in.readContentTag("email").getContent(); + String firstName = in.readNullableContentTag("first_names").getContent(); + String lastName = in.readNullableContentTag("last_name").getContent(); + String comment = in.readNullableContentTag("comment").getContent(); + + System.out.println("Data read:"); + System.out.println("Id=" + id); + System.out.println("email=" + email); + System.out.println("firstName=" + firstName); + System.out.println("lastName=" + lastName); + System.out.println("comment=" + comment); + } + in.readEndTag("account"); + + // + in.close(); + } + } +} diff --git a/src/fr/devinsy/util/xml/demo/XmlDemo.java b/src/fr/devinsy/util/xml/demo/XmlDemo.java deleted file mode 100644 index 729d2ef..0000000 --- a/src/fr/devinsy/util/xml/demo/XmlDemo.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 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.demo; - -import fr.devinsy.util.strings.StringList; - -/** - * The Class XmlDemo. - */ -public class XmlDemo -{ - /** - * The main method. - * - * @param args - * the arguments - */ - public static void main(final String[] args) - { - // #1 - { - StringList strings = new StringList(); - - strings.appendln("========== DEMO #1;"); - strings.appendln(); - - System.out.println(strings.toString()); - } - - // #2 - { - StringList strings = new StringList(); - - strings.appendln("========== DEMO #2;"); - strings.appendln(); - - System.out.println(strings.toString()); - } - } -}