org.xml.sax.ext.DefaultHandler2 Java Examples

The following examples show how to use org.xml.sax.ext.DefaultHandler2. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HtmlParseTest.java    From sling-whiteboard with Apache License 2.0 5 votes vote down vote up
@Test
public void docParseSAXTest() {
    HtmlSAXSupport support = new HtmlSAXSupport(new DefaultHandler2() {
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {
            //System.out.println(localName);
        }

    }, new DefaultHandler2());
    stream.forEach(support);
}
 
Example #2
Source File: XMLTools.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new {@link DocumentBuilder} instance that is secured against XXE attacks.
 *
 * Needed because DocumentBuilder is not thread-safe and crashes when different threads try to
 * parse at the same time.
 *
 * @return
 * @throws IOException
 *             if it fails to create a {@link DocumentBuilder}
 */
public static DocumentBuilder createDocumentBuilder() throws XMLParserException {
	try {
		synchronized (BUILDER_FACTORY) {
			DocumentBuilder builder = BUILDER_FACTORY.newDocumentBuilder();
			// In contrast to the native default error handler,
			// DefaultHandler2 does not print stuff to System.err
			builder.setErrorHandler(new DefaultHandler2());
			return builder;
		}
	} catch (ParserConfigurationException e) {
		LogService.getRoot().log(Level.WARNING, "Unable to create document builder", e);
		throw new XMLParserException(e);
	}
}
 
Example #3
Source File: RemoteRepositoryBase.java    From database with GNU General Public License v2.0 4 votes vote down vote up
static protected ContextsResult contextsResults(
        final JettyResponseListener response) throws Exception {

    try {
        
        final String contentType = response.getContentType();

        if (!contentType.startsWith(IMimeTypes.MIME_APPLICATION_XML)) {

            throw new RuntimeException("Expecting Content-Type of "
                    + IMimeTypes.MIME_APPLICATION_XML + ", not "
                    + contentType);

        }

        final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        
        final Collection<Resource> contexts = 
              Collections.synchronizedCollection(new LinkedList<Resource>());

        /*
         * For example: 
         * <contexts>
         * <context uri="http://foo"/>
         * <context uri="http://bar"/>
         * </contexts>
         */
        parser.parse(response.getInputStream(), new DefaultHandler2(){

           @Override
            public void startElement(final String uri,
                    final String localName, final String qName,
                    final Attributes attributes) {

                if ("context".equals(qName))
                 contexts.add(new URIImpl(attributes.getValue("uri")));

            }
            
        });
        
        // done.
        return new ContextsResult(contexts);

    } finally {

     if (response != null) {
        response.abort();
     }
     
    }

}
 
Example #4
Source File: DefaultHandler2Test.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void testDefaultHandler2() {
    new DefaultHandler2();
}
 
Example #5
Source File: RemoteRepositoryBase.java    From database with GNU General Public License v2.0 2 votes vote down vote up
static protected MutationResult mutationResults(final JettyResponseListener response)
        throws Exception {

    try {

        final String contentType = response.getContentType();

        if (!contentType.startsWith(IMimeTypes.MIME_APPLICATION_XML)) {

            throw new RuntimeException("Expecting Content-Type of "
                    + IMimeTypes.MIME_APPLICATION_XML + ", not "
                    + contentType);

        }

        final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        
        final AtomicLong mutationCount = new AtomicLong();
        final AtomicLong elapsedMillis = new AtomicLong();
        
        /*
         * For example: <data modified="5" milliseconds="112"/>
         */
        parser.parse(response.getInputStream(), new DefaultHandler2(){

           @Override
            public void startElement(final String uri,
                    final String localName, final String qName,
                    final Attributes attributes) {

                if (!"data".equals(qName))
                    throw new RuntimeException("Expecting: 'data', but have: uri=" + uri
                            + ", localName=" + localName + ", qName="
                            + qName);

                mutationCount.set(Long.valueOf(attributes
                        .getValue("modified")));

                elapsedMillis.set(Long.valueOf(attributes
                        .getValue("milliseconds")));
                       
            }
            
        });
        
        // done.
        return new MutationResult(mutationCount.get(), elapsedMillis.get());

    } finally {

     if (response != null) {
        response.abort();
     }
     
    }

}
 
Example #6
Source File: RemoteRepositoryBase.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
   * Accept and parse a boolean response (NSS specific response type).
   */
  static protected BooleanResult booleanResults(
        final JettyResponseListener response) throws Exception {

    try {
        
        final String contentType = response.getContentType();

        if (!contentType.startsWith(IMimeTypes.MIME_APPLICATION_XML)) {

            throw new RuntimeException("Expecting Content-Type of "
                    + IMimeTypes.MIME_APPLICATION_XML + ", not "
                    + contentType);

        }

        final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        
        final AtomicBoolean result = new AtomicBoolean();
        final AtomicLong elapsedMillis = new AtomicLong();

        /*
         * For example: <data rangeCount="5" milliseconds="112"/>
         */
        parser.parse(response.getInputStream(), new DefaultHandler2(){

           @Override
            public void startElement(final String uri,
                    final String localName, final String qName,
                    final Attributes attributes) {

                if (!"data".equals(qName))
                    throw new RuntimeException("Expecting: 'data', but have: uri=" + uri
                            + ", localName=" + localName + ", qName="
                            + qName);

                result.set(Boolean.valueOf(attributes
                        .getValue("result")));

                elapsedMillis.set(Long.valueOf(attributes
                        .getValue("milliseconds")));
                       
            }
            
        });
        
        // done.
        return new BooleanResult(result.get(), elapsedMillis.get());

    } finally {

     if (response != null) {
        response.abort();
     }
     
    }

}
 
Example #7
Source File: RemoteRepositoryBase.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Accept and parse a range count response (NSS specific response type).
 */
static protected RangeCountResult rangeCountResults(
        final JettyResponseListener response) throws Exception {

    try {
        
        final String contentType = response.getContentType();

        if (!contentType.startsWith(IMimeTypes.MIME_APPLICATION_XML)) {

            throw new RuntimeException("Expecting Content-Type of "
                    + IMimeTypes.MIME_APPLICATION_XML + ", not "
                    + contentType);

        }

        final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        
        final AtomicLong rangeCount = new AtomicLong();
        final AtomicLong elapsedMillis = new AtomicLong();

        /*
         * For example: <data rangeCount="5" milliseconds="112"/>
         */
        parser.parse(response.getInputStream(), new DefaultHandler2(){

           @Override
            public void startElement(final String uri,
                    final String localName, final String qName,
                    final Attributes attributes) {

                if (!"data".equals(qName))
                    throw new RuntimeException("Expecting: 'data', but have: uri=" + uri
                            + ", localName=" + localName + ", qName="
                            + qName);

                rangeCount.set(Long.valueOf(attributes
                        .getValue("rangeCount")));

                elapsedMillis.set(Long.valueOf(attributes
                        .getValue("milliseconds")));
                       
            }
            
        });
        
        // done.
        return new RangeCountResult(rangeCount.get(), elapsedMillis.get());

    } finally {

     if (response != null) {
        response.abort();
     }
     
    }

}
 
Example #8
Source File: RemoteTransactionManager.java    From database with GNU General Public License v2.0 2 votes vote down vote up
private IRemoteTxState0 singleTxResponse(final JettyResponseListener response)
      throws Exception {

   try {

      final String contentType = response.getContentType();

      if (!contentType.startsWith(IMimeTypes.MIME_APPLICATION_XML)) {

         throw new RuntimeException("Expecting Content-Type of "
               + IMimeTypes.MIME_APPLICATION_XML + ", not " + contentType);

      }

      final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();

      final AtomicLong txId = new AtomicLong();
      final AtomicLong readsOnCommitTime = new AtomicLong();

      /*
       * For example: <tx txId="-512" readsOnCommitTime="11201201212"/>
       */
      parser.parse(response.getInputStream(), new DefaultHandler2() {

         @Override
         public void startElement(final String uri, final String localName,
               final String qName, final Attributes attributes) {

            if ("response".equals(qName)) {
               // This is the outer element.
               return;
            }

            if (!"tx".equals(qName))
               throw new RuntimeException("Expecting: 'tx', but have: uri="
                     + uri + ", localName=" + localName + ", qName=" + qName);

            txId.set(Long.valueOf(attributes.getValue("txId")));

            readsOnCommitTime.set(Long.valueOf(attributes
                  .getValue("readsOnCommitTime")));

         }

      });

      // done.
      return new RemoteTxState0(txId.get(), readsOnCommitTime.get());

   } finally {

      if (response != null) {
         response.abort();
      }

   }

}
 
Example #9
Source File: RemoteTransactionManager.java    From database with GNU General Public License v2.0 2 votes vote down vote up
private List<IRemoteTxState0> multiTxResponse(
      final JettyResponseListener response) throws Exception {

   try {

      final String contentType = response.getContentType();

      if (!contentType.startsWith(IMimeTypes.MIME_APPLICATION_XML)) {

         throw new RuntimeException("Expecting Content-Type of "
               + IMimeTypes.MIME_APPLICATION_XML + ", not " + contentType);

      }

      final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();

      final List<IRemoteTxState0> list = new LinkedList<IRemoteTxState0>();

      /*
       * For example: <tx txId="-512" readsOnCommitTime="11201201212"/>
       */
      parser.parse(response.getInputStream(), new DefaultHandler2() {

         @Override
         public void startElement(final String uri, final String localName,
               final String qName, final Attributes attributes) {

            if ("response".equals(qName)) {
               // This is the outer element.
               return;
            }

            if (!"tx".equals(qName))
               throw new RuntimeException("Expecting: 'tx', but have: uri="
                     + uri + ", localName=" + localName + ", qName=" + qName);

            final long txId = Long.valueOf(attributes.getValue("txId"));

            final long readsOnCommitTime = Long.valueOf(attributes
                  .getValue("readsOnCommitTime"));

            list.add(new RemoteTxState0(txId, readsOnCommitTime));

         }

      });

      // done.
      return list;

   } finally {

      if (response != null) {
         response.abort();
      }

   }

}