Beispiel #1
0
 @Override
 @SuppressWarnings({"unchecked"})
 public List<?> translate(Object result, NamespaceContext nsContext) {
   List nodeList = (List) result;
   int i = 0;
   for (Object item : nodeList) {
     if (item instanceof List) nodeList.set(i, translate(item, nsContext));
     else {
       NodeInfo node = (NodeInfo) item;
       int type = node.getNodeKind();
       String value = "";
       if (type != NodeType.DOCUMENT && type != NodeType.ELEMENT) value = node.getStringValue();
       String localName = node.getLocalPart();
       String namespaceURI = node.getURI();
       String qualifiedName = node.getDisplayName();
       String location = SaxonNavigator.INSTANCE.getXPath(node, nsContext);
       NodeItem nodeItem =
           new NodeItem(type, location, value, localName, namespaceURI, qualifiedName);
       nodeItem.xml = node;
       nodeList.set(i, nodeItem);
     }
     i++;
   }
   return nodeList;
 }
 /**
  * Set all the declared namespaces to be the namespaces that are in-scope for a given node. In
  * addition, the standard namespaces (xml, xslt, saxon) are declared.
  *
  * @param node The node whose in-scope namespaces are to be used as the context namespaces. Note
  *     that this will have no effect unless this node is an element.
  */
 public void setNamespaces(NodeInfo node) {
   namespaces.clear();
   AxisIterator iter = node.iterateAxis(Axis.NAMESPACE);
   while (true) {
     NodeInfo ns = (NodeInfo) iter.next();
     if (ns == null) {
       return;
     }
     declareNamespace(ns.getLocalPart(), ns.getStringValue());
   }
 }
Beispiel #3
0
  public static void assertXPathEquals(
      String xpathString, Document doc, boolean ignoreOrder, Object... expectedValues) {
    try {
      XPathEvaluator xpathEvaluator = new XPathEvaluator();
      XPathExpression expr = xpathEvaluator.createExpression(xpathString);

      final JDOMSource docAsDomSource = new JDOMSource(doc);

      List nodes = expr.evaluate(docAsDomSource);

      if (nodes.size() != expectedValues.length) {
        org.junit.Assert.fail(
            "expected " + expectedValues.length + " values at xpath: " + xpathString);
      }

      String[] actualValues = new String[nodes.size()];
      for (int i = 0; i < expectedValues.length; i++) {
        Object node = nodes.get(i);
        if (node instanceof NodeInfo) {
          NodeInfo nodeInfo = (NodeInfo) node;
          actualValues[i] = nodeInfo.getStringValue();
        } else {
          actualValues[i] = String.valueOf(node);
        }
      }

      String[] expectedValuesAsString = new String[expectedValues.length];
      for (int i = 0; i < expectedValues.length; i++) {
        expectedValuesAsString[i] = String.valueOf(expectedValues[i]);
      }

      if (ignoreOrder) {
        Arrays.sort(actualValues);
        Arrays.sort(expectedValuesAsString);
      }

      org.junit.Assert.assertArrayEquals(expectedValuesAsString, actualValues);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  public Expression compile(Executable exec) throws XPathException {
    NamespaceResolver nsContext = null;

    int annotation = getTypeAnnotation(schemaType);

    // deal specially with the case where the attribute name is known statically

    if (attributeName instanceof StringLiteral) {
      String qName = Whitespace.trim(((StringLiteral) attributeName).getStringValue());
      String[] parts;
      try {
        parts = getConfiguration().getNameChecker().getQNameParts(qName);
      } catch (QNameException e) {
        // This can't happen, because of previous checks
        return null;
      }

      if (namespace == null) {
        String nsuri = "";
        if (!parts[0].equals("")) {
          nsuri = getURIForPrefix(parts[0], false);
          if (nsuri == null) {
            undeclaredNamespaceError(parts[0], "XTSE0280");
            return null;
          }
        }
        int nameCode = getNamePool().allocate(parts[0], nsuri, parts[1]);
        FixedAttribute inst =
            new FixedAttribute(nameCode, validationAction, schemaType, annotation);
        inst.setContainer(this); // temporarily
        compileContent(exec, inst, separator);
        return inst;
      } else if (namespace instanceof StringLiteral) {
        String nsuri = ((StringLiteral) namespace).getStringValue();
        if (nsuri.equals("")) {
          parts[0] = "";
        } else if (parts[0].equals("")) {
          // Need to choose an arbitrary prefix
          // First see if the requested namespace is declared in the stylesheet
          AxisIterator iter = iterateAxis(Axis.NAMESPACE);
          while (true) {
            NodeInfo ns = (NodeInfo) iter.next();
            if (ns == null) {
              break;
            }
            if (ns.getStringValue().equals(nsuri)) {
              parts[0] = ns.getLocalPart();
              break;
            }
          }
          // Otherwise see the URI is known to the namepool
          if (parts[0].equals("")) {
            String p =
                getNamePool().suggestPrefixForURI(((StringLiteral) namespace).getStringValue());
            if (p != null) {
              parts[0] = p;
            }
          }
          // Otherwise choose something arbitrary. This will get changed
          // if it clashes with another attribute
          if (parts[0].equals("")) {
            parts[0] = "ns0";
          }
        }
        int nameCode = getNamePool().allocate(parts[0], nsuri, parts[1]);
        FixedAttribute inst =
            new FixedAttribute(nameCode, validationAction, schemaType, annotation);
        compileContent(exec, inst, separator);
        return inst;
      }
    } else {
      // if the namespace URI must be deduced at run-time from the attribute name
      // prefix, we need to save the namespace context of the instruction

      if (namespace == null) {
        nsContext = makeNamespaceContext();
      }
    }

    ComputedAttribute inst =
        new ComputedAttribute(
            attributeName, namespace, nsContext, validationAction, schemaType, annotation, false);
    compileContent(exec, inst, separator);
    return inst;
  }
Beispiel #5
0
  private boolean shallowEquals(NodeInfo n1, Node n2) {
    if (n1 == n2) return true;
    if (n1 == null || n2 == null) return false;

    int type1 = n1.getNodeKind();
    if (type1 == Node.CDATA_SECTION_NODE) type1 = Node.TEXT_NODE;
    else if (type1 == NodeType.NAMESPACE) type1 = Node.ATTRIBUTE_NODE;

    int type2 = n2.getNodeType();
    if (type2 == Node.CDATA_SECTION_NODE) type2 = Node.TEXT_NODE;

    if (type1 != type2) return false;

    switch (type1) {
      case Node.PROCESSING_INSTRUCTION_NODE:
        ProcessingInstruction pi2 = (ProcessingInstruction) n2;
        String target1 = n1.getDisplayName();
        String target2 = pi2.getTarget();
        if (!target1.equals(target2)) return false;
        String data1 = n1.getStringValue();
        String data2 = pi2.getData();
        if (!data1.equals(data2)) return false;
        break;
      case Node.COMMENT_NODE:
        Comment comment2 = (Comment) n2;
        data1 = n1.getStringValue();
        data2 = comment2.getData();
        if (!data1.equals(data2)) return false;
        break;
      case Node.ELEMENT_NODE:
        Element element2 = (Element) n2;
        String namespaceURI1 = n1.getURI();
        if (namespaceURI1 == null) namespaceURI1 = "";
        String namespaceURI2 = element2.getNamespaceURI();
        if (namespaceURI2 == null) namespaceURI2 = "";
        if (!namespaceURI1.equals(namespaceURI2)) return false;
        String localName1 = n1.getLocalPart();
        String localName2 = element2.getLocalName();
        if (!localName1.equals(localName2)) return false;

        NodeInfoSequence attrs1 = new NodeInfoSequence(n1, Axis.ATTRIBUTE);
        NamedNodeMap attrs2 = element2.getAttributes();
        BitSet bitSet = new BitSet();
        NodeInfo attr1;
        while ((attr1 = attrs1.findNext()) != null) {
          if (isNamespaceDeclaration(attr1)) continue;
          namespaceURI1 = attr1.getURI();
          if (namespaceURI1 == null) namespaceURI1 = "";
          localName1 = attr1.getLocalPart();
          String value1 = attr1.getStringValue();

          int found = -1;
          for (int i = 0; i < attrs2.getLength(); i++) {
            Attr attr2 = (Attr) attrs2.item(i);
            namespaceURI2 = attr2.getNamespaceURI();
            if (namespaceURI2 == null) namespaceURI2 = "";
            localName2 = attr2.getLocalName();
            if (namespaceURI1.equals(namespaceURI2) && localName1.equals(localName2)) {
              String value2 = attr2.getNodeValue();
              if (!value1.equals(value2)) return false;
              found = i;
              break;
            }
          }
          if (found == -1) return false;
          else bitSet.set(found);
        }
        for (int i = 0; i < attrs2.getLength(); i++) {
          if (!bitSet.get(i)) {
            Attr attr2 = (Attr) attrs2.item(i);
            if (!DOMUtil.isNamespaceDeclaration(attr2)) return false;
          }
        }

        break;
      case Node.ATTRIBUTE_NODE:
        Attr attr2 = (Attr) n2;
        namespaceURI1 = isNamespaceDeclaration(n1) ? Namespaces.URI_XMLNS : n1.getURI();
        if (namespaceURI1 == null) namespaceURI1 = "";
        namespaceURI2 = attr2.getNamespaceURI();
        if (namespaceURI2 == null) namespaceURI2 = "";
        if (!namespaceURI1.equals(namespaceURI2)) return false;
        localName1 = n1.getLocalPart();
        localName2 = attr2.getLocalName();
        if (!localName1.equals(localName2)) return false;
        String value1 = n1.getStringValue();
        String value2 = attr2.getNodeValue();
        if (!value1.equals(value2)) return false;
        break;
      case Node.TEXT_NODE:
        value1 = n1.getStringValue();
        value2 = n2.getNodeValue();
        if (!value1.equals(value2)) return false;
    }

    return true;
  }