summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/DamlEngine.java
blob: ecbdefdf3065ec590b8b77de9dbf2455c46e3243 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package org.madore.damlengine;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.*;
import org.xml.sax.EntityResolver;
import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl;

public final class DamlEngine {

    private static Document doc;

    private DamlEngine() { }  // Forbid instantiation

    public static void processDocument() {
	// ...
    }

    public static void main(String[] args)
	throws Exception {

	final Resolver resolver = new Resolver();
        final DocumentBuilderFactory dbf = new DocumentBuilderFactoryImpl();
        dbf.setNamespaceAware(true);
        dbf.setValidating(false);
	final DocumentBuilder db = dbf.newDocumentBuilder();
	db.setEntityResolver((EntityResolver)resolver);

	if ( args.length == 0 ) {
	    System.err.println("expecting filename as argument");
	}

	for (String fname : args) {
	    doc = db.parse(fname);
	    processDocument();
	    Node node0 = null;
	    Node node = doc;
	    while ( node != null || node0 != null ) {
		if ( node != null && node.getNodeType() == Node.ELEMENT_NODE ) {
		    Element elt = (Element)node;
		    System.out.print("<"+elt.getTagName());
		    if ( elt.hasAttributes() ) {
			NamedNodeMap attrs = elt.getAttributes();
			Node n2;
			for ( int i=0 ; (n2=attrs.item(i)) != null ; i++ ) {
			    Attr attr = (Attr)n2;
			    System.out.print(" "+attr.getName()
					     +"=\"(value)\"");
			}
		    }
		    if ( ! elt.hasChildNodes() ) {
			System.out.print(" />");
			node = node.getNextSibling();
		    } else {
			System.out.print(">");
			node0 = node;
			node = node0.getFirstChild();
		    }
		} else if ( node != null && node.getNodeType() == Node.DOCUMENT_NODE ) {
		    node0 = node;
		    node = node0.getFirstChild();
		} else if ( node != null ) {
		    if ( node.getNodeType() == Node.TEXT_NODE ) {
			System.out.print("(text)");
		    } else if ( node.getNodeType() == Node.COMMENT_NODE ) {
			System.out.print("<!--(comment)-->");
		    } else if ( node.getNodeType() == Node.CDATA_SECTION_NODE ) {
			System.out.print("<![CDATA[(cdata)]]>");
		    } else {
			System.out.print("<!--(some other kind of node)-->");
		    }
		    node = node.getNextSibling();
		} else {
		    node = node0;
		    node0 = node.getParentNode();
		    if ( node.getNodeType() == Node.ELEMENT_NODE ) {
			Element elt = (Element)node;
			System.out.print("</"+elt.getTagName()+">");
		    }
		    node = node.getNextSibling();
		}
	    }
	}

    }

}