001: /*
002: * Copyright (c) 2003 The Visigoth Software Society. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowledgement:
019: * "This product includes software developed by the
020: * Visigoth Software Society (http://www.visigoths.org/)."
021: * Alternately, this acknowledgement may appear in the software itself,
022: * if and wherever such third-party acknowledgements normally appear.
023: *
024: * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
025: * project contributors may be used to endorse or promote products derived
026: * from this software without prior written permission. For written
027: * permission, please contact visigoths@visigoths.org.
028: *
029: * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
030: * nor may "FreeMarker" or "Visigoth" appear in their names
031: * without prior written permission of the Visigoth Software Society.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of the Visigoth Software Society. For more
049: * information on the Visigoth Software Society, please see
050: * http://www.visigoths.org/
051: */
052:
053: package freemarker.ext.dom;
054:
055: import org.w3c.dom.*;
056: import freemarker.template.*;
057: import freemarker.template.utility.StringUtil;
058: import freemarker.core.Environment;
059:
060: class ElementModel extends NodeModel implements TemplateScalarModel {
061:
062: public ElementModel(Element element) {
063: super (element);
064: }
065:
066: public boolean isEmpty() {
067: return false;
068: }
069:
070: /**
071: * An Element node supports various hash keys.
072: * Any key that corresponds to the tag name of any child elements
073: * returns a sequence of those elements. The special key "*" returns
074: * all the element's direct children.
075: * The "**" key return all the element's descendants in the order they
076: * occur in the document.
077: * Any key starting with '@' is taken to be the name of an element attribute.
078: * The special key "@@" returns a hash of all the element's attributes.
079: * The special key "/" returns the root document node associated with this element.
080: */
081: public TemplateModel get(String key) throws TemplateModelException {
082: if (key.equals("*")) {
083: NodeListModel ns = new NodeListModel(this );
084: TemplateSequenceModel children = getChildNodes();
085: for (int i = 0; i < children.size(); i++) {
086: NodeModel child = (NodeModel) children.get(i);
087: if (child.node.getNodeType() == Node.ELEMENT_NODE) {
088: ns.add(child);
089: }
090: }
091: return ns;
092: }
093: if (key.equals("**")) {
094: Element elem = (Element) node;
095: return new NodeListModel(elem.getElementsByTagName("*"),
096: this );
097: }
098: if (key.startsWith("@")) {
099: if (key.equals("@@") || key.equals("@*")) {
100: return new NodeListModel(node.getAttributes(), this );
101: }
102: if (key.equals("@@start_tag")) {
103: NodeOutputter nodeOutputter = new NodeOutputter(node);
104: return new SimpleScalar(nodeOutputter
105: .getOpeningTag((Element) node));
106: }
107: if (key.equals("@@end_tag")) {
108: NodeOutputter nodeOutputter = new NodeOutputter(node);
109: return new SimpleScalar(nodeOutputter
110: .getClosingTag((Element) node));
111: }
112: if (key.equals("@@attributes_markup")) {
113: StringBuffer buf = new StringBuffer();
114: NodeOutputter nu = new NodeOutputter(node);
115: nu.outputContent(node.getAttributes(), buf);
116: return new SimpleScalar(buf.toString().trim());
117: }
118: if (StringUtil.isXMLID(key.substring(1))) {
119: Attr att = getAttribute(key.substring(1), Environment
120: .getCurrentEnvironment());
121: if (att == null) {
122: return new NodeListModel(this );
123: }
124: return wrap(att);
125: }
126: }
127: if (StringUtil.isXMLID(key)) {
128: NodeListModel result = ((NodeListModel) getChildNodes())
129: .filterByName(key);
130: if (result.size() == 1) {
131: return result.get(0);
132: }
133: return result;
134: }
135: return super .get(key);
136: }
137:
138: public String getAsString() throws TemplateModelException {
139: NodeList nl = node.getChildNodes();
140: String result = "";
141: for (int i = 0; i < nl.getLength(); i++) {
142: Node child = nl.item(i);
143: int nodeType = child.getNodeType();
144: if (nodeType == Node.ELEMENT_NODE) {
145: String msg = "Only elements with no child elements can be processed as text."
146: + "\nThis element with name \""
147: + node.getNodeName()
148: + "\" has a child element named: "
149: + child.getNodeName();
150: throw new TemplateModelException(msg);
151: } else if (nodeType == Node.TEXT_NODE
152: || nodeType == Node.CDATA_SECTION_NODE) {
153: result += child.getNodeValue();
154: }
155: }
156: return result;
157: }
158:
159: public String getNodeName() {
160: String result = node.getLocalName();
161: if (result == null || result.equals("")) {
162: result = node.getNodeName();
163: }
164: return result;
165: }
166:
167: String getQualifiedName() {
168: String nodeName = getNodeName();
169: String nsURI = getNodeNamespace();
170: if (nsURI == null || nsURI.length() == 0) {
171: return nodeName;
172: }
173: Environment env = Environment.getCurrentEnvironment();
174: String defaultNS = env.getDefaultNS();
175: String prefix;
176: if (defaultNS != null && defaultNS.equals(nsURI)) {
177: prefix = Template.DEFAULT_NAMESPACE_PREFIX;
178: } else {
179: prefix = env.getPrefixForNamespace(nsURI);
180:
181: }
182: if (prefix == null) {
183: return null; // We have no qualified name, because there is no prefix mapping
184: }
185: if (prefix.length() > 0) {
186: prefix += ":";
187: }
188: return prefix + nodeName;
189: }
190:
191: private Attr getAttribute(String qname, Environment env) {
192: Element element = (Element) node;
193: Attr result = element.getAttributeNode(qname);
194: if (result != null)
195: return result;
196: int colonIndex = qname.indexOf(':');
197: if (colonIndex > 0) {
198: String prefix = qname.substring(0, colonIndex);
199: String uri;
200: if (prefix.equals(Template.DEFAULT_NAMESPACE_PREFIX)) {
201: uri = Environment.getCurrentEnvironment()
202: .getDefaultNS();
203: } else {
204: uri = Environment.getCurrentEnvironment()
205: .getNamespaceForPrefix(prefix);
206: }
207: String localName = qname.substring(1 + colonIndex);
208: if (uri != null) {
209: result = element.getAttributeNodeNS(uri, localName);
210: }
211: }
212: return result;
213: }
214:
215: boolean matchesName(String name, Environment env) {
216: return StringUtil.matchesName(name, getNodeName(),
217: getNodeNamespace(), env);
218: }
219: }
|