예제 #1
0
  public void validate() throws XPathException {
    stackFrameMap = getConfiguration().makeSlotManager();
    checkTopLevel(null);

    // the check for duplicates is now done in the buildIndexes() method of XSLStylesheet
    if (match != null) {
      match = typeCheck("match", match);
      if (match.getNodeTest() instanceof EmptySequenceTest) {
        try {
          getConfiguration()
              .getErrorListener()
              .warning(new TransformerException("Match pattern cannot match any nodes", this));
        } catch (TransformerException e) {
          compileError(XPathException.makeXPathException(e));
        }
      }
    }

    // See if there are any required parameters.
    AxisIterator kids = iterateAxis(Axis.CHILD);
    while (true) {
      NodeInfo param = (NodeInfo) kids.next();
      if (param == null) {
        break;
      }
      if (param instanceof XSLParam && ((XSLParam) param).isRequiredParam()) {
        hasRequiredParams = true;
        break;
      }
    }
  }
 /**
  * Get n'th attribute (DOM NamedNodeMap method). In this implementation we number the attributes
  * as follows: 0 - the xmlns:xml namespace declaration 1-n further namespace declarations n+1...
  * "real" attribute declarations
  */
 public Node item(int index) {
   if (index < 0) {
     return null;
   }
   if (index == 0) {
     NamespaceIterator.NamespaceNodeImpl nn =
         new NamespaceIterator.NamespaceNodeImpl(parent, NamespaceConstant.XML_NAMESPACE_CODE, 0);
     return NodeOverNodeInfo.wrap(nn);
   }
   int nscount = getNumberOfNamespaces();
   if (index < nscount) {
     int[] buffer = new int[8];
     int[] nsList = parent.getDeclaredNamespaces(buffer);
     int nscode = nsList[index - 1];
     NamespaceIterator.NamespaceNodeImpl nn =
         new NamespaceIterator.NamespaceNodeImpl(parent, nscode, index);
     return NodeOverNodeInfo.wrap(nn);
   }
   int pos = 0;
   int attNr = (index - nscount);
   AxisIterator atts = parent.iterateAxis(Axis.ATTRIBUTE);
   while (true) {
     NodeInfo att = (NodeInfo) atts.next();
     if (att == null) {
       return null;
     }
     if (pos == attNr) {
       return NodeOverNodeInfo.wrap(att);
     }
     pos++;
   }
 }
예제 #3
0
  private static int findNodeIndex(NodeInfo node) {

    if (node.getParent() == null) return 0;

    final AxisIterator it = node.iterateAxis(Axis.PRECEDING_SIBLING);
    int result = 0;

    Item i = it.next();

    while (i != null) {
      result++;
      i = it.next();
    }

    return result;
  }
 /** Get number of attributes and namespaces (DOM NamedNodeMap method). */
 public int getLength() {
   int length = 0;
   AxisIterator atts = parent.iterateAxis(Axis.ATTRIBUTE);
   while (atts.next() != null) {
     length++;
   }
   return getNumberOfNamespaces() + length;
 }
예제 #5
0
 /**
  * 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());
   }
 }
예제 #6
0
 /**
  * Get the next item in the sequence. <br>
  *
  * @return the next Item. If there are no more nodes, return null.
  */
 public Item next() {
   if (position == 0) {
     position = 1;
     return start;
   }
   Item n = base.next();
   if (n == null) {
     position = -1;
   } else {
     position++;
   }
   return n;
 }
 /** Get named attribute (DOM NamedNodeMap method) */
 public Node getNamedItem(String name) {
   if (name.equals("xmlns")) {
     int[] nsarray = parent.getDeclaredNamespaces(null);
     for (int i = 0; i < nsarray.length; i++) {
       if (nsarray[i] == -1) {
         return null;
       } else if (((nsarray[i] >> 16) & 0xffff) == 0) {
         NamespaceIterator.NamespaceNodeImpl nn =
             new NamespaceIterator.NamespaceNodeImpl(parent, nsarray[i], i + 1);
         return NodeOverNodeInfo.wrap(nn);
       }
     }
     return null;
   } else if (name.startsWith("xmlns:")) {
     String prefix = name.substring(6);
     if (prefix.equals("xml")) {
       NamespaceIterator.NamespaceNodeImpl nn =
           new NamespaceIterator.NamespaceNodeImpl(parent, NamespaceConstant.XML_CODE, 0);
       return NodeOverNodeInfo.wrap(nn);
     }
     int[] buffer = new int[8];
     int[] nsarray = parent.getDeclaredNamespaces(buffer);
     for (int i = 0; i < nsarray.length; i++) {
       if (nsarray[i] == -1) {
         return null;
       } else if (prefix.equals(parent.getNamePool().getPrefixFromNamespaceCode(nsarray[i]))) {
         NamespaceIterator.NamespaceNodeImpl nn =
             new NamespaceIterator.NamespaceNodeImpl(parent, nsarray[i], i + 1);
         return NodeOverNodeInfo.wrap(nn);
       }
     }
     return null;
   } else {
     AxisIterator atts = parent.iterateAxis(Axis.ATTRIBUTE);
     while (true) {
       NodeInfo att = (NodeInfo) atts.next();
       if (att == null) {
         return null;
       }
       if (name.equals(att.getDisplayName())) {
         return NodeOverNodeInfo.wrap(att);
       }
     }
   }
 }
예제 #8
0
  /**
   * Get simple node number. This is defined as one plus the number of previous siblings of the same
   * node type and name. It is not accessible directly in XSL. This version doesn't require the
   * controller, and therefore doesn't remember previous results. It is used only by getPath().
   *
   * @param node the node whose number is required
   * @return the node number, as defined above
   */
  private static int getNumberSimple(NodeInfo node) {

    int fingerprint = node.getFingerprint();
    NodeTest same;

    if (fingerprint == -1) {
      same = NodeKindTest.makeNodeKindTest(node.getNodeKind());
    } else {
      same = new NameTest(node);
    }

    AxisIterator preceding = node.iterateAxis(Axis.PRECEDING_SIBLING, same);

    int i = 1;
    while (preceding.next() != null) {
      i++;
    }

    return i;
  }
 /** Get named attribute (DOM NamedNodeMap method) */
 public Node getNamedItemNS(String uri, String localName) {
   if (uri == null) {
     uri = "";
   }
   if (NamespaceConstant.XMLNS.equals(uri)) {
     return getNamedItem("xmlns:" + localName);
   }
   if (uri.equals("") && localName.equals("xmlns")) {
     return getNamedItem("xmlns");
   }
   AxisIterator atts = parent.iterateAxis(Axis.ATTRIBUTE);
   while (true) {
     NodeInfo att = (NodeInfo) atts.next();
     if (att == null) {
       return null;
     }
     if (uri.equals(att.getURI()) && localName.equals(att.getLocalPart())) {
       return NodeOverNodeInfo.wrap(att);
     }
   }
 }
예제 #10
0
    public int getLastPosition() {

      // To find out how many nodes there are in the axis, we
      // make a copy of the original node enumeration, and run through
      // the whole thing again, counting how many nodes match the filter.

      if (last >= 0) {
        return last;
      }
      last = 0;
      AxisIterator b = (AxisIterator) base.getAnother();
      while (true) {
        NodeInfo n = (NodeInfo) b.next();
        if (n == null) {
          return last;
        }
        if (nodeTest.matches(n.getNodeKind(), n.getFingerprint(), n.getTypeAnnotation())) {
          last++;
        }
      }
    }
예제 #11
0
  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;
  }
예제 #12
0
  /**
   * Generic (model-independent) implementation of deep copy algorithm for nodes. This is available
   * for use by any node implementations that choose to use it.
   *
   * @param node The node to be copied
   * @param out The receiver to which events will be sent
   * @param namePool Namepool holding the name codes (used only to resolve namespace codes)
   * @param whichNamespaces Indicates which namespace nodes for an element should be copied
   * @param copyAnnotations Indicates whether type annotations should be copied
   * @throws TransformerException on any failure reported by the Receiver
   */
  public static void copy(
      NodeInfo node, Receiver out, NamePool namePool, int whichNamespaces, boolean copyAnnotations)
      throws TransformerException {

    switch (node.getNodeKind()) {
      case Type.DOCUMENT:
        AxisIterator children0 = node.iterateAxis(Axis.CHILD, new AnyNodeTest());
        while (true) {
          NodeInfo child = (NodeInfo) children0.next();
          if (child == null) {
            return;
          }
          child.copy(out, whichNamespaces, copyAnnotations);
        }

      case Type.ELEMENT:
        out.startElement(node.getNameCode(), 0, 0);

        // output the namespaces

        if (whichNamespaces != NodeInfo.NO_NAMESPACES) {
          node.outputNamespaceNodes(out, true);
        }

        // output the attributes

        AxisIterator attributes = node.iterateAxis(Axis.ATTRIBUTE, new AnyNodeTest());
        while (true) {
          NodeInfo att = (NodeInfo) attributes.next();
          if (att == null) {
            break;
          }
          att.copy(out, whichNamespaces, copyAnnotations);
        }

        // output the children

        AxisIterator children = node.iterateAxis(Axis.CHILD, new AnyNodeTest());
        while (true) {
          NodeInfo child = (NodeInfo) children.next();
          if (child == null) {
            break;
          }
          child.copy(out, whichNamespaces, copyAnnotations);
        }

        // finally the end tag

        out.endElement();
        return;

      case Type.ATTRIBUTE:
        out.attribute(node.getNameCode(), 0, node.getStringValue(), 0);
        return;

      case Type.TEXT:
        out.characters(node.getStringValue(), 0);
        return;

      case Type.COMMENT:
        out.comment(node.getStringValue(), 0);
        return;

      case Type.PROCESSING_INSTRUCTION:
        out.processingInstruction(node.getLocalPart(), node.getStringValue(), 0);
        return;

      case Type.NAMESPACE:
        out.namespace(
            namePool.allocateNamespaceCode(node.getLocalPart(), node.getStringValue()), 0);
        return;

      default:
    }
  }