001: package org.apache.dvsl.dom4j;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.StringWriter;
023: import java.io.Writer;
024:
025: import java.util.List;
026: import java.util.ArrayList;
027: import java.util.Map;
028: import java.util.HashMap;
029: import java.util.Iterator;
030:
031: import org.dom4j.Node;
032: import org.dom4j.Element;
033: import org.dom4j.Document;
034: import org.dom4j.Text;
035: import org.dom4j.Attribute;
036: import org.dom4j.Comment;
037: import org.dom4j.CDATA;
038: import org.dom4j.Branch;
039: import org.dom4j.DocumentHelper;
040: import org.dom4j.XPath;
041:
042: import org.dom4j.io.XMLWriter;
043:
044: import org.apache.dvsl.DVSLNode;
045:
046: /**
047: * wrapper class for dom4j nodes to implement the
048: * DVSLNode interface for template use
049: *
050: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
051: */
052: public class Dom4jNodeImpl implements DVSLNode {
053: protected Node element = null;
054:
055: protected Map attributes = null;
056:
057: /**
058: * this is a bit yecchy - need to revamp
059: */
060: public Dom4jNodeImpl(Element e) {
061: element = e;
062: }
063:
064: public Dom4jNodeImpl(Document e) {
065: element = e;
066: }
067:
068: public Dom4jNodeImpl(Text e) {
069: element = e;
070: }
071:
072: public Dom4jNodeImpl(Attribute e) {
073: element = e;
074: }
075:
076: public Dom4jNodeImpl(Comment e) {
077: element = e;
078: }
079:
080: public Dom4jNodeImpl(CDATA e) {
081: element = e;
082: }
083:
084: private Dom4jNodeImpl() {
085: }
086:
087: /**
088: * returns the name of the node
089: */
090: public String name() {
091: return element.getName();
092: }
093:
094: /**
095: * returns a specificed attributeattribute
096: */
097: public String attribute(String attribute) {
098: if (element instanceof Element)
099: return ((Element) element).attributeValue(attribute);
100:
101: return null;
102: }
103:
104: /**
105: * returns a list of nodes that satisfy
106: * the xpath
107: */
108: public List selectNodes(String xpath) {
109: List l = element.selectNodes(xpath);
110:
111: List list = new ArrayList();
112:
113: for (int i = 0; i < l.size(); i++) {
114: Node n = (Node) l.get(i);
115:
116: if (n != null) {
117: list.add(makeDVSLNode(n));
118: }
119: }
120:
121: return list;
122: }
123:
124: public DVSLNode selectSingleNode(String xpath) {
125: org.dom4j.Node n = element.selectSingleNode(xpath);
126:
127: return makeDVSLNode(n);
128: }
129:
130: public DVSLNode get(String xpath) {
131: return selectSingleNode(xpath);
132: }
133:
134: public String value() {
135: return element.getText();
136: }
137:
138: public Object valueOf(String xpath) {
139: return element.valueOf(xpath);
140: }
141:
142: public String toString() {
143: return value();
144: }
145:
146: public List children() {
147: List list = new ArrayList();
148:
149: if (element.getNodeType() == Node.ELEMENT_NODE) {
150: List nodes = ((Element) element).content();
151:
152: for (int i = 0; i < nodes.size(); i++)
153: list.add(makeDVSLNode((Node) nodes.get(i)));
154: }
155:
156: return list;
157: }
158:
159: /**
160: * assumes a list of DVSLNodes
161: */
162: public String copy(List nodes) {
163: if (nodes == null)
164: return "";
165:
166: StringWriter sw = new StringWriter();
167:
168: for (int i = 0; i < nodes.size(); i++) {
169: DVSLNode dn = (DVSLNode) nodes.get(i);
170:
171: copy((Node) dn.getNodeImpl(), sw);
172: }
173:
174: return sw.toString();
175: }
176:
177: public String copy() {
178: StringWriter sw = new StringWriter();
179: copy(element, sw);
180: return sw.toString();
181: }
182:
183: private void copy(Node node, Writer writer) {
184: XMLWriter xw = new XMLWriter(writer);
185:
186: try {
187: xw.write(node);
188: } catch (Exception ee) {
189: } finally {
190: try {
191: xw.flush();
192: } catch (Exception eee) {
193: }
194: }
195: }
196:
197: public String render() {
198: try {
199: StringWriter sw = new StringWriter();
200:
201: element.write(sw);
202:
203: return sw.toString();
204: } catch (Exception e) {
205: }
206:
207: return "";
208: }
209:
210: public String attrib(String name) {
211: if (element instanceof Element) {
212: Attribute attrib = ((Element) element).attribute(name);
213:
214: if (attrib != null)
215: return attrib.getValue();
216: }
217:
218: return null;
219: }
220:
221: public Object getNodeImpl() {
222: return element;
223: }
224:
225: public Map getAttribMap() {
226: /*
227: * $$$ GMJ sync issue? yes. Do I care?
228: */
229:
230: if (attributes == null)
231: attributes = new HashMap();
232:
233: /*
234: * only Elements have attributes
235: */
236:
237: if (element instanceof Element) {
238: Iterator it = ((Element) element).attributeIterator();
239:
240: while (it.hasNext()) {
241: Attribute at = (Attribute) it.next();
242:
243: attributes.put(at.getName(), at.getValue());
244: }
245: }
246:
247: return attributes;
248: }
249:
250: /**
251: * will recast all of this later
252: */
253: private DVSLNode makeDVSLNode(Node n) {
254: if (n == null)
255: return null;
256:
257: short type = n.getNodeType();
258:
259: if (type == Node.ELEMENT_NODE) {
260: return new Dom4jNodeImpl((Element) n);
261: } else if (type == Node.TEXT_NODE) {
262: return new Dom4jNodeImpl((Text) n);
263: } else if (type == Node.ATTRIBUTE_NODE) {
264: return new Dom4jNodeImpl((Attribute) n);
265: } else if (type == Node.COMMENT_NODE) {
266: return new Dom4jNodeImpl((Comment) n);
267: } else if (type == Node.CDATA_SECTION_NODE) {
268: return new Dom4jNodeImpl((CDATA) n);
269: }
270:
271: return null;
272: }
273: }
|