moved parts of XML parsing into shared library
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / xml / xsd / XSD.java
1 package eu.svjatoslav.inspector.xml.xsd;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import javax.xml.parsers.ParserConfigurationException;
9
10 import org.xml.sax.SAXException;
11
12 import eu.svjatoslav.commons.data.xml.XmlElement;
13 import eu.svjatoslav.commons.data.xml.XmlHelper;
14
15 public class XSD {
16
17         private static final String XMLNS_PREFIX = "xmlns:";
18         Map<String, String> namespaces = new HashMap<String, String>();
19
20         private void detectNamespaces(final XmlElement xsdSchema) {
21                 for (final String attributeName : xsdSchema.getAttributeNames())
22
23                         if (attributeName.startsWith(XMLNS_PREFIX)) {
24                                 final String nameSpaceName = attributeName
25                                                 .substring(XMLNS_PREFIX.length());
26                                 namespaces.put(nameSpaceName,
27                                                 xsdSchema.getAttributeValue(attributeName));
28                         }
29         }
30
31         public void parse(final InputStream inputStream) throws SAXException,
32                         IOException, ParserConfigurationException {
33
34                 final XmlElement xsdSchema = XmlHelper.parseXml(inputStream);
35
36                 detectNamespaces(xsdSchema);
37
38                 System.out.println(xsdSchema.toString());
39         }
40
41         @Override
42         public String toString() {
43                 final StringBuffer result = new StringBuffer();
44
45                 result.append("namespaces:\n");
46
47                 for (final Map.Entry<String, String> entry : namespaces.entrySet())
48                         result.append("    " + entry.getKey() + " = " + entry.getValue()
49                                         + "\n");
50
51                 return result.toString();
52         }
53
54 }