summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/DamlEngine.java
blob: e54ee86ae1fbfce67fa3a8e5456b382a45e30503 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package org.madore.damlengine;

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
// import org.xml.sax.EntityResolver;
import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl;

public final class DamlEngine {

    public static final String XMLNS_NS = XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
    public static final String XML_NS = XMLConstants.XML_NS_URI;
    public static final String XHTML_NS = "http://www.w3.org/1999/xhtml";
    public static final String DAML_NS = "http://www.madore.org/~david/NS/daml/";
    public static final String RDF_NS = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    public static final String RSS10_NS = "http://purl.org/rss/1.0/";
    public static final String DUBLINCORE_NS = "http://purl.org/dc/elements/1.1/";
    public static final String SYNDICATION_NS = "http://purl.org/rss/1.0/modules/syndication/";

    public static final class DamlNSMapping implements NamespaceContext {
	// This is used for XPath resolution (_not_ for parsing the document).
	public String getNamespaceURI(String prefix) {
	    if ( prefix == null )
		throw new IllegalArgumentException("getNamespaceURI() called with null prefix");
	    else if ( prefix.equals("") )
		return XHTML_NS;
	    else if ( prefix.equals("d") )
		return DAML_NS;
	    else if ( prefix.equals("xml") )
		return XML_NS;
	    else if ( prefix.equals("xmlns") )
		return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
	    else
		return XMLConstants.NULL_NS_URI;
	}
	public String getPrefix(String uri) {
	    throw new UnsupportedOperationException("getPrefix() not implemented");
	}
	public java.util.Iterator<String> getPrefixes(String uri) {
	    throw new UnsupportedOperationException("getPrefixes() not implemented");
	}
    }

    public static final class GetDocumentBuilder {
	static final DocumentBuilder db;
	static {
	    final Resolver resolver = new Resolver();
	    final DocumentBuilderFactory dbf = new DocumentBuilderFactoryImpl();
	    dbf.setNamespaceAware(true);
	    dbf.setValidating(false);
	    try {
		db = dbf.newDocumentBuilder();
	    } catch (ParserConfigurationException e) {
		throw new RuntimeException(e);
	    }
	    db.setEntityResolver(resolver);
	}
    }

    private DamlEngine() { // Forbid instantiation
	throw new AssertionError("DamlEngine cannot be instantiated");
    }

    public static class RootTodo extends TodoItem {
	public RootTodo(Context ctx) {
	    super(ctx, null);
	}
	public void handle() {
	    TodoItem it
		= TodoElement.getTodoElement(ctx.doc.getDocumentElement(),
					     ctx, this);
	    this.ownerDeque.registerAtEnd(it);
	}
    }

    public static void processDocument(Document doc,
				       Context.WeblogSelectionContext wsc) {
	TodoDeque todoDeque = new TodoDeque();
	Context ctx = new Context(doc);
	ctx.wsc = wsc;
	todoDeque.registerAtEnd(new RootTodo(ctx));
	todoDeque.dispatchLoop();
    }

    public static void processDocument(Document doc) {
	processDocument(doc, null);
    }

    public static void fullProcess(InputStream in, OutputStream out,
				   Context.WeblogSelectionContext wsc)
	throws Exception {

	final DocumentBuilder db = GetDocumentBuilder.db;

	Document doc = db.parse(in);
	processDocument(doc, wsc);
	doc.normalizeDocument();
	Unparser unparser
	    = new Unparser(doc, new OutputStreamWriter(out, "UTF-8"),
			   "html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"");
	unparser.unparse();

    }

    public static void fullProcess(InputStream in, OutputStream out)
	throws Exception {
	fullProcess(in, out, null);
    }

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

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

	for (String fname : args) {
	    fullProcess (new FileInputStream(fname), System.out);
	}

    }

}