Exemplo n.º 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.");
 }
 @Override
 public Object exec(List arguments) throws TemplateModelException {
   if (arguments.size() != 2) {
     throw new TemplateModelException("getCharactersByGroup needs two arguments - page and group");
   }
   try {
     Page page = (Page) DeepUnwrap.unwrap((TemplateModel) arguments.get(0));
     TemplateScalarModel groupScalar = (TemplateScalarModel) arguments.get(1);
     return objectWrapper.wrap(getCharactersByGroup(page, groupScalar.getAsString()));
   } catch (ClassCastException e) {
     throw new TemplateModelException("Error: Expecting Page and String arguments");
   }
 }
Exemplo n.º 3
0
 static String getString(TemplateScalarModel model, Expression expr, Environment env)
     throws TemplateException {
   String value = model.getAsString();
   if (value == null) {
     if (env.isClassicCompatible()) {
       return "";
     } else {
       throw new TemplateException(expr + " evaluated to null string.", env);
     }
   }
   return value;
 }