01: // Copyright (c) 2001, 2002, 2003, 2006 Per M.A. Bothner and Brainfood Inc.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.kawa.xml;
05:
06: import gnu.mapping.*;
07: import gnu.lists.*;
08: import gnu.xml.*;
09: import java.net.URL;
10: import gnu.text.*;
11:
12: /** Implement the XQuery function 'document'. */
13:
14: public class Document {
15: public static final Document document = new Document();
16:
17: public static void parse(Object name, Consumer out)
18: throws Throwable {
19: SourceMessages messages = new SourceMessages();
20: if (out instanceof XConsumer)
21: ((XConsumer) out).beginEntity(name);
22: gnu.xml.XMLParser.parse(name, messages, out);
23: if (messages.seenErrors())
24: throw new SyntaxException(
25: "document function read invalid XML", messages);
26: if (out instanceof XConsumer)
27: ((XConsumer) out).endEntity();
28: }
29:
30: public static KDocument parse(Object uri) throws Throwable {
31: NodeTree doc = new NodeTree();
32: parse(uri, doc);
33: return new KDocument(doc, 0);
34: }
35:
36: /** Internal namespace used to mange cached documents. */
37: static String docNamespace = "http://gnu.org/kawa/cached-documents";
38:
39: public static Object parseCached(Object uri) throws Throwable {
40: Symbol sym = Symbol.make(docNamespace, uri.toString());
41: Environment env = Environment.getCurrent();
42: synchronized (sym) {
43: NamedLocation loc = env.getLocation(sym, null, true);
44: Object val = loc.get(null);
45: if (val != null)
46: return val;
47:
48: NodeTree tree = new NodeTree();
49: SourceMessages messages = new SourceMessages();
50: tree.beginEntity(uri);
51: gnu.xml.XMLParser.parse(uri, messages, tree);
52: if (messages.seenErrors())
53: throw new SyntaxException(
54: "document function read invalid XML", messages);
55: tree.endEntity();
56: val = new KDocument(tree, TreeList.BEGIN_ENTITY_SIZE << 1);
57: loc.set(val);
58: return val;
59: }
60: }
61: }
|