001: /*
002: * @(#)element.java 1.3 05/01/22
003: *
004: * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.xml;
010:
011: import pnuts.lang.*;
012: import java.util.*;
013: import org.w3c.dom.*;
014:
015: /*
016: * function element(tag) {
017: * function (args[]){
018: * function e() e(newDocument())
019: * function e(doc){
020: * elem = doc.createElement(tag)
021: * for (i: args) {
022: * if (i instanceof String){
023: * elem.appendChild(doc.createTextNode(i))
024: * } else if (i instanceof java.util.Map){
025: * for (entry : i) elem.setAttribute(entry.key, entry.value)
026: * } else if (a instanceof Document){
027: * elem.appendChild(doc.importNode(a.documentElement, true));
028: * } else if (a instanceof Element){
029: * elem.appendChild(doc.importNode(a, true))
030: * } else {
031: * elem.appendChild(i(doc))
032: * }
033: * }
034: * elem
035: * }
036: * }
037: *}
038: */
039: public class element extends PnutsFunction {
040: final static String NEWDOCUMENT = "newDocument".intern();
041: final static Object[] NOARG = new Object[] {};
042:
043: public element() {
044: super ("element");
045: }
046:
047: public boolean defined(int args) {
048: return args == 1 || args == 2;
049: }
050:
051: protected Object exec(Object[] args1, final Context context) {
052: int nargs = args1.length;
053: if (nargs != 1 && nargs != 2) {
054: undefined(args1, context);
055: }
056: Object arg;
057: String ns = null;
058: if (nargs == 2) {
059: ns = String.valueOf(args1[0]);
060: arg = args1[1];
061: } else {
062: arg = args1[0];
063: }
064: final String namespaceURI = ns;
065:
066: if (arg instanceof String) {
067: final String tag = (String) arg;
068: class E extends PnutsFunction {
069: String tag;
070:
071: E(String tag) {
072: this .tag = tag;
073: }
074:
075: public String toString() {
076: return "<" + tag + "/>";
077: }
078:
079: protected Object exec(final Object[] args2,
080: final Context context) {
081:
082: class F extends PnutsFunction {
083: void dispatch(Document doc, Element elem,
084: Object a, Context context) {
085: if (a instanceof String) {
086: elem.appendChild(doc
087: .createTextNode((String) a));
088: } else if (a instanceof Map) {
089: for (Iterator it = ((Map) a).entrySet()
090: .iterator(); it.hasNext();) {
091: Map.Entry entry = (Map.Entry) it
092: .next();
093: Object key = entry.getKey();
094: String value = (String) entry
095: .getValue();
096: if (key instanceof String) {
097: elem.setAttribute((String) key,
098: value);
099: } else {
100: String ns = null;
101: String attr = null;
102: if (key instanceof Object[]) {
103: Object[] keyArray = (Object[]) key;
104: if (keyArray.length >= 2) {
105: ns = String
106: .valueOf(keyArray[0]);
107: attr = String
108: .valueOf(keyArray[1]);
109: }
110: } else if (key instanceof List) {
111: List keyList = (List) key;
112: if (keyList.size() >= 2) {
113: ns = String
114: .valueOf(keyList
115: .get(0));
116: attr = String
117: .valueOf(keyList
118: .get(1));
119: }
120: }
121: if (ns != null && attr != null) {
122: elem.setAttributeNS(ns,
123: attr, value);
124: } else {
125: throw new IllegalArgumentException(
126: String.valueOf(key));
127: }
128: }
129: }
130: } else if (a instanceof PnutsFunction) {
131: elem
132: .appendChild((Node) ((PnutsFunction) a)
133: .call(
134: new Object[] { doc },
135: context));
136: } else if (a instanceof Document) {
137: Element e = ((Document) a)
138: .getDocumentElement();
139: elem.appendChild(doc
140: .importNode(e, true));
141: } else if (a instanceof Element) {
142: elem.appendChild(doc.importNode(
143: (Element) a, true));
144: } else if (a instanceof Node) {
145: Node n = (Node) a;
146: String value = null;
147: do {
148: value = n.getNodeValue();
149: if (value != null) {
150: break;
151: }
152: n = n.getFirstChild();
153: } while (n != null);
154: if (value != null) {
155: elem.appendChild(doc
156: .createTextNode(value));
157: }
158: }
159: }
160:
161: protected Object exec(Object[] args3,
162: Context context) {
163: int nargs = args3.length;
164: Document doc;
165: if (nargs == 0) {
166: PnutsFunction newDoc = (PnutsFunction) context
167: .resolveSymbol(NEWDOCUMENT);
168: doc = (Document) newDoc.call(NOARG,
169: context);
170: } else if (nargs == 1) {
171: doc = (Document) args3[0];
172: } else {
173: undefined(args3, context);
174: return null;
175: }
176: Element elem;
177: if (namespaceURI == null) {
178: elem = doc.createElement(tag);
179: } else {
180: elem = doc.createElementNS(
181: namespaceURI, tag);
182: }
183: for (int i = 0; i < args2.length; i++) {
184: dispatch(doc, elem, args2[i], context);
185: }
186: doc.appendChild(elem);
187: return elem;
188: }
189:
190: public String toString() {
191: return String.valueOf(exec(new Object[] {},
192: context));
193: }
194: }
195: return new F();
196: }
197: }
198: return new E(tag);
199: } else {
200: throw new IllegalArgumentException(String.valueOf(arg));
201: }
202: }
203:
204: public String toString() {
205: return "function element( {namespaceURI,} tag)";
206: }
207: }
|