freemarker.ext.xml
public class: NodeListModel [javadoc |
source]
java.lang.Object
freemarker.ext.xml.NodeListModel
All Implemented Interfaces:
TemplateMethodModel, TemplateSequenceModel, TemplateScalarModel, TemplateHashModel, TemplateNodeModel
Deprecated! Use
-
freemarker.ext.dom.NodeModel instead.
A data model adapter for three widespread XML document object model
representations: W3C DOM, dom4j, and JDOM. The adapter automatically
recognizes the used XML object model and provides a unified interface for it
toward the template. The model provides access to all XML InfoSet features
of the XML document and includes XPath support if it has access to the XPath-
evaluator library Jaxen. The model's philosophy (which closely follows that
of XML InfoSet and XPath) is as follows: it always wraps a list of XML nodes
(the "nodelist"). The list can be empty, can have a single element, or can
have multiple elements. Every operation applied to the model is applied to
all nodes in its nodelist. You usually start with a single- element nodelist,
usually the root element node or the document node of the XML tree.
Additionally, the nodes can contain String objects as a result of certain
evaluations (getting the names of elements, values of attributes, etc.)
Implementation note: If you are using W3C DOM documents
built by the Crimson XML parser (or you are using the built-in JDK 1.4 XML
parser, which is essentially Crimson), make sure you call
setNamespaceAware(true) on the
javax.xml.parsers.DocumentBuilderFactory instance used for document
building even when your documents don't use XML namespaces. Failing to do so,
you will experience incorrect behavior when using the documents wrapped with
this model.
- version:
$
- Id: NodeListModel.java,v 1.15 2004/01/06 17:06:43 szegedia Exp $
- author:
Attila
- Szegedi
Constructor: |
public NodeListModel(Object nodes) {
Object node = nodes;
if(nodes instanceof Collection) {
this.nodes = new ArrayList((Collection)nodes);
node = this.nodes.isEmpty() ? null : this.nodes.get(0);
}
else if(nodes != null) {
this.nodes = Collections12.singletonList(nodes);
}
else {
throw new IllegalArgumentException("nodes == null");
}
if(DOM_NODE_CLASS != null && DOM_NODE_CLASS.isInstance(node)) {
navigator = DOM_NAVIGATOR;
}
else if(DOM4J_NODE_CLASS != null && DOM4J_NODE_CLASS.isInstance(node)) {
navigator = DOM4J_NAVIGATOR;
}
else {
// Assume JDOM
navigator = JDOM_NAVIGATOR;
}
namespaces = NS_FACTORY.create();
}
Creates a new NodeListModel, wrapping the passed nodes. Parameters:
nodes - you can pass it a single XML node from any supported
document model, or a Java collection containing any number of nodes.
Passing null is prohibited. To create an empty model, pass it an empty
collection. If a collection is passed, all passed nodes must belong to
the same XML object model, i.e. you can't mix JDOM and dom4j in a single
instance of NodeListModel. The model itself doesn't check for this condition,
as it can be time consuming, but will throw spurious
ClassCastException s when it encounters mixed objects.
Throws:
IllegalArgumentException - if you pass null
|
Method from freemarker.ext.xml.NodeListModel Summary: |
---|
exec, get, get, getAsString, getChildNodes, getNodeName, getNodeNamespace, getNodeType, getParentNode, isEmpty, registerNamespace, size |
Method from freemarker.ext.xml.NodeListModel Detail: |
public Object exec(List arguments) throws TemplateModelException {
if(arguments.size() != 1) {
throw new TemplateModelException(
"Expecting exactly one argument - an XPath expression");
}
return deriveModel(navigator.applyXPath(nodes, (String)arguments.get(0), namespaces));
} Deprecated!Evaluates an XPath expression on XML nodes in this model. |
public TemplateModel get(int index) {
return deriveModel(Collections12.singletonList(nodes.get(index)));
} Deprecated!Selects a single node from this model's nodelist by its list index and
returns a new NodeListModel containing that single node. |
public TemplateModel get(String key) throws TemplateModelException {
// Try a built-in navigator operator
NodeOperator op = navigator.getOperator(key);
String localName = null;
String namespaceUri = "";
// If not a nav op, then check for special keys.
if(op == null && key.length() > 0 && key.charAt(0) == '_") {
if(key.equals("_unique")) {
return deriveModel(removeDuplicates(nodes));
}
else if(key.equals("_filterType") || key.equals("_ftype")) {
return new FilterByType();
}
else if(key.equals("_registerNamespace")) {
if(namespaces.isShared()) {
namespaces = (Namespaces)namespaces.clone();
}
}
}
// Last, do a named child element or attribute lookup
if(op == null) {
int colon = key.indexOf(':");
if(colon == -1) {
// No namespace prefix specified
localName = key;
}
else {
// Namespace prefix specified
localName = key.substring(colon + 1);
String prefix = key.substring(0, colon);
namespaceUri = namespaces.translateNamespacePrefixToUri(prefix);
if(namespaceUri == null) {
throw new TemplateModelException("Namespace prefix " + prefix + " is not registered.");
}
}
if(localName.charAt(0) == '@") {
op = navigator.getAttributeOperator();
localName = localName.substring(1);
}
else {
op = navigator.getChildrenOperator();
}
}
List result = new ArrayList();
for (Iterator iter = nodes.iterator(); iter.hasNext();) {
try {
op.process(iter.next(), localName, namespaceUri, result);
}
catch(RuntimeException e) {
throw new TemplateModelException(e);
}
}
return deriveModel(result);
} Deprecated!Returns a new NodeListModel containing the nodes that result from applying
an operator to this model's nodes. |
public String getAsString() throws TemplateModelException {
StringWriter sw = new StringWriter(size() * 128);
for (Iterator iter = nodes.iterator(); iter.hasNext();) {
Object o = iter.next();
if(o instanceof String) {
sw.write((String)o);
}
else {
navigator.getAsString(o, sw);
}
}
return sw.toString();
} Deprecated!Returns the string representation of the wrapped nodes. String objects in
the nodelist are rendered as-is (with no XML escaping applied). All other
nodes are rendered in the default XML serialization format ("plain XML").
This makes the model quite suited for use as an XML-transformation tool. |
public TemplateSequenceModel getChildNodes() throws TemplateModelException {
return (TemplateSequenceModel)get("_content");
} Deprecated! |
public String getNodeName() throws TemplateModelException {
return getUniqueText((NodeListModel)get("_name"), "name");
} Deprecated! |
public String getNodeNamespace() throws TemplateModelException {
return getUniqueText((NodeListModel)get("_nsuri"), "namespace");
} Deprecated! |
public String getNodeType() throws TemplateModelException {
return getUniqueText((NodeListModel)get("_type"), "type");
} Deprecated! |
public TemplateNodeModel getParentNode() throws TemplateModelException {
return (TemplateNodeModel)get("_parent");
} Deprecated! |
public boolean isEmpty() {
return nodes.isEmpty();
} Deprecated!Returns true if this NodeListModel contains no nodes. |
public void registerNamespace(String prefix,
String uri) {
if(namespaces.isShared()) {
namespaces = (Namespaces)namespaces.clone();
}
namespaces.registerNamespace(prefix, uri);
} Deprecated!Registers a namespace prefix-URI pair for subsequent use in #get(String) as well as for use in XPath expressions. |
public int size() {
return nodes.size();
} Deprecated!Returns the number of nodes in this model's nodelist. |