From 250b125774c089983db95f62e82995c39b04017d Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Wed, 3 Jul 2013 21:58:48 +0300 Subject: [PATCH] implementing XSD parser --- .../eu/svjatoslav/inspector/xml/xsd/Main.java | 10 ++- .../eu/svjatoslav/inspector/xml/xsd/XSD.java | 84 +++++++++++++++++++ .../inspector/xml/xsd/XmlElement.java | 53 ++++++++++++ 3 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 src/main/java/eu/svjatoslav/inspector/xml/xsd/XSD.java create mode 100644 src/main/java/eu/svjatoslav/inspector/xml/xsd/XmlElement.java diff --git a/src/main/java/eu/svjatoslav/inspector/xml/xsd/Main.java b/src/main/java/eu/svjatoslav/inspector/xml/xsd/Main.java index 9ecb76e..d0093f4 100644 --- a/src/main/java/eu/svjatoslav/inspector/xml/xsd/Main.java +++ b/src/main/java/eu/svjatoslav/inspector/xml/xsd/Main.java @@ -1,8 +1,16 @@ package eu.svjatoslav.inspector.xml.xsd; +import java.io.FileNotFoundException; + public class Main { - public static void main(final String[] args) { + public static void main(final String[] args) throws FileNotFoundException { + + final XSD xsd = new XSD(); + + xsd.parse("/home/n0/Desktop/MeterSchema.xsd"); + + System.out.println(xsd); } diff --git a/src/main/java/eu/svjatoslav/inspector/xml/xsd/XSD.java b/src/main/java/eu/svjatoslav/inspector/xml/xsd/XSD.java new file mode 100644 index 0000000..3d5ff45 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/xml/xsd/XSD.java @@ -0,0 +1,84 @@ +package eu.svjatoslav.inspector.xml.xsd; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +public class XSD { + + private static final String XMLNS_PREFIX = "xmlns:"; + Map namespaces = new HashMap(); + + private void detectNamespaces(final XmlElement xsdSchema) { + for (final String attributeName : xsdSchema.getAttributeNames()) + + if (attributeName.startsWith(XMLNS_PREFIX)) { + final String nameSpaceName = attributeName + .substring(XMLNS_PREFIX.length()); + namespaces.put(nameSpaceName, + xsdSchema.getAttributeValue(attributeName)); + } + } + + public void parse(final InputStream inputStream) { + + final DocumentBuilderFactory builderFactory = DocumentBuilderFactory + .newInstance(); + + DocumentBuilder builder = null; + try { + builder = builderFactory.newDocumentBuilder(); + } catch (final ParserConfigurationException e) { + e.printStackTrace(); + } + + Document document; + try { + document = builder.parse(inputStream); + } catch (final SAXException e) { + e.printStackTrace(); + return; + } catch (final IOException e) { + e.printStackTrace(); + return; + } + + final XmlElement xsdSchema = new XmlElement( + document.getDocumentElement()); + + detectNamespaces(xsdSchema); + + System.out.println(xsdSchema.toString()); + } + + public void parse(final String filePath) throws FileNotFoundException { + + final FileInputStream inputStream = new FileInputStream(filePath); + + parse(inputStream); + } + + @Override + public String toString() { + final StringBuffer result = new StringBuffer(); + + result.append("namespaces:\n"); + + for (final Map.Entry entry : namespaces.entrySet()) + result.append(" " + entry.getKey() + " = " + entry.getValue() + + "\n"); + + return result.toString(); + } + +} diff --git a/src/main/java/eu/svjatoslav/inspector/xml/xsd/XmlElement.java b/src/main/java/eu/svjatoslav/inspector/xml/xsd/XmlElement.java new file mode 100644 index 0000000..6863fd4 --- /dev/null +++ b/src/main/java/eu/svjatoslav/inspector/xml/xsd/XmlElement.java @@ -0,0 +1,53 @@ +package eu.svjatoslav.inspector.xml.xsd; + +import java.util.ArrayList; +import java.util.List; + +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +public class XmlElement { + + Element element; + + public XmlElement(final Element element) { + this.element = element; + } + + public List getAttributeNames() { + final ArrayList result = new ArrayList(); + + final NamedNodeMap attributes = element.getAttributes(); + + for (int i = 0; i < attributes.getLength(); i++) { + final Node node = attributes.item(i); + + result.add(node.getNodeName()); + } + + return result; + } + + public String getAttributeValue(final String attributeName) { + return element.getAttribute(attributeName); + } + + @Override + public String toString() { + final StringBuffer result = new StringBuffer(); + + result.append("node name: " + element.getNodeName() + "\n"); + + final NamedNodeMap attributes = element.getAttributes(); + + for (int i = 0; i < attributes.getLength(); i++) { + final Node node = attributes.item(i); + + result.append(" " + node.getNodeName() + "\n"); + } + + return result.toString(); + } + +} -- 2.20.1