/**
  * Constructor SAXSourceLocator
  *
  * @param locator Source locator
  */
 public SAXSourceLocator(Locator locator) {
   m_locator = locator;
   this.setColumnNumber(locator.getColumnNumber());
   this.setLineNumber(locator.getLineNumber());
   this.setPublicId(locator.getPublicId());
   this.setSystemId(locator.getSystemId());
 }
    private SAXLocation(Locator locator) {

      lineNumber = locator.getLineNumber();
      columnNumber = locator.getColumnNumber();
      publicId = locator.getPublicId();
      systemId = locator.getSystemId();
    }
 /**
  * Copy constructor.
  *
  * <p>Create a persistent copy of the current state of a locator.
  * When the original locator changes, this copy will still keep
  * the original values (and it can be used outside the scope of
  * DocumentHandler methods).</p>
  *
  * @param locator The locator to copy.
  */
 public SerializableLocatorImpl (org.xml.sax.Locator locator)
 {
     setPublicId(locator.getPublicId());
     setSystemId(locator.getSystemId());
     setLineNumber(locator.getLineNumber());
     setColumnNumber(locator.getColumnNumber());
 }
Exemple #4
0
  /**
   * Called by the SAX parser when an error occurs. Used by this class to report the current
   * position in the file.
   *
   * @param e The exception that occurred.
   * @throws SAXException if the error reporting throws an exception.
   */
  @Override
  public void error(SAXParseException e) throws SAXException {
    LOG.severe(
        "Unable to parse xml file.  publicId=("
            + documentLocator.getPublicId()
            + "), systemId=("
            + documentLocator.getSystemId()
            + "), lineNumber="
            + documentLocator.getLineNumber()
            + ", columnNumber="
            + documentLocator.getColumnNumber()
            + ".");

    super.error(e);
  }
Exemple #5
0
 public String getPublicId() {
   return locator.getPublicId();
 }
 /**
  * Return the public identifier for the current document event.
  *
  * <p>The return value is the public identifier of the document entity or of the external parsed
  * entity in which the markup triggering the event appears.
  *
  * @return A string containing the public identifier, or null if none is available.
  * @see #getSystemId
  */
 public String getPublicId() {
   return (null == m_locator) ? super.getPublicId() : m_locator.getPublicId();
 }
  /**
   * Receive notification of the start of an element.
   *
   * @param handler non-null reference to current StylesheetHandler that is constructing the
   *     Templates.
   * @param uri The Namespace URI, or an empty string.
   * @param localName The local name (without prefix), or empty string if not namespace processing.
   * @param rawName The qualified name (with prefix).
   * @param attributes The specified or defaulted attributes.
   */
  public void startElement(
      StylesheetHandler handler,
      String uri,
      String localName,
      String rawName,
      Attributes attributes)
      throws org.xml.sax.SAXException {

    try {
      ElemTemplateElement p = handler.getElemTemplateElement();
      boolean excludeXSLDecl = false;
      boolean isLREAsStyleSheet = false;

      if (null == p) {

        // Literal Result Template as stylesheet.
        XSLTElementProcessor lreProcessor = handler.popProcessor();
        XSLTElementProcessor stylesheetProcessor =
            handler.getProcessorFor(Constants.S_XSLNAMESPACEURL, "stylesheet", "xsl:stylesheet");

        handler.pushProcessor(lreProcessor);

        Stylesheet stylesheet;
        try {
          stylesheet = getStylesheetRoot(handler);
        } catch (TransformerConfigurationException tfe) {
          throw new TransformerException(tfe);
        }

        // stylesheet.setDOMBackPointer(handler.getOriginatingNode());
        // ***** Note that we're assigning an empty locator. Is this necessary?
        SAXSourceLocator slocator = new SAXSourceLocator();
        Locator locator = handler.getLocator();
        if (null != locator) {
          slocator.setLineNumber(locator.getLineNumber());
          slocator.setColumnNumber(locator.getColumnNumber());
          slocator.setPublicId(locator.getPublicId());
          slocator.setSystemId(locator.getSystemId());
        }
        stylesheet.setLocaterInfo(slocator);
        stylesheet.setPrefixes(handler.getNamespaceSupport());
        handler.pushStylesheet(stylesheet);

        isLREAsStyleSheet = true;

        AttributesImpl stylesheetAttrs = new AttributesImpl();
        AttributesImpl lreAttrs = new AttributesImpl();
        int n = attributes.getLength();

        for (int i = 0; i < n; i++) {
          String attrLocalName = attributes.getLocalName(i);
          String attrUri = attributes.getURI(i);
          String value = attributes.getValue(i);

          if ((null != attrUri) && attrUri.equals(Constants.S_XSLNAMESPACEURL)) {
            stylesheetAttrs.addAttribute(
                null, attrLocalName, attrLocalName, attributes.getType(i), attributes.getValue(i));
          } else if ((attrLocalName.startsWith("xmlns:") || attrLocalName.equals("xmlns"))
              && value.equals(Constants.S_XSLNAMESPACEURL)) {

            // ignore
          } else {
            lreAttrs.addAttribute(
                attrUri,
                attrLocalName,
                attributes.getQName(i),
                attributes.getType(i),
                attributes.getValue(i));
          }
        }

        attributes = lreAttrs;

        // Set properties from the attributes, but don't throw
        // an error if there is an attribute defined that is not
        // allowed on a stylesheet.
        try {
          stylesheetProcessor.setPropertiesFromAttributes(
              handler, "stylesheet", stylesheetAttrs, stylesheet);
        } catch (Exception e) {
          // This is pretty ugly, but it will have to do for now.
          // This is just trying to append some text specifying that
          // this error came from a missing or invalid XSLT namespace
          // declaration.
          // If someone comes up with a better solution, please feel
          // free to contribute it. -mm

          if (stylesheet.getDeclaredPrefixes() == null || !declaredXSLNS(stylesheet)) {
            throw new org.xml.sax.SAXException(
                XSLMessages.createWarning(XSLTErrorResources.WG_OLD_XSLT_NS, null));
          } else {
            throw new org.xml.sax.SAXException(e);
          }
        }
        handler.pushElemTemplateElement(stylesheet);

        ElemTemplate template = new ElemTemplate();
        if (slocator != null) template.setLocaterInfo(slocator);

        appendAndPush(handler, template);

        XPath rootMatch =
            new XPath(
                "/",
                stylesheet,
                stylesheet,
                XPath.MATCH,
                handler.getStylesheetProcessor().getErrorListener());

        template.setMatch(rootMatch);

        // template.setDOMBackPointer(handler.getOriginatingNode());
        stylesheet.setTemplate(template);

        p = handler.getElemTemplateElement();
        excludeXSLDecl = true;
      }

      XSLTElementDef def = getElemDef();
      Class classObject = def.getClassObject();
      boolean isExtension = false;
      boolean isComponentDecl = false;
      boolean isUnknownTopLevel = false;

      while (null != p) {

        // System.out.println("Checking: "+p);
        if (p instanceof ElemLiteralResult) {
          ElemLiteralResult parentElem = (ElemLiteralResult) p;

          isExtension = parentElem.containsExtensionElementURI(uri);
        } else if (p instanceof Stylesheet) {
          Stylesheet parentElem = (Stylesheet) p;

          isExtension = parentElem.containsExtensionElementURI(uri);

          if ((false == isExtension)
              && (null != uri)
              && (uri.equals(Constants.S_BUILTIN_EXTENSIONS_URL)
                  || uri.equals(Constants.S_BUILTIN_OLD_EXTENSIONS_URL))) {
            isComponentDecl = true;
          } else {
            isUnknownTopLevel = true;
          }
        }

        if (isExtension) break;

        p = p.getParentElem();
      }

      ElemTemplateElement elem = null;

      try {
        if (isExtension) {

          // System.out.println("Creating extension(1): "+uri);
          elem = new ElemExtensionCall();
        } else if (isComponentDecl) {
          elem = (ElemTemplateElement) classObject.newInstance();
        } else if (isUnknownTopLevel) {

          // TBD: Investigate, not sure about this.  -sb
          elem = (ElemTemplateElement) classObject.newInstance();
        } else {
          elem = (ElemTemplateElement) classObject.newInstance();
        }

        elem.setDOMBackPointer(handler.getOriginatingNode());
        elem.setLocaterInfo(handler.getLocator());
        elem.setPrefixes(handler.getNamespaceSupport(), excludeXSLDecl);

        if (elem instanceof ElemLiteralResult) {
          ((ElemLiteralResult) elem).setNamespace(uri);
          ((ElemLiteralResult) elem).setLocalName(localName);
          ((ElemLiteralResult) elem).setRawName(rawName);
          ((ElemLiteralResult) elem).setIsLiteralResultAsStylesheet(isLREAsStyleSheet);
        }
      } catch (InstantiationException ie) {
        handler.error(
            XSLTErrorResources.ER_FAILED_CREATING_ELEMLITRSLT,
            null,
            ie); // "Failed creating ElemLiteralResult instance!", ie);
      } catch (IllegalAccessException iae) {
        handler.error(
            XSLTErrorResources.ER_FAILED_CREATING_ELEMLITRSLT,
            null,
            iae); // "Failed creating ElemLiteralResult instance!", iae);
      }

      setPropertiesFromAttributes(handler, rawName, attributes, elem);

      // bit of a hack here...
      if (!isExtension && (elem instanceof ElemLiteralResult)) {
        isExtension = ((ElemLiteralResult) elem).containsExtensionElementURI(uri);

        if (isExtension) {

          // System.out.println("Creating extension(2): "+uri);
          elem = new ElemExtensionCall();

          elem.setLocaterInfo(handler.getLocator());
          elem.setPrefixes(handler.getNamespaceSupport());
          ((ElemLiteralResult) elem).setNamespace(uri);
          ((ElemLiteralResult) elem).setLocalName(localName);
          ((ElemLiteralResult) elem).setRawName(rawName);
          setPropertiesFromAttributes(handler, rawName, attributes, elem);
        }
      }

      appendAndPush(handler, elem);
    } catch (TransformerException te) {
      throw new org.xml.sax.SAXException(te);
    }
  }
  /**
   * Start processing given node
   *
   * @param node Node to process
   * @throws org.xml.sax.SAXException
   */
  protected void startNode(Node node) throws org.xml.sax.SAXException {

    //   TODO: <REVIEW>
    //    A Serializer implements ContentHandler, but not NodeConsumer
    //    so drop this reference to NodeConsumer which would otherwise
    //    pull in all sorts of things
    //    if (m_contentHandler instanceof NodeConsumer)
    //    {
    //      ((NodeConsumer) m_contentHandler).setOriginatingNode(node);
    //    }
    //    TODO: </REVIEW>

    if (node instanceof Locator) {
      Locator loc = (Locator) node;
      m_locator.setColumnNumber(loc.getColumnNumber());
      m_locator.setLineNumber(loc.getLineNumber());
      m_locator.setPublicId(loc.getPublicId());
      m_locator.setSystemId(loc.getSystemId());
    } else {
      m_locator.setColumnNumber(0);
      m_locator.setLineNumber(0);
    }

    switch (node.getNodeType()) {
      case Node.COMMENT_NODE:
        {
          String data = ((Comment) node).getData();

          if (m_contentHandler instanceof LexicalHandler) {
            LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);

            lh.comment(data.toCharArray(), 0, data.length());
          }
        }
        break;
      case Node.DOCUMENT_FRAGMENT_NODE:

        // ??;
        break;
      case Node.DOCUMENT_NODE:
        break;
      case Node.ELEMENT_NODE:
        Element elem_node = (Element) node;
        {
          // Make sure the namespace node
          // for the element itself is declared
          // to the ContentHandler
          String uri = elem_node.getNamespaceURI();
          if (uri != null) {
            String prefix = elem_node.getPrefix();
            if (prefix == null) prefix = "";
            this.m_contentHandler.startPrefixMapping(prefix, uri);
          }
        }
        NamedNodeMap atts = elem_node.getAttributes();
        int nAttrs = atts.getLength();
        // System.out.println("TreeWalker#startNode: "+node.getNodeName());

        // Make sure the namespace node of
        // each attribute is declared to the ContentHandler
        for (int i = 0; i < nAttrs; i++) {
          final Node attr = atts.item(i);
          final String attrName = attr.getNodeName();
          final int colon = attrName.indexOf(':');
          final String prefix;

          // System.out.println("TreeWalker#startNode: attr["+i+"] = "+attrName+",
          // "+attr.getNodeValue());
          if (attrName.equals("xmlns") || attrName.startsWith("xmlns:")) {
            // Use "" instead of null, as Xerces likes "" for the
            // name of the default namespace.  Fix attributed
            // to "Steven Murray" <*****@*****.**>.
            if (colon < 0) prefix = "";
            else prefix = attrName.substring(colon + 1);

            this.m_contentHandler.startPrefixMapping(prefix, attr.getNodeValue());
          } else if (colon > 0) {
            prefix = attrName.substring(0, colon);
            String uri = attr.getNamespaceURI();
            if (uri != null) this.m_contentHandler.startPrefixMapping(prefix, uri);
          }
        }

        String ns = m_dh.getNamespaceOfNode(node);
        if (null == ns) ns = "";
        this.m_contentHandler.startElement(
            ns, m_dh.getLocalNameOfNode(node), node.getNodeName(), new AttList(atts, m_dh));
        break;
      case Node.PROCESSING_INSTRUCTION_NODE:
        {
          ProcessingInstruction pi = (ProcessingInstruction) node;
          String name = pi.getNodeName();

          // String data = pi.getData();
          if (name.equals("xslt-next-is-raw")) {
            nextIsRaw = true;
          } else {
            this.m_contentHandler.processingInstruction(pi.getNodeName(), pi.getData());
          }
        }
        break;
      case Node.CDATA_SECTION_NODE:
        {
          boolean isLexH = (m_contentHandler instanceof LexicalHandler);
          LexicalHandler lh = isLexH ? ((LexicalHandler) this.m_contentHandler) : null;

          if (isLexH) {
            lh.startCDATA();
          }

          dispatachChars(node);

          {
            if (isLexH) {
              lh.endCDATA();
            }
          }
        }
        break;
      case Node.TEXT_NODE:
        {
          // String data = ((Text) node).getData();

          if (nextIsRaw) {
            nextIsRaw = false;

            m_contentHandler.processingInstruction(
                javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, "");
            dispatachChars(node);
            m_contentHandler.processingInstruction(
                javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, "");
          } else {
            dispatachChars(node);
          }
        }
        break;
      case Node.ENTITY_REFERENCE_NODE:
        {
          EntityReference eref = (EntityReference) node;

          if (m_contentHandler instanceof LexicalHandler) {
            ((LexicalHandler) this.m_contentHandler).startEntity(eref.getNodeName());
          } else {

            // warning("Can not output entity to a pure SAX ContentHandler");
          }
        }
        break;
      default:
    }
  }