Package javax.xml.parsers

Examples of javax.xml.parsers.DocumentBuilder


   
    private Document getDocument(String str) throws Exception {
        if (null == document) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder parser = factory.newDocumentBuilder();
            document = parser.parse(new InputSource(new ByteArrayInputStream(str.getBytes())));
        }
        return document;
    }


  public void testXMPSchemaBibtexElement()
    throws ParserConfigurationException {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory
      .newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    Element e = builder.newDocument().createElement("rdf:Description");

    XMPSchemaBibtex bibtex = new XMPSchemaBibtex(e, "bibtex");

    assertEquals(e, bibtex.getElement());
    assertEquals("rdf:Description", bibtex.getElement().getTagName());

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

            dbf.setNamespaceAware(true);
            dbf.setValidating(false);

            DocumentBuilder db = dbf.newDocumentBuilder();
     
      // We don't allow users to override the Apache XML Security
      // configuration in the JRE. Users should use the standard security
      // provider mechanism instead if implementing their own
      // transform or canonicalization algorithms.
            // String cfile = System.getProperty("com.sun.org.apache.xml.internal.security.resource.config");
      // InputStream is =
      //     Class.forName("com.sun.org.apache.xml.internal.security.Init")
      //     .getResourceAsStream(cfile != null ? cfile : "resource/config.xml");
      InputStream is = (InputStream) AccessController.doPrivileged(
    new PrivilegedAction() {
        public Object run() {
      return getClass().getResourceAsStream
            ("resource/config.xml");
        }
    });

            Document doc = db.parse(is);
            long XX_parsing_end = System.currentTimeMillis();                      
            long XX_configure_i18n_start = 0;           
           
            {
                XX_configure_reg_keyInfo_start = System.currentTimeMillis();

      dfactory.setNamespaceAware(true);

      // needs to validate for ID attribute nomalization
      dfactory.setValidating(true);

      DocumentBuilder db = dfactory.newDocumentBuilder();

      /*
       * for some of the test vectors from the specification,
       * there has to be a validatin parser for ID attributes, default
       * attribute values, NMTOKENS, etc.
       * Unfortunaltely, the test vectors do use different DTDs or
       * even no DTD. So Xerces 1.3.1 fires many warnings about using
       * ErrorHandlers.
       *
       * Text from the spec:
       *
       * The input octet stream MUST contain a well-formed XML document,
       * but the input need not be validated. However, the attribute
       * value normalization and entity reference resolution MUST be
       * performed in accordance with the behaviors of a validating
       * XML processor. As well, nodes for default attributes (declared
       * in the ATTLIST with an AttValue but not specified) are created
       * in each element. Thus, the declarations in the document type
       * declaration are used to help create the canonical form, even
       * though the document type declaration is not retained in the
       * canonical form.
       *
       */
      db.setErrorHandler(new com.sun.org.apache.xml.internal.security.utils
         .IgnoreAllErrorHandler());

      Document document = db.parse(in);
      byte result[] = this.canonicalizeSubtree(document);

      return result;
   }

      DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();

      // needs to validate for ID attribute nomalization
      dfactory.setNamespaceAware(true);

      DocumentBuilder db = dfactory.newDocumentBuilder();

      /*
       * for some of the test vectors from the specification,
       * there has to be a validatin parser for ID attributes, default
       * attribute values, NMTOKENS, etc.
       * Unfortunaltely, the test vectors do use different DTDs or
       * even no DTD. So Xerces 1.3.1 fires many warnings about using
       * ErrorHandlers.
       *
       * Text from the spec:
       *
       * The input octet stream MUST contain a well-formed XML document,
       * but the input need not be validated. However, the attribute
       * value normalization and entity reference resolution MUST be
       * performed in accordance with the behaviors of a validating
       * XML processor. As well, nodes for default attributes (declared
       * in the ATTLIST with an AttValue but not specified) are created
       * in each element. Thus, the declarations in the document type
       * declaration are used to help create the canonical form, even
       * though the document type declaration is not retained in the
       * canonical form.
       *
       */

      // ErrorHandler eh = new C14NErrorHandler();
      // db.setErrorHandler(eh);
      Document document = db.parse(in);
      byte result[] = this.engineCanonicalizeSubTree(document);
      return result;
   }

           
            URL url = AspectXmlLoader.class.getResource("/jboss-aop_1_0.dtd");
           
            source.setSystemId(url.toString());
           
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
           
            docBuilder.setEntityResolver(new Resolver());
           
            Document doc = docBuilder.parse(source);
           
            this.deployXML(doc, null);             
         }
         finally
         {

            try {
                DocumentBuilderFactory dbf =
                    DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document d = db.parse(
            new InputSource(new StringReader(fragment)));

        Element fragElt = (Element) _contextDocument.importNode(
             d.getDocumentElement(), true);
        result = _contextDocument.createDocumentFragment();

 
  void convertToNodes() throws CanonicalizationException, ParserConfigurationException, IOException, SAXException{
    DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
        dfactory.setValidating(false);       
        dfactory.setNamespaceAware(true);
        DocumentBuilder db = dfactory.newDocumentBuilder();
        // select all nodes, also the comments.       
        try {
           db.setErrorHandler(new com.sun.org.apache.xml.internal.security.utils
              .IgnoreAllErrorHandler());

           Document doc = db.parse(this.getOctetStream());
          
           XMLUtils.circumventBug2650(doc);
           this._subNode=doc.getDocumentElement();                   
        } catch (SAXException ex) {

           // if a not-wellformed nodeset exists, put a container around it...
           ByteArrayOutputStream baos = new ByteArrayOutputStream();

           baos.write("<container>".getBytes());
           baos.write(this.getBytes());
           baos.write("</container>".getBytes());

           byte result[] = baos.toByteArray();
           Document document = db.parse(new ByteArrayInputStream(result));
           this._subNode=document.getDocumentElement().getFirstChild().getFirstChild();       
        }
        this._inputOctetStreamProxy=null;
        this.bytes=null;
  }

   }

   public static Element readerToElement(Reader r) throws Exception
   {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder parser = factory.newDocumentBuilder();
      Document doc = parser.parse(new InputSource(r));
      return doc.getDocumentElement();
   }

    */
   public void configure(String conf) throws Exception {
      Element sec = null;
      if (conf != null) {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         DocumentBuilder parser = factory.newDocumentBuilder();
         Document doc = parser.parse(new InputSource(new StringReader(conf)));
         sec = doc.getDocumentElement();

      }
      configure(sec);
   }

TOP

Related Classes of javax.xml.parsers.DocumentBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.