01: /*
02: * @(#)parseXML.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 java.util.*;
13: import org.xml.sax.*;
14: import org.xml.sax.ContentHandler;
15: import javax.xml.parsers.*;
16: import org.xml.sax.helpers.DefaultHandler;
17:
18: public class parseXML extends PnutsFunction {
19: public parseXML() {
20: super ("parseXML");
21: }
22:
23: public boolean defined(int nargs) {
24: return nargs >= 2 && nargs <= 4;
25: }
26:
27: protected Object exec(Object[] args, Context context) {
28: int nargs = args.length;
29: Object input;
30: Object handler;
31: Map properties = null;
32: Object schema = null;
33: if (nargs == 2) {
34: input = args[0];
35: handler = args[1];
36: schema = null;
37: } else if (nargs == 3) {
38: input = args[0];
39: handler = args[1];
40: properties = (Map) args[2];
41: } else if (nargs == 4) {
42: input = args[0];
43: handler = args[1];
44: properties = (Map) args[2];
45: schema = args[3];
46: } else {
47: undefined(args, context);
48: return null;
49: }
50: if (properties == null) {
51: properties = new LinkedHashMap();
52: }
53: if (schema != null) {
54: properties.put(Util.KEY_SCHEMA, schema);
55: }
56: DefaultHandler defaultHandler;
57: if (handler == null) {
58: defaultHandler = Util.getDefaultErrorHandler(context);
59: } else {
60: defaultHandler = (DefaultHandler) Util.contentHandler(
61: handler, context);
62: }
63: try {
64: SAXParser parser = Util.getSAXParser(properties, context);
65: parser.parse(Util.inputSource(input, context),
66: defaultHandler);
67: return null;
68: } catch (Exception e) {
69: throw new PnutsException(e, context);
70: }
71: }
72:
73: public String toString() {
74: return "function parseXML(input, handler {, properties {, schema }})";
75: }
76: }
|