📄 nodelistmodel.java
字号:
private static final class AllChildrenOp implements NodeOperator {
public List operate(Object node)
{
if (node instanceof Element)
return((Element)node).getChildren();
else if (node instanceof Document) {
Element root = ((Document)node).getRootElement();
return root == null ? Collections.EMPTY_LIST : Collections12.singletonList(root);
}
// With 2.1 semantics it makes more sense to just return a null and let the core
// throw an InvalidReferenceException and the template writer can use ?exists etcetera. (JR)
return null;
/*
else
throw new TemplateModelException("_allChildren can not be applied on " + node.getClass());
*/
}
}
private static final class NamedChildrenOp implements NamedNodeOperator {
public List operate(Object node, String localName, Namespace namespace)
{
if (node instanceof Element) {
return((Element)node).getChildren(localName, namespace);
} else if (node instanceof Document) {
Element root = ((Document)node).getRootElement();
if (root != null &&
root.getName().equals(localName) &&
root.getNamespaceURI().equals(namespace.getURI())) {
return Collections12.singletonList(root);
} else
return Collections.EMPTY_LIST;
}
// With 2.1 semantics it makes more sense to just return a null and let the core
// throw an InvalidReferenceException and the template writer can use ?exists etcetera. (JR)
return null;
/*
else
throw new TemplateModelException("_namedChildren can not be applied on " + node.getClass());
*/
}
}
private static final class AllAttributesOp implements NodeOperator {
public List operate(Object node)
{
// With 2.1 semantics it makes more sense to just return a null and let the core
// throw an InvalidReferenceException and the template writer can use ?exists etcetera. (JR)
if (!(node instanceof Element)) {
return null;
}
return ((Element)node).getAttributes();
/*
else
throw new TemplateModelException("_allAttributes can not be applied on " + node.getClass());
*/
}
}
private static final class NamedAttributeOp implements NamedNodeOperator {
public List operate(Object node, String localName, Namespace namespace)
{
Attribute attr = null;
if (node instanceof Element) {
Element element = (Element)node;
attr = element.getAttribute(localName, namespace);
} else if (node instanceof ProcessingInstruction) {
ProcessingInstruction pi = (ProcessingInstruction)node;
if ("target".equals(localName))
attr = new Attribute("target", pi.getTarget());
else if ("data".equals(localName))
attr = new Attribute("data", pi.getData());
else
attr = new Attribute(localName, pi.getValue(localName));
} else if (node instanceof DocType) {
DocType doctype = (DocType)node;
if ("publicId".equals(localName))
attr = new Attribute("publicId", doctype.getPublicID());
else if ("systemId".equals(localName))
attr = new Attribute("systemId", doctype.getSystemID());
else if ("elementName".equals(localName))
attr = new Attribute("elementName", doctype.getElementName());
}
// With 2.1 semantics it makes more sense to just return a null and let the core
// throw an InvalidReferenceException and the template writer can use ?exists etcetera. (JR)
else {
return null;
}
/*
else
throw new TemplateModelException("_allAttributes can not be applied on " + node.getClass());
*/
return attr == null ? Collections.EMPTY_LIST : Collections12.singletonList(attr);
}
}
private static final class NameOp implements NodeOperator {
public List operate(Object node)
{
if (node instanceof Element)
return Collections12.singletonList(((Element)node).getName());
else if (node instanceof Attribute)
return Collections12.singletonList(((Attribute)node).getName());
else if (node instanceof EntityRef)
return Collections12.singletonList(((EntityRef)node).getName());
else if (node instanceof ProcessingInstruction)
return Collections12.singletonList(((ProcessingInstruction)node).getTarget());
else if (node instanceof DocType)
return Collections12.singletonList(((DocType)node).getPublicID());
else
return null;
// With 2.1 semantics it makes more sense to just return a null and let the core
// throw an InvalidReferenceException and the template writer can use ?exists etcetera. (JR)
// throw new TemplateModelException("_name can not be applied on " + node.getClass());
}
}
private static final class QNameOp implements NodeOperator {
public List operate(Object node)
{
if (node instanceof Element)
return Collections12.singletonList(((Element)node).getQualifiedName());
else if (node instanceof Attribute)
return Collections12.singletonList(((Attribute)node).getQualifiedName());
// With 2.1 semantics it makes more sense to just return a null and let the core
// throw an InvalidReferenceException and the template writer can use ?exists etcetera. (JR)
return null;
// throw new TemplateModelException("_qname can not be applied on " + node.getClass());
}
}
private static final class NamespaceUriOp implements NodeOperator {
public List operate(Object node)
{
if (node instanceof Element)
return Collections12.singletonList(((Element)node).getNamespace().getURI());
else if (node instanceof Attribute)
return Collections12.singletonList(((Attribute)node).getNamespace().getURI());
// With 2.1 semantics it makes more sense to just return a null and let the core
// throw an InvalidReferenceException and the template writer can use ?exists etcetera. (JR)
return null;
// throw new TemplateModelException("_nsuri can not be applied on " + node.getClass());
}
}
private static final class NamespacePrefixOp implements NodeOperator {
public List operate(Object node)
{
if (node instanceof Element)
return Collections12.singletonList(((Element)node).getNamespace().getPrefix());
else if (node instanceof Attribute)
return Collections12.singletonList(((Attribute)node).getNamespace().getPrefix());
// With 2.1 semantics it makes more sense to just return a null and let the core
// throw an InvalidReferenceException and the template writer can use ?exists etcetera. (JR)
return null;
// throw new TemplateModelException("_nsprefix can not be applied on " + node.getClass());
}
}
private static final class CanonicalNameOp implements NodeOperator {
public List operate(Object node)
{
if (node instanceof Element)
{
Element element = (Element)node;
return Collections12.singletonList(element.getNamespace().getURI() + element.getName());
}
else if (node instanceof Attribute)
{
Attribute attribute = (Attribute)node;
return Collections12.singletonList(attribute.getNamespace().getURI() + attribute.getName());
}
// With 2.1 semantics it makes more sense to just return a null and let the core
// throw an InvalidReferenceException and the template writer can use ?exists etcetera. (JR)
return null;
// throw new TemplateModelException("_cname can not be applied on " + node.getClass());
}
}
private static final Element getParent(Object node)
{
if (node instanceof Element)
return((Element)node).getParent();
else if (node instanceof Attribute)
return((Attribute)node).getParent();
else if (node instanceof Text)
return((Text)node).getParent();
else if (node instanceof ProcessingInstruction)
return((ProcessingInstruction)node).getParent();
else if (node instanceof Comment)
return((Comment)node).getParent();
else if (node instanceof EntityRef)
return((EntityRef)node).getParent();
else
// With 2.1 semantics it makes more sense to just return a null and let the core
// throw an InvalidReferenceException and the template writer can use ?exists etcetera. (JR)
return null;
// throw new TemplateModelException("_parent can not be applied on " + node.getClass());
}
private static final class ParentOp implements NodeOperator {
public List operate(Object node)
{
Element parent = getParent(node);
return parent == null ? Collections.EMPTY_LIST : Collections12.singletonList(parent);
}
}
private static final class AncestorOp implements NodeOperator {
public List operate(Object node)
{
Element parent = getParent(node);
if (parent == null) return Collections.EMPTY_LIST;
LinkedList list = new LinkedList();
do {
list.addFirst(parent);
parent = parent.getParent();
}
while (parent != null);
return list;
}
}
private static final class AncestorOrSelfOp implements NodeOperator {
public List operate(Object node)
{
Element parent = getParent(node);
if (parent == null) return Collections12.singletonList(node);
LinkedList list = new LinkedList();
list.addFirst(node);
do {
list.addFirst(parent);
parent = parent.getParent();
}
while (parent != null);
return list;
}
}
private static class DescendantOp implements NodeOperator {
public List operate(Object node)
{
LinkedList list = new LinkedList();
if (node instanceof Element) {
addChildren((Element)node, list);
}
else if (node instanceof Document) {
Element root = ((Document)node).getRootElement();
list.add(root);
addChildren(root, list);
}
else
// With 2.1 semantics it makes more sense to just return a null and let the core
// throw an InvalidReferenceException and the template writer can use ?exists etcetera. (JR)
return null;
// throw new TemplateModelException("_descendant can not be applied on " + node.getClass());
return list;
}
private void addChildren(Element element, List list)
{
List children = element.getChildren();
Iterator it = children.iterator();
while (it.hasNext()) {
Element child = (Element)it.next();
list.add(child);
addChildren(child, list);
}
}
}
private static final class DescendantOrSelfOp extends DescendantOp {
public List operate(Object node)
{
LinkedList list = (LinkedList)super.operate(node);
list.addFirst(node);
return list;
}
}
private static final class DocumentOp implements NodeOperator {
public List operate(Object node)
{
Document doc = null;
if (node instanceof Element)
doc = ((Element)node).getDocument();
else if (node instanceof Attribute) {
Element parent = ((Attribute)node).getParent();
doc = parent == null ? null : parent.getDocument();
} else if (node instanceof Text) {
Element parent = ((Text)node).getParent();
doc = parent == null ? null : parent.getDocument();
} else if (node instanceof Document)
doc = (Document)node;
else if (node instanceof ProcessingInstruction)
doc = ((ProcessingInstruction)node).getDocument();
else if (node instanceof EntityRef)
doc = ((EntityRef)node).getDocument();
else if (node instanceof Comment)
doc = ((Comment)node).getDocument();
else
// With 2.1 semantics it makes more sense to just return a null and let the core
// throw an InvalidReferenceException and the template writer can use ?exists etcetera. (JR)
return null;
// throw new TemplateModelException("_document can not be applied on " + node.getClass());
return doc == null ? Collections.EMPTY_LIST : Collections12.singletonList(doc);
}
}
private static final class DocTypeOp implements NodeOperator {
public List operate(Object node)
{
if (node instanceof Document) {
DocType doctype = ((Document)node).getDocType();
return doctype == null ? Collections.EMPTY_LIST : Collections12.singletonList(doctype);
} else
// With 2.1 semantics it makes more sense to just return a null and let the core
// throw an InvalidReferenceException and the template writer can use ?exists etcetera. (JR)
return null;
// throw new TemplateModelException("_doctype can not be applied on " + node.getClass());
}
}
private static final class ContentOp implements NodeOperator {
public List operate(Object node)
{
if (node instanceof Element)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -