예제 #1
0
  /**
   * Constructor.
   *
   * @param contentHandler The implemention of the contentHandler operation (toXMLString, digest,
   *     ...)
   */
  public TreeWalker(ContentHandler contentHandler, String systemId) {
    // Set the content handler
    m_contentHandler = contentHandler;
    if (m_contentHandler instanceof SerializationHandler) {
      m_Serializer = (SerializationHandler) m_contentHandler;
    } else m_Serializer = null;

    // Set the system ID, if it is given
    m_contentHandler.setDocumentLocator(m_locator);
    if (systemId != null) m_locator.setSystemId(systemId);
    else {
      try {
        // Bug see Bugzilla  26741
        m_locator.setSystemId(
            SecuritySupport.getSystemProperty("user.dir") + File.separator + "dummy.xsl");
      } catch (SecurityException se) { // user.dir not accessible from applet
      }
    }

    // Set the document locator
    if (m_contentHandler != null) m_contentHandler.setDocumentLocator(m_locator);
    try {
      // Bug see Bugzilla  26741
      m_locator.setSystemId(
          SecuritySupport.getSystemProperty("user.dir") + File.separator + "dummy.xsl");
    } catch (SecurityException se) { // user.dir not accessible from applet

    }
    m_dh = new DOM2Helper();
  }
예제 #2
0
  private void parseAuth() {
    errReceiver.info(
        new SAXParseException(WscompileMessages.WSIMPORT_READING_AUTH_FILE(authFile), null));

    BufferedReader in;
    try {
      in = new BufferedReader(new InputStreamReader(new FileInputStream(authFile), "UTF-8"));
    } catch (UnsupportedEncodingException e) {
      error(new SAXParseException(e.getMessage(), null));
      return;
    } catch (FileNotFoundException e) {
      error(
          new SAXParseException(
              WscompileMessages.WSIMPORT_AUTH_FILE_NOT_FOUND(authFile, defaultAuthfile), null, e));
      return;
    }
    String text;
    LocatorImpl locator = new LocatorImpl();
    try {
      int lineno = 1;

      locator.setSystemId(authFile.getCanonicalPath());

      while ((text = in.readLine()) != null) {
        locator.setLineNumber(lineno++);
        try {
          URL url = new URL(text);
          String authinfo = url.getUserInfo();

          if (authinfo != null) {
            int i = authinfo.indexOf(':');

            if (i >= 0) {
              String user = authinfo.substring(0, i);
              String password = authinfo.substring(i + 1);
              authInfo.add(new AuthInfo(new URL(text), user, password));
            } else {
              error(
                  new SAXParseException(
                      WscompileMessages.WSIMPORT_ILLEGAL_AUTH_INFO(url), locator));
            }
          } else {
            error(
                new SAXParseException(WscompileMessages.WSIMPORT_ILLEGAL_AUTH_INFO(url), locator));
          }

        } catch (NumberFormatException e) {
          error(new SAXParseException(WscompileMessages.WSIMPORT_ILLEGAL_AUTH_INFO(text), locator));
        }
      }
      in.close();
    } catch (IOException e) {
      error(
          new SAXParseException(
              WscompileMessages.WSIMPORT_FAILED_TO_PARSE(authFile, e.getMessage()), locator));
    }
  }
 /*
  * 1 covered goal:
  * 1 nu.staldal.lagoon.producer.LSSIHandler.setDocumentLocator(Lorg/xml/sax/Locator;)V: root-Branch
  */
 @Test
 public void test6() throws Throwable {
   XMLFilterImpl xMLFilterImpl0 = new XMLFilterImpl();
   TreeSet<Object> treeSet0 = new TreeSet<Object>();
   LSSIHandler lSSIHandler0 =
       new LSSIHandler(
           (SourceManager) null, (ContentHandler) xMLFilterImpl0, (Target) null, (Set) treeSet0);
   LocatorImpl locatorImpl0 = new LocatorImpl();
   lSSIHandler0.setDocumentLocator((Locator) locatorImpl0);
   assertNull(locatorImpl0.getPublicId());
 }
예제 #4
0
 public void startDocument() throws SAXException {
   if (locator == null) {
     LocatorImpl tem = new LocatorImpl();
     tem.setLineNumber(-1);
     tem.setColumnNumber(-1);
     locator = tem;
   }
   openElements.push(new OpenElement("", "#root"));
   try {
     constraint.activate(this);
   } catch (WrappedSAXException e) {
     throw e.exception;
   }
 }
  /**
   * parse
   *
   * @param inputSource
   * @exception org.xml.sax.SAXException
   * @exception java.io.IOException
   */
  public void parse(InputSource inputSource) throws SAXException, IOException {

    // parse document
    try {
      XMLInputSource xmlInputSource =
          new XMLInputSource(inputSource.getPublicId(), inputSource.getSystemId(), null);
      xmlInputSource.setByteStream(inputSource.getByteStream());
      xmlInputSource.setCharacterStream(inputSource.getCharacterStream());
      xmlInputSource.setEncoding(inputSource.getEncoding());
      parse(xmlInputSource);
    }

    // wrap XNI exceptions as SAX exceptions
    catch (XMLParseException e) {
      Exception ex = e.getException();
      if (ex == null) {
        // must be a parser exception; mine it for locator info and throw
        // a SAXParseException
        LocatorImpl locatorImpl = new LocatorImpl();
        locatorImpl.setPublicId(e.getPublicId());
        locatorImpl.setSystemId(e.getExpandedSystemId());
        locatorImpl.setLineNumber(e.getLineNumber());
        locatorImpl.setColumnNumber(e.getColumnNumber());
        throw new SAXParseException(e.getMessage(), locatorImpl);
      }
      if (ex instanceof SAXException) {
        // why did we create an XMLParseException?
        throw (SAXException) ex;
      }
      if (ex instanceof IOException) {
        throw (IOException) ex;
      }
      throw new SAXException(ex);
    } catch (XNIException e) {
      Exception ex = e.getException();
      if (ex == null) {
        throw new SAXException(e.getMessage());
      }
      if (ex instanceof SAXException) {
        throw (SAXException) ex;
      }
      if (ex instanceof IOException) {
        throw (IOException) ex;
      }
      throw new SAXException(ex);
    }
  } // parse(InputSource)
  /**
   * Parses the input source specified by the given system identifier.
   *
   * <p>This method is equivalent to the following:
   *
   * <pre>
   *     parse(new InputSource(systemId));
   * </pre>
   *
   * @param systemId The system identifier (URI).
   * @exception org.xml.sax.SAXException Throws exception on SAX error.
   * @exception java.io.IOException Throws exception on i/o error.
   */
  public void parse(String systemId) throws SAXException, IOException {

    // parse document
    XMLInputSource source = new XMLInputSource(null, systemId, null);
    try {
      parse(source);
    }

    // wrap XNI exceptions as SAX exceptions
    catch (XMLParseException e) {
      Exception ex = e.getException();
      if (ex == null) {
        // must be a parser exception; mine it for locator info and throw
        // a SAXParseException
        LocatorImpl locatorImpl = new LocatorImpl();
        locatorImpl.setPublicId(e.getPublicId());
        locatorImpl.setSystemId(e.getExpandedSystemId());
        locatorImpl.setLineNumber(e.getLineNumber());
        locatorImpl.setColumnNumber(e.getColumnNumber());
        throw new SAXParseException(e.getMessage(), locatorImpl);
      }
      if (ex instanceof SAXException) {
        // why did we create an XMLParseException?
        throw (SAXException) ex;
      }
      if (ex instanceof IOException) {
        throw (IOException) ex;
      }
      throw new SAXException(ex);
    } catch (XNIException e) {
      e.printStackTrace();
      Exception ex = e.getException();
      if (ex == null) {
        throw new SAXException(e.getMessage());
      }
      if (ex instanceof SAXException) {
        throw (SAXException) ex;
      }
      if (ex instanceof IOException) {
        throw (IOException) ex;
      }
      throw new SAXException(ex);
    }
  } // parse(String)
예제 #7
0
파일: SAXWriter.java 프로젝트: enenuki/phd
 /* 406:    */
 /* 407:    */ protected void documentLocator(Document document) /* 408:    */ throws SAXException
       /* 409:    */ {
   /* 410:661 */ LocatorImpl locator = new LocatorImpl();
   /* 411:    */
   /* 412:663 */ String publicID = null;
   /* 413:664 */ String systemID = null;
   /* 414:665 */ DocumentType docType = document.getDocType();
   /* 415:667 */ if (docType != null)
   /* 416:    */ {
     /* 417:668 */ publicID = docType.getPublicID();
     /* 418:669 */ systemID = docType.getSystemID();
     /* 419:    */ }
   /* 420:672 */ if (publicID != null) {
     /* 421:673 */ locator.setPublicId(publicID);
     /* 422:    */ }
   /* 423:676 */ if (systemID != null) {
     /* 424:677 */ locator.setSystemId(systemID);
     /* 425:    */ }
   /* 426:680 */ locator.setLineNumber(-1);
   /* 427:681 */ locator.setColumnNumber(-1);
   /* 428:    */
   /* 429:683 */ this.contentHandler.setDocumentLocator(locator);
   /* 430:    */ }
예제 #8
0
  /**
   * Generate XML data.
   *
   * @throws IOException
   * @throws ProcessingException
   * @throws SAXException
   */
  public void generate() throws IOException, SAXException, ProcessingException {
    InputStreamReader in = null;

    try {
      final InputStream sis = this.inputSource.getInputStream();
      if (sis == null) {
        throw new ProcessingException("Source '" + this.inputSource.getURI() + "' not found");
      }

      if (encoding != null) {
        in = new InputStreamReader(sis, encoding);
      } else {
        in = new InputStreamReader(sis);
      }
    } catch (SourceException se) {
      throw new ProcessingException("Error during resolving of '" + this.source + "'.", se);
    }

    LocatorImpl locator = new LocatorImpl();

    locator.setSystemId(this.inputSource.getURI());
    locator.setLineNumber(1);
    locator.setColumnNumber(1);

    contentHandler.setDocumentLocator(locator);
    contentHandler.startDocument();
    contentHandler.startPrefixMapping("", URI);

    AttributesImpl atts = new AttributesImpl();
    if (localizable) {
      atts.addAttribute("", "source", "source", "CDATA", locator.getSystemId());
      atts.addAttribute("", "line", "line", "CDATA", String.valueOf(locator.getLineNumber()));
      atts.addAttribute("", "column", "column", "CDATA", String.valueOf(locator.getColumnNumber()));
    }

    contentHandler.startElement(URI, "text", "text", atts);

    LineNumberReader reader = new LineNumberReader(in);
    String line;
    String newline = null;

    while (true) {
      if (newline == null) {
        line = convertNonXmlChars(reader.readLine());
      } else {
        line = newline;
      }
      if (line == null) {
        break;
      }
      newline = convertNonXmlChars(reader.readLine());
      if (newline != null) {
        line += SystemUtils.LINE_SEPARATOR;
      }
      locator.setLineNumber(reader.getLineNumber());
      locator.setColumnNumber(1);
      contentHandler.characters(line.toCharArray(), 0, line.length());
      if (newline == null) {
        break;
      }
    }
    reader.close();
    contentHandler.endElement(URI, "text", "text");
    contentHandler.endPrefixMapping("");
    contentHandler.endDocument();
  }
 static {
   DUMMY_LOCATOR.setLineNumber(-1);
   DUMMY_LOCATOR.setColumnNumber(-1);
 }
예제 #10
0
  /**
   * 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:
    }
  }