001package bradleyross.opensource.xerces;
002import java.io.File;
003import org.xml.sax.helpers.DefaultHandler;
004import org.xml.sax.Attributes;
005import org.xml.sax.SAXException;
006import javax.xml.parsers.ParserConfigurationException;
007import javax.xml.parsers.SAXParser;
008import javax.xml.parsers.SAXParserFactory;
009import javax.xml.stream.XMLStreamReader;
010import java.io.FileInputStream;
011import java.io.IOException;
012/**
013 * This is a demonstration of XML parsing.
014 * 
015 * <p>It reads a file as a resource and parses it as an XML file.</p>
016 * <p>http://stackoverflow.com/questions/9429037/getting-encoding-type-of-a-xml-in-java</p>
017 * <p>https://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/input/XmlStreamReader.html</p>
018 * @author Bradley Ross
019 *
020 */
021public class TestXmlParser {
022        protected class MyHandler extends DefaultHandler
023        {
024                @Override
025                public void startDocument() throws SAXException {
026                        System.out.println("Starting document");
027                }
028                @Override
029                public void endDocument() throws SAXException {
030                        System.out.println("End of document");
031                }
032                @Override
033                public void startPrefixMapping(String prefix, String uri) throws SAXException {
034                        System.out.println("startPrefixMapping " + prefix + " " + uri);
035                }
036                @Override
037                public void endPrefixMapping(String prefix) throws SAXException {
038                        System.out.println("endPrefixMapping " + prefix);
039                }
040                @Override
041                public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
042                        System.out.println();
043                        System.out.println("Starting element " + uri + " " + localName +
044                                        " " + qName);
045                        Attributes attributes = atts;
046                        if (attributes.getLength() > 0) {
047                                for (int i = 0; i < attributes.getLength(); i++) {
048                                        System.out.println("Attribute " + Integer.toString(i) + ":" + attributes.getQName(i) +
049                                                        " " + attributes.getType(i) + " " + attributes.getValue(i));
050                                }
051                        }
052                }
053                @Override
054                public void endElement(String uri, String localName, String qName) throws SAXException {
055                        System.out.println();
056                        System.out.println("Ending element " + uri + " " + localName +
057                                        " " + qName);           
058                }
059                @Override
060                public void characters(char[] ch, int start, int length) throws SAXException {
061                        System.out.println();
062                        System.out.println("Processing characters " + Integer.toString(start) +
063                                        " " + Integer.toString(length));
064                        if (length == 1) {
065                                System.out.print("Character is " + Integer.toString((int) ch[start]));
066                        } else {
067                                for (int i = start; i <= start + length - 1; i++) {
068                                        System.out.print(ch[i]);
069                                }
070                        }
071                        System.out.println();
072                }
073                @Override
074                public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
075                        System.out.println("Ignorable whitespace " + Integer.toString(start) +
076                                        " " + Integer.toString(length));
077                        if (length == 1) {
078                                System.out.println("Character is " + Integer.toString((int) ch[start]));
079                        } else {
080                                for (int i = start; i <= start + length - 1; i++) {
081                                        System.out.print(ch[i]);
082                                }
083                        }
084                        System.out.println();
085                }
086                @Override
087                public void processingInstruction(String target, String data) throws SAXException {
088                        System.out.println("ProcessingInstruction " + target + " " + data);
089
090                }
091
092                public void skippedEntity(String name) throws SAXException {
093                        System.out.println("Skipped entity " + name);
094
095                }
096
097        }
098        /**
099         * Carries out the actual parsing.
100         * 
101         * @see FileInputStream
102         * @see SAXParserFactory
103         * @see SAXParser
104         * @see XMLStreamReader
105         */
106        protected void runParser() {
107                SAXParserFactory spf = SAXParserFactory.newInstance();
108                SAXParser parser = null;
109                try 
110                {
111                        parser = spf.newSAXParser();
112                }
113                catch (ParserConfigurationException e) {
114                        e.getStackTrace();
115                }
116                catch (SAXException e)
117                {
118                        System.out.println ("SAXException error when creating XMLReader");
119                        e.getStackTrace();
120                }
121                try
122                {
123                        ClassLoader classLoader = getClass().getClassLoader();
124                        File file = new File(classLoader.getResource("bradleyross/opensource/xerces/utf.xml").getFile());
125                        // parser.parse(new FileInputStream("utf.xml"), new MyHandler());
126                        parser.parse(new FileInputStream(file), new MyHandler());
127                }
128                catch (SAXException e)
129                {
130                        System.out.println ("SAXException while parsing");
131                        e.getStackTrace();
132                }
133                catch (IOException e)
134                {
135                        System.out.println ("IOException while parsing");
136                        e.getStackTrace();
137                }
138        }
139        /**
140         * Main driver.
141         * @param args not used in this case
142         */
143        public static void main(String[] args) {
144                TestXmlParser instance = new TestXmlParser();
145                instance.runParser();
146        }
147}
148
149
150