001 import java.io.IOException;
002 //import org.apache.xerces.parsers.DOMParser;
003 import org.apache.xerces.dom.DOMImplementationImpl;
004 import org.w3c.dom.ls.*;
005 import org.w3c.dom.*;
006 import org.xml.sax.SAXException;
007
008 /**
009 * This class provides static methods to analyse and display a DOM
010 * structure of an XML Document
011 * @author Udo Altmann
012 * @version 1.0
013 *
014 */
015
016 public class analyse {
017
018 /**
019 * @param args File to be analysed
020 * @throws IOException
021 * @throws SAXException
022 */
023 public static void main(String args[]) throws IOException, SAXException {
024 //DOMParser dp = new DOMParser();
025
026
027 DOMImplementationLS impl = (DOMImplementationLS) DOMImplementationImpl.getDOMImplementation();
028 LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
029 DOMConfiguration conf = parser.getDomConfig();
030
031 conf.setParameter("validate-if-schema", new Boolean(true));
032 conf.setParameter("cdata-sections", new Boolean(true));
033 conf.setParameter("entities", new Boolean(true));
034
035
036 DOMStringList sl = conf.getParameterNames();
037 int i;
038 System.out.println("Parameters for parsing:");
039 for(i=0; i<sl.getLength(); i++){
040 System.out.println("\t"+ sl.item(i) + ": " + conf.getParameter(sl.item(i)));
041 }
042 //
043 // Es gibt keine DOM-Methode zum Anlegen von Documenten
044 //
045 Document doc = parser.parseURI(args[0]);
046 //
047 // Die Verarbeitung startet mit dem Interface Document
048 //
049 // Document doc = dp.getDocument();
050 //
051 // Dokumente sind auch Knoten
052 //
053 process(doc);
054 }
055
056 /**
057 * @param nd Node to be analysed. Child nodes are processed recursively
058 */
059 public static void process(Node nd) {
060 System.out.println(
061 "Node type:\t"
062 + nd.getNodeType()
063 + " - "
064 + nodeTypeToString(nd.getNodeType()));
065 System.out.println("\tname:\t" + nd.getNodeName());
066 System.out.println("\tvalue:\t\"" + nd.getNodeValue() + "\"");
067 if (nd.getParentNode() != null) {
068 System.out.println(
069 "\tparent:\t"
070 + nodeTypeToString(nd.getParentNode().getNodeType())
071 + "\t"
072 + nd.getParentNode().getNodeName());
073 } else {
074 System.out.println("\tno parent\t");
075 }
076 System.out.println("\tspecific details:\t");
077 //nd.
078 switch (nd.getNodeType()) {
079 case Node.DOCUMENT_NODE :
080 Document doc = (Document) nd;
081 System.out.println(
082 "\t\tDocumentElement: "
083 + doc.getDocumentElement().getNodeName());
084 break;
085 case Node.ELEMENT_NODE :
086 Element el = (Element) nd;
087 //
088 // hasAttributes ist nicht in DOM-Level 1!
089 //
090 if (el.hasAttributes()) {
091 System.out.println("\t\tgetTagName: " + el.getTagName());
092 System.out.println("\t\tAttribute:");
093 NamedNodeMap nnm = el.getAttributes();
094 int i;
095 for (i = 0; i < nnm.getLength(); i++) {
096 process(nnm.item(i));
097 }
098 } else {
099 System.out.println("\t\tno attributes!");
100 }
101 break;
102 case Node.ATTRIBUTE_NODE :
103 Attr at = (Attr) nd;
104 System.out.println("\t\tgetName:" + at.getName());
105 System.out.println("\t\tspecified: " + at.getSpecified());
106 System.out.println("\t\tgetValue: " + at.getValue());
107 break;
108 case Node.COMMENT_NODE :
109 Comment cm = (Comment) nd;
110 System.out.println("\t\tgetData: " + cm.getData());
111 System.out.println("\t\tgetLength: " + cm.getLength());
112 break;
113 case Node.TEXT_NODE :
114 Text tn = (Text) nd;
115 System.out.println("\t\tgetData: " + tn.getData());
116 System.out.println("\t\tgetLength: " + tn.getLength());
117 break;
118 case Node.CDATA_SECTION_NODE :
119 Text cs = (Text) nd;
120 System.out.println("\t\tgetData: " + cs.getData());
121 System.out.println("\t\tgetLength: " + cs.getLength());
122 break;
123 case Node.DOCUMENT_TYPE_NODE :
124 int i;
125 DocumentType dt = (DocumentType) nd;
126 System.out.println("\t\tgetName: " + dt.getName());
127 System.out.println("\t\tgetPublicId: " + dt.getPublicId());
128 System.out.println("\t\tgetSytemId: " + dt.getSystemId());
129 //
130 // DOM 2 und parserabhängig
131 //
132 System.out.println(
133 "\t\tgetInternalSubset: " + dt.getInternalSubset());
134 NamedNodeMap nnm = dt.getEntities();
135 if (nnm.getLength() <= 0) {
136 System.out.println("\t\tno Entities");
137 } else {
138 for (i = 0; i < nnm.getLength(); i++) {
139 process(nnm.item(i));
140 }
141 }
142 nnm = dt.getNotations();
143 if (nnm.getLength() <= 0) {
144 System.out.println("\t\tno Notations");
145 } else {
146 for (i = 0; i < nnm.getLength(); i++) {
147 process(nnm.item(i));
148 }
149 }
150 break;
151 case Node.NOTATION_NODE :
152 Notation no = (Notation) nd;
153 System.out.println("\t\tgetPublicId: " + no.getPublicId());
154 System.out.println("\t\tgetSytemId: " + no.getSystemId());
155 break;
156 case Node.ENTITY_NODE :
157 Entity en = (Entity) nd;
158 System.out.println(
159 "\t\tgetNotationName: " + en.getNotationName());
160 System.out.println("\t\tgetPublicId: " + en.getPublicId());
161 System.out.println("\t\tgetSytemId: " + en.getSystemId());
162 break;
163 case Node.PROCESSING_INSTRUCTION_NODE :
164 ProcessingInstruction pe = (ProcessingInstruction) nd;
165 System.out.println("\t\tgetTarget: " + pe.getTarget());
166 System.out.println("\t\tgetData: " + pe.getData());
167 break;
168 //
169 // Für die folgenden Knotentyp gibt es keine speziellen Attribute/Methoden
170 //
171 case Node.ENTITY_REFERENCE_NODE :
172 //
173 // EntityRefences werden evtl. nicht erkannt, wenn sie beim Parsen ersetzt werden
174 //
175 // EntityReference er = (EntityReference) nd;
176 // break;
177 case Node.DOCUMENT_FRAGMENT_NODE :
178 // DocumentFragment df = (DocumentFragment) nd;
179 // break;
180 default :
181 System.out.println("\t\tno details!");
182 }
183 if (nd.hasChildNodes()) {
184 NodeList nl = nd.getChildNodes();
185 int i;
186 for (i = 0; i < nl.getLength(); i++) {
187 process(nl.item(i));
188 }
189 }
190 }
191
192 /**
193 * Static method to convert Nodetypes to Strings
194 * @param s Type as short
195 * @return Type as String
196 */
197 public static String nodeTypeToString(short s) {
198
199 switch (s) {
200 case Node.ATTRIBUTE_NODE :
201 return "ATTRIBUTE_NODE";
202 case Node.CDATA_SECTION_NODE :
203 return "CDATA_SECTION_NODE";
204 case Node.COMMENT_NODE :
205 return "COMMENT_NODE";
206 case Node.DOCUMENT_FRAGMENT_NODE :
207 return "DOCUMENT_FRAGMENT_NODE";
208 case Node.DOCUMENT_NODE :
209 return "DOCUMENT_NODE";
210 case Node.DOCUMENT_TYPE_NODE :
211 return "DOCUMENT_TYPE_NODE";
212 case Node.ELEMENT_NODE :
213 return "ELEMENT_NODE";
214 case Node.ENTITY_NODE :
215 return "ENTITY_NODE";
216 case Node.ENTITY_REFERENCE_NODE :
217 return "ENTITY_REFERENCE_NODE";
218 case Node.NOTATION_NODE :
219 return "NOTATION_NODE";
220 case Node.PROCESSING_INSTRUCTION_NODE :
221 return "PROCESSING_INSTRUCTION_NODE";
222 case Node.TEXT_NODE :
223 return "TEXT_NODE";
224 default :
225
226 return null;
227 }
228 }
229
230
231 }