01: /*
02: * @(#)readDocument.java 1.2 04/12/06
03: *
04: * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.xml;
10:
11: import pnuts.lang.*;
12: import org.pnuts.util.*;
13: import javax.xml.parsers.*;
14: import javax.xml.transform.*;
15: import javax.xml.transform.stream.*;
16: import javax.xml.transform.dom.*;
17: import java.util.*;
18: import java.io.*;
19: import org.w3c.dom.Node;
20: import org.xml.sax.ErrorHandler;
21: import org.xml.sax.SAXException;
22:
23: public class readDocument extends PnutsFunction {
24:
25: public readDocument() {
26: super ("readDocument");
27: }
28:
29: public boolean defined(int nargs) {
30: return nargs >= 1 && nargs <= 4;
31: }
32:
33: protected Object exec(Object[] args, Context context) {
34: int nargs = args.length;
35: Object schema = null;
36: Map properties = null;
37: Object errorHandler;
38: Object input;
39: switch (nargs) {
40: case 4:
41: schema = args[3];
42: case 3:
43: properties = (Map) args[2];
44: case 2:
45: errorHandler = args[1];
46: break;
47: case 1:
48: errorHandler = Util.getDefaultErrorHandler(context);
49: break;
50: default:
51: undefined(args, context);
52: return null;
53: }
54: input = args[0];
55: if (properties == null) {
56: properties = new LinkedHashMap();
57: }
58: if (schema != null) {
59: properties.put(Util.KEY_SCHEMA, schema);
60: }
61: DocumentBuilder builder = Util.getDocumentBuilder(properties,
62: context);
63: if (errorHandler != null) {
64: builder.setErrorHandler((ErrorHandler) Util.contentHandler(
65: errorHandler, context));
66: }
67: try {
68: return builder.parse(Util.inputSource(input, context));
69: } catch (IOException e1) {
70: throw new PnutsException(e1, context);
71: } catch (SAXException e2) {
72: throw new PnutsException(e2, context);
73: }
74: }
75:
76: public String toString() {
77: return "function readDocument(in {, errorHandler, {, properties {, schema }}} )";
78: }
79: }
|