📄 nodemodel.java
字号:
/*
* Copyright (c) 2003 The Visigoth Software Society. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Visigoth Software Society (http://www.visigoths.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
* project contributors may be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact visigoths@visigoths.org.
*
* 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
* nor may "FreeMarker" or "Visigoth" appear in their names
* without prior written permission of the Visigoth Software Society.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Visigoth Software Society. For more
* information on the Visigoth Software Society, please see
* http://www.visigoths.org/
*/
package freemarker.ext.dom;
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import freemarker.ext.util.WrapperTemplateModel;
import freemarker.log.Logger;
import freemarker.template.AdapterTemplateModel;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateNodeModel;
import freemarker.template.TemplateSequenceModel;
/**
* A base class for wrapping a W3C DOM Node as a FreeMarker template model.
* @author <a href="mailto:jon@revusky.com">Jonathan Revusky</a>
* @version $Id: NodeModel.java,v 1.80 2005/06/22 11:33:31 ddekany Exp $
*/
abstract public class NodeModel
implements TemplateNodeModel, TemplateHashModel, TemplateSequenceModel,
AdapterTemplateModel, WrapperTemplateModel
{
static final Logger logger = Logger.getLogger("freemarker.dom");
static private DocumentBuilderFactory docBuilderFactory;
static private Map xpathSupportMap = Collections.synchronizedMap(new WeakHashMap());
static private XPathSupport jaxenXPathSupport;
static private ErrorHandler errorHandler;
static Class xpathSupportClass;
static {
try {
useDefaultXPathSupport();
} catch (Exception e) {
// do nothing
}
if (xpathSupportClass == null && logger.isWarnEnabled()) {
logger.warn("No XPath support is available.");
}
}
/**
* The W3C DOM Node being wrapped.
*/
final Node node;
private TemplateSequenceModel children;
private NodeModel parent;
/**
* Sets the DOM Parser implementation to be used when building NodeModel
* objects from XML files.
*/
static public void setDocumentBuilderFactory(DocumentBuilderFactory docBuilderFactory) {
NodeModel.docBuilderFactory = docBuilderFactory;
}
/**
* @return the DOM Parser implementation that is used when
* building NodeModel objects from XML files.
*/
static public DocumentBuilderFactory getDocumentBuilderFactory() {
if (docBuilderFactory == null) {
docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
docBuilderFactory.setIgnoringElementContentWhitespace(true);
}
return docBuilderFactory;
}
/**
* sets the error handler to use when parsing the document.
*/
static public void setErrorHandler(ErrorHandler errorHandler) {
NodeModel.errorHandler = errorHandler;
}
/**
* Create a NodeModel from a SAX input source. Adjacent text nodes will be merged (and CDATA sections
* are considered as text nodes).
* @param removeComments whether to remove all comment nodes
* (recursively) from the tree before processing
* @param removePIs whether to remove all processing instruction nodes
* (recursively from the tree before processing
*/
static public NodeModel parse(InputSource is, boolean removeComments, boolean removePIs)
throws SAXException, IOException, ParserConfigurationException
{
DocumentBuilder builder = getDocumentBuilderFactory().newDocumentBuilder();
if (errorHandler != null) builder.setErrorHandler(errorHandler);
Document doc = builder.parse(is);
if (removeComments && removePIs) {
simplify(doc);
} else {
if (removeComments) {
removeComments(doc);
}
if (removePIs) {
removePIs(doc);
}
mergeAdjacentText(doc);
}
return wrap(doc);
}
/**
* Create a NodeModel from an XML input source. By default,
* all comments and processing instruction nodes are
* stripped from the tree.
*/
static public NodeModel parse(InputSource is)
throws SAXException, IOException, ParserConfigurationException {
return parse(is, true, true);
}
/**
* Create a NodeModel from an XML file.
* @param removeComments whether to remove all comment nodes
* (recursively) from the tree before processing
* @param removePIs whether to remove all processing instruction nodes
* (recursively from the tree before processing
*/
static public NodeModel parse(File f, boolean removeComments, boolean removePIs)
throws SAXException, IOException, ParserConfigurationException
{
DocumentBuilder builder = getDocumentBuilderFactory().newDocumentBuilder();
if (errorHandler != null) builder.setErrorHandler(errorHandler);
Document doc = builder.parse(f);
if (removeComments) {
removeComments(doc);
}
if (removePIs) {
removePIs(doc);
}
mergeAdjacentText(doc);
return wrap(doc);
}
/**
* Create a NodeModel from an XML file. By default,
* all comments and processing instruction nodes are
* stripped from the tree.
*/
static public NodeModel parse(File f)
throws SAXException, IOException, ParserConfigurationException {
return parse(f, true, true);
}
protected NodeModel(Node node) {
this.node = node;
}
/**
* @return the underling W3C DOM Node object that this TemplateNodeModel
* is wrapping.
*/
public Node getNode() {
return node;
}
public TemplateModel get(String key) throws TemplateModelException {
if (key.startsWith("@@")) {
if (key.equals("@@text")) {
return new SimpleScalar(getText(node));
}
if (key.equals("@@namespace")) {
String nsURI = node.getNamespaceURI();
return nsURI == null ? null : new SimpleScalar(nsURI);
}
if (key.equals("@@local_name")) {
String localName = node.getLocalName();
if (localName == null) {
localName = getNodeName();
}
return new SimpleScalar(localName);
}
if (key.equals("@@markup")) {
StringBuffer buf = new StringBuffer();
NodeOutputter nu = new NodeOutputter(node);
nu.outputContent(node, buf);
return new SimpleScalar(buf.toString());
}
if (key.equals("@@nested_markup")) {
StringBuffer buf = new StringBuffer();
NodeOutputter nu = new NodeOutputter(node);
nu.outputContent(node.getChildNodes(), buf);
return new SimpleScalar(buf.toString());
}
if (key.equals("@@qname")) {
String qname = getQualifiedName();
return qname == null ? null : new SimpleScalar(qname);
}
}
XPathSupport xps = getXPathSupport();
if (xps != null) {
return xps.executeQuery(node, key);
} else {
throw new TemplateModelException(
"Can't try to resolve the XML query key, because no XPath support is available. "
+ "It's either malformed or an XPath expression: " + key);
}
}
public TemplateNodeModel getParentNode() {
if (parent == null) {
Node parentNode = node.getParentNode();
if (parentNode == null) {
if (node instanceof Attr) {
parentNode = ((Attr) node).getOwnerElement();
}
}
parent = wrap(parentNode);
}
return parent;
}
public TemplateSequenceModel getChildNodes() {
if (children == null) {
children = new NodeListModel(node.getChildNodes(), this);
}
return children;
}
public final String getNodeType() throws TemplateModelException {
short nodeType = node.getNodeType();
switch (nodeType) {
case Node.ATTRIBUTE_NODE : return "attribute";
case Node.CDATA_SECTION_NODE : return "text";
case Node.COMMENT_NODE : return "comment";
case Node.DOCUMENT_FRAGMENT_NODE : return "document_fragment";
case Node.DOCUMENT_NODE : return "document";
case Node.DOCUMENT_TYPE_NODE : return "document_type";
case Node.ELEMENT_NODE : return "element";
case Node.ENTITY_NODE : return "entity";
case Node.ENTITY_REFERENCE_NODE : return "entity_reference";
case Node.NOTATION_NODE : return "notation";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -