예제 #1
0
 public TemplateModel get(String key) throws TemplateModelException {
   if (size() == 1) {
     NodeModel nm = (NodeModel) get(0);
     return nm.get(key);
   }
   if (key.startsWith("@@")
       && (key.equals("@@markup") || key.equals("@@nested_markup") || key.equals("@@text"))) {
     StringBuffer result = new StringBuffer();
     for (int i = 0; i < size(); i++) {
       NodeModel nm = (NodeModel) get(i);
       TemplateScalarModel textModel = (TemplateScalarModel) nm.get(key);
       result.append(textModel.getAsString());
     }
     return new SimpleScalar(result.toString());
   }
   if (StringUtil.isXMLID(key)
       || ((key.startsWith("@") && StringUtil.isXMLID(key.substring(1))))
       || key.equals("*")
       || key.equals("**")
       || key.equals("@@")
       || key.equals("@*")) {
     NodeListModel result = new NodeListModel(contextNode);
     for (int i = 0; i < size(); i++) {
       NodeModel nm = (NodeModel) get(i);
       if (nm instanceof ElementModel) {
         TemplateSequenceModel tsm = (TemplateSequenceModel) ((ElementModel) nm).get(key);
         if (tsm != null) {
           int size = tsm.size();
           for (int j = 0; j < size; j++) {
             result.add(tsm.get(j));
           }
         }
       }
     }
     if (result.size() == 1) {
       return result.get(0);
     }
     return result;
   }
   XPathSupport xps = getXPathSupport();
   if (xps != null) {
     Object context = (size() == 0) ? null : rawNodeList();
     return xps.executeQuery(context, key);
   }
   throw new TemplateModelException(
       "Key: '"
           + key
           + "' is not legal for a node sequence ("
           + this.getClass().getName()
           + "). This node sequence contains "
           + size()
           + " node(s). "
           + "Some keys are valid only for node sequences of size 1. "
           + "If you use Xalan (instead of Jaxen), XPath expression keys work only with "
           + "node lists of size 1.");
 }
예제 #2
0
 public TemplateModel exec(List args) throws TemplateModelException {
   if (args.size() != 1) {
     throw new TemplateModelException("Expecting exactly one arguments");
   }
   String query = (String) args.get(0);
   // Now, we try to behave as if this is an XPath expression
   XPathSupport xps = getXPathSupport();
   if (xps == null) {
     throw new TemplateModelException("No XPath support available");
   }
   return xps.executeQuery(node, query);
 }
예제 #3
0
 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")) {
       StringBuilder buf = new StringBuilder();
       NodeOutputter nu = new NodeOutputter(node);
       nu.outputContent(node, buf);
       return new SimpleScalar(buf.toString());
     }
     if (key.equals("@@nested_markup")) {
       StringBuilder buf = new StringBuilder();
       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);
   }
 }