%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.xml.CopyOfTag |
|
|
1 | /* |
|
2 | * Copyright 2002,2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | package org.apache.commons.jelly.tags.xml; |
|
17 | ||
18 | import java.util.Iterator; |
|
19 | import java.util.List; |
|
20 | ||
21 | import org.apache.commons.jelly.JellyTagException; |
|
22 | import org.apache.commons.jelly.MissingAttributeException; |
|
23 | import org.apache.commons.jelly.XMLOutput; |
|
24 | import org.apache.commons.jelly.xpath.XPathTagSupport; |
|
25 | import org.dom4j.Node; |
|
26 | import org.dom4j.io.SAXWriter; |
|
27 | import org.jaxen.JaxenException; |
|
28 | import org.jaxen.XPath; |
|
29 | import org.xml.sax.SAXException; |
|
30 | ||
31 | /** A tag which performs a copy-of operation like the XSLT tag |
|
32 | * |
|
33 | * @author James Strachan |
|
34 | */ |
|
35 | public class CopyOfTag extends XPathTagSupport { |
|
36 | ||
37 | /** The XPath expression to evaluate. */ |
|
38 | private XPath select; |
|
39 | ||
40 | /** Should we output lexical XML data like comments |
|
41 | * or entity names? |
|
42 | */ |
|
43 | private boolean lexical; |
|
44 | ||
45 | 5 | public CopyOfTag() { |
46 | 5 | } |
47 | ||
48 | // Tag interface |
|
49 | //------------------------------------------------------------------------- |
|
50 | public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { |
|
51 | 5 | Object xpathContext = getXPathContext(); |
52 | ||
53 | 5 | if (select == null) { |
54 | 0 | throw new MissingAttributeException( "select" ); |
55 | } |
|
56 | ||
57 | SAXWriter saxWriter; |
|
58 | ||
59 | 5 | if (lexical) { |
60 | 4 | saxWriter = new SAXWriter(output, output); |
61 | } else { |
|
62 | 1 | saxWriter = new SAXWriter(output); |
63 | } |
|
64 | ||
65 | Object obj; |
|
66 | try { |
|
67 | 5 | obj = select.evaluate(xpathContext); |
68 | 5 | } catch (JaxenException e) { |
69 | 0 | throw new JellyTagException("Failed to evaluate XPath expression", e); |
70 | } |
|
71 | ||
72 | try { |
|
73 | 5 | if (obj instanceof List) { |
74 | 4 | List nodes = (List) obj; |
75 | 12 | for (Iterator iter = nodes.iterator(); iter.hasNext(); ) { |
76 | 4 | Object object = iter.next(); |
77 | 4 | if ( object instanceof Node ) { |
78 | 4 | saxWriter.write( (Node) object ); |
79 | } |
|
80 | 0 | else if (object != null ) { |
81 | 0 | saxWriter.write( object.toString() ); |
82 | } |
|
83 | } |
|
84 | 1 | } else if (obj instanceof Node) { |
85 | 0 | saxWriter.write( (Node) obj ); |
86 | } else { |
|
87 | 1 | saxWriter.write( obj.toString() ); |
88 | } |
|
89 | 5 | } catch (SAXException e) { |
90 | 0 | throw new JellyTagException("Failed to write XML output.", e); |
91 | } |
|
92 | 5 | } |
93 | ||
94 | // Properties |
|
95 | //------------------------------------------------------------------------- |
|
96 | /** Sets the XPath expression to evaluate. */ |
|
97 | public void setSelect(XPath select) { |
|
98 | 5 | this.select = select; |
99 | 5 | } |
100 | ||
101 | public void setLexical(boolean lexical) { |
|
102 | 4 | this.lexical = lexical; |
103 | 4 | } |
104 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |