org.xml.sax.helpers.DefaultHandler Java Examples
The following examples show how to use
org.xml.sax.helpers.DefaultHandler.
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: SAXParser.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
/** * Parse the content given {@link org.xml.sax.InputSource} * as XML using the specified * {@link org.xml.sax.helpers.DefaultHandler}. * * @param is The InputSource containing the content to be parsed. * @param dh The SAX DefaultHandler to use. * * @throws IllegalArgumentException If the <code>InputSource</code> object * is <code>null</code>. * @throws IOException If any IO errors occur. * @throws SAXException If any SAX errors occur during processing. * * @see org.xml.sax.DocumentHandler */ public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException { if (is == null) { throw new IllegalArgumentException("InputSource cannot be null"); } XMLReader reader = this.getXMLReader(); if (dh != null) { reader.setContentHandler(dh); reader.setEntityResolver(dh); reader.setErrorHandler(dh); reader.setDTDHandler(dh); } reader.parse(is); }
Example #2
Source File: SAXParserTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
public void testCloseReaders() throws Exception { if (!System.getProperty("os.name").contains("Windows")) { System.out.println("This test only needs to be run on Windows."); return; } Path testFile = createTestFile(null, "Test"); System.out.println("Test file: " + testFile.toString()); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); try { parser.parse(testFile.toFile(), new DefaultHandler() { @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { throw new SAXException("Stop the parser."); } }); } catch (SAXException e) { // Do nothing } // deletion failes on Windows when the file is not closed Files.deleteIfExists(testFile); }
Example #3
Source File: PieDatasetHandler.java From openstock with GNU General Public License v3.0 | 6 votes |
/** * Starts an element. * * @param namespaceURI the namespace. * @param localName the element name. * @param qName the element name. * @param atts the element attributes. * * @throws SAXException for errors. */ @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { DefaultHandler current = getCurrentHandler(); if (current != this) { current.startElement(namespaceURI, localName, qName, atts); } else if (qName.equals(PIEDATASET_TAG)) { this.dataset = new DefaultPieDataset(); } else if (qName.equals(ITEM_TAG)) { ItemHandler subhandler = new ItemHandler(this, this); getSubHandlers().push(subhandler); subhandler.startElement(namespaceURI, localName, qName, atts); } }
Example #4
Source File: CategoryDatasetHandler.java From SIMVA-SoS with Apache License 2.0 | 6 votes |
/** * The start of an element. * * @param namespaceURI the namespace. * @param localName the element name. * @param qName the element name. * @param atts the element attributes. * * @throws SAXException for errors. */ @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { DefaultHandler current = getCurrentHandler(); if (current != this) { current.startElement(namespaceURI, localName, qName, atts); } else if (qName.equals(CATEGORYDATASET_TAG)) { this.dataset = new DefaultCategoryDataset(); } else if (qName.equals(SERIES_TAG)) { CategorySeriesHandler subhandler = new CategorySeriesHandler(this); getSubHandlers().push(subhandler); subhandler.startElement(namespaceURI, localName, qName, atts); } else { throw new SAXException("Element not recognised: " + qName); } }
Example #5
Source File: SaxParserSafePrivilegedExceptionAction.java From Android_Code_Arbiter with GNU Lesser General Public License v3.0 | 6 votes |
private static void receiveXMLStream(final InputStream inStream, final DefaultHandler defHandler) throws ParserConfigurationException, SAXException, IOException { // ... SAXParserFactory spf = SAXParserFactory.newInstance(); final SAXParser saxParser = spf.newSAXParser(); try { AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() throws SAXException, IOException { saxParser.parse(inStream, defHandler); return null; } }, RESTRICTED_ACCESS_CONTROL); // From nested class } catch (PrivilegedActionException pae) { System.out.println("Filesystem access blocked"); pae.printStackTrace(); } }
Example #6
Source File: PieDatasetHandler.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Starts an element. * * @param namespaceURI the namespace. * @param localName the element name. * @param qName the element name. * @param atts the element attributes. * * @throws SAXException for errors. */ public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { DefaultHandler current = getCurrentHandler(); if (current != this) { current.startElement(namespaceURI, localName, qName, atts); } else if (qName.equals(PIEDATASET_TAG)) { this.dataset = new DefaultPieDataset(); } else if (qName.equals(ITEM_TAG)) { ItemHandler subhandler = new ItemHandler(this, this); getSubHandlers().push(subhandler); subhandler.startElement(namespaceURI, localName, qName, atts); } }
Example #7
Source File: EDIAbstractReaderTest.java From edireader with GNU General Public License v3.0 | 6 votes |
@Test public void testCopyWriter() throws Exception { // Create a reader from ANSI data String ediData = EDITestData.getAnsiInterchange(); inputSource = new InputSource(new StringReader(ediData)); reader = EDIReaderFactory.createEDIReader(inputSource); reader.setContentHandler(new DefaultHandler()); StringWriter sw = new StringWriter(); reader.setCopyWriter(sw); reader.parse(inputSource); // Now compare the copy to the original assertEquals(ediData, sw.toString()); }
Example #8
Source File: EDIAbstractReaderTest.java From edireader with GNU General Public License v3.0 | 6 votes |
@Test public void testHandlersEtc() throws Exception { reader = EDIReaderFactory.createEDIReader(EDITestData .getAnsiInputSource()); assertNull(reader.getDTDHandler()); assertNull(reader.getErrorHandler()); assertNull(reader.getEntityResolver()); AnEntityResolver er = new AnEntityResolver(); reader.setEntityResolver(er); assertSame(er, reader.getEntityResolver()); ErrorHandler eh = new AnErrorHandler(); reader.setErrorHandler(eh); assertSame(eh, reader.getErrorHandler()); ContentHandler ch = new DefaultHandler(); reader.setContentHandler(ch); assertSame(ch, reader.getContentHandler()); BranchingWriter ackStream = new BranchingWriter(new StringWriter()); reader.setAckStream(ackStream); assertSame(ackStream, reader.getAckStream()); }
Example #9
Source File: SAXParser.java From JDKSourceCode1.8 with MIT License | 6 votes |
/** * Parse the content given {@link org.xml.sax.InputSource} * as XML using the specified * {@link org.xml.sax.helpers.DefaultHandler}. * * @param is The InputSource containing the content to be parsed. * @param dh The SAX DefaultHandler to use. * * @throws IllegalArgumentException If the <code>InputSource</code> object * is <code>null</code>. * @throws IOException If any IO errors occur. * @throws SAXException If any SAX errors occur during processing. * * @see org.xml.sax.DocumentHandler */ public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException { if (is == null) { throw new IllegalArgumentException("InputSource cannot be null"); } XMLReader reader = this.getXMLReader(); if (dh != null) { reader.setContentHandler(dh); reader.setEntityResolver(dh); reader.setErrorHandler(dh); reader.setDTDHandler(dh); } reader.parse(is); }
Example #10
Source File: JavaLaunchConfigurationInfo.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
public JavaLaunchConfigurationInfo(String scope) { super(); // Since MavenRuntimeClasspathProvider will only encluding test entries when: // 1. Launch configuration is JUnit/TestNG type // 2. Mapped resource is in test path. // That's why we use JUnit launch configuration here to make sure the result is right when excludeTestCode is false. // See: {@link org.eclipse.m2e.jdt.internal.launch.MavenRuntimeClasspathProvider#getArtifactScope(ILaunchConfiguration)} String launchXml = null; if ("test".equals(scope)) { launchXml = String.format(JAVA_APPLICATION_LAUNCH, "org.eclipse.jdt.junit.launchconfig"); } else { launchXml = String.format(JAVA_APPLICATION_LAUNCH, "org.eclipse.jdt.launching.localJavaApplication"); } try { DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); parser.setErrorHandler(new DefaultHandler()); StringReader reader = new StringReader(launchXml); InputSource source = new InputSource(reader); Element root = parser.parse(source).getDocumentElement(); initializeFromXML(root); } catch (ParserConfigurationException | SAXException | IOException | CoreException e) { // do nothing } }
Example #11
Source File: SAXParser.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
/** * Parse the content given {@link org.xml.sax.InputSource} * as XML using the specified * {@link org.xml.sax.helpers.DefaultHandler}. * * @param is The InputSource containing the content to be parsed. * @param dh The SAX DefaultHandler to use. * * @throws IllegalArgumentException If the <code>InputSource</code> object * is <code>null</code>. * @throws IOException If any IO errors occur. * @throws SAXException If any SAX errors occur during processing. * * @see org.xml.sax.DocumentHandler */ public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException { if (is == null) { throw new IllegalArgumentException("InputSource cannot be null"); } XMLReader reader = this.getXMLReader(); if (dh != null) { reader.setContentHandler(dh); reader.setEntityResolver(dh); reader.setErrorHandler(dh); reader.setDTDHandler(dh); } reader.parse(is); }
Example #12
Source File: IvyXmlModuleDescriptorParser.java From pushfish-android with BSD 2-Clause "Simplified" License | 6 votes |
public static void parse( URL xmlURL, URL schema, DefaultHandler handler) throws SAXException, IOException, ParserConfigurationException { InputStream xmlStream = URLHandlerRegistry.getDefault().openStream(xmlURL); try { InputSource inSrc = new InputSource(xmlStream); inSrc.setSystemId(xmlURL.toExternalForm()); parse(inSrc, schema, handler); } finally { try { xmlStream.close(); } catch (IOException e) { // ignored } } }
Example #13
Source File: IvyXmlModuleDescriptorParser.java From pushfish-android with BSD 2-Clause "Simplified" License | 6 votes |
public static void parse( InputSource xmlStream, URL schema, DefaultHandler handler) throws SAXException, IOException, ParserConfigurationException { InputStream schemaStream = null; try { if (schema != null) { schemaStream = URLHandlerRegistry.getDefault().openStream(schema); } SAXParser parser = newSAXParser(schema, schemaStream); parser.parse(xmlStream, handler); } finally { if (schemaStream != null) { try { schemaStream.close(); } catch (IOException ex) { // ignored } } } }
Example #14
Source File: CategoryDatasetHandler.java From ECG-Viewer with GNU General Public License v2.0 | 6 votes |
/** * The start of an element. * * @param namespaceURI the namespace. * @param localName the element name. * @param qName the element name. * @param atts the element attributes. * * @throws SAXException for errors. */ @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { DefaultHandler current = getCurrentHandler(); if (current != this) { current.startElement(namespaceURI, localName, qName, atts); } else if (qName.equals(CATEGORYDATASET_TAG)) { this.dataset = new DefaultCategoryDataset(); } else if (qName.equals(SERIES_TAG)) { CategorySeriesHandler subhandler = new CategorySeriesHandler(this); getSubHandlers().push(subhandler); subhandler.startElement(namespaceURI, localName, qName, atts); } else { throw new SAXException("Element not recognised: " + qName); } }
Example #15
Source File: SAXParser.java From hottub with GNU General Public License v2.0 | 6 votes |
/** * Parse the content given {@link org.xml.sax.InputSource} * as XML using the specified * {@link org.xml.sax.helpers.DefaultHandler}. * * @param is The InputSource containing the content to be parsed. * @param dh The SAX DefaultHandler to use. * * @throws IllegalArgumentException If the <code>InputSource</code> object * is <code>null</code>. * @throws IOException If any IO errors occur. * @throws SAXException If any SAX errors occur during processing. * * @see org.xml.sax.DocumentHandler */ public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException { if (is == null) { throw new IllegalArgumentException("InputSource cannot be null"); } XMLReader reader = this.getXMLReader(); if (dh != null) { reader.setContentHandler(dh); reader.setEntityResolver(dh); reader.setErrorHandler(dh); reader.setDTDHandler(dh); } reader.parse(is); }
Example #16
Source File: JavaActions.java From netbeans with Apache License 2.0 | 6 votes |
/** * Find the line number of a target in an Ant script, or some other line in an XML file. * Able to find a certain element with a certain attribute matching a given value. * See also AntTargetNode.TargetOpenCookie. * @param file an Ant script or other XML file * @param match the attribute value to match (e.g. target name) * @param elementLocalName the (local) name of the element to look for * @param elementAttributeName the name of the attribute to match on * @return the line number (0-based), or -1 if not found */ static final int findLine(FileObject file, final String match, final String elementLocalName, final String elementAttributeName) throws IOException, SAXException, ParserConfigurationException { InputSource in = new InputSource(file.toURL().toString()); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); final int[] line = new int[] {-1}; class Handler extends DefaultHandler { private Locator locator; public void setDocumentLocator(Locator l) { locator = l; } public void startElement(String uri, String localname, String qname, Attributes attr) throws SAXException { if (line[0] == -1) { if (localname.equals(elementLocalName) && match.equals(attr.getValue(elementAttributeName))) { // NOI18N line[0] = locator.getLineNumber() - 1; } } } } parser.parse(in, new Handler()); return line[0]; }
Example #17
Source File: VmIdentifierCreateResolve.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
public static void main(String args[]) throws Exception { File testcases = new File(System.getProperty("test.src", "."), "testcases"); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); DefaultHandler dh = new VmIdentifierTestHandler(); sp.parse(testcases, dh); }
Example #18
Source File: ExclusionSeqRecurrenceRule.java From sakai with Educational Community License v2.0 | 5 votes |
public ContentHandler getContentHandler(Map<String,Object> services) { return new DefaultHandler() { /* * (non-Javadoc) * * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, * java.lang.String, java.lang.String, org.xml.sax.Attributes) */ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // the children (time ranges) if ("exclude".equals(qName)) { try { m_exclusions.add(new Integer(attributes.getValue("sequence"))); } catch (Exception e) { log.warn("set: while reading exclude sequence: " + e); } } } }; }
Example #19
Source File: CategoryDatasetHandler.java From ccu-historian with GNU General Public License v3.0 | 5 votes |
/** * The end of an element. * * @param namespaceURI the namespace. * @param localName the element name. * @param qName the element name. * * @throws SAXException for errors. */ @Override public void endElement(String namespaceURI, String localName, String qName) throws SAXException { DefaultHandler current = getCurrentHandler(); if (current != this) { current.endElement(namespaceURI, localName, qName); } }
Example #20
Source File: CategoryDatasetHandler.java From opensim-gui with Apache License 2.0 | 5 votes |
/** * The end of an element. * * @param namespaceURI the namespace. * @param localName the element name. * @param qName the element name. * * @throws SAXException for errors. */ public void endElement(String namespaceURI, String localName, String qName) throws SAXException { DefaultHandler current = getCurrentHandler(); if (current != this) { current.endElement(namespaceURI, localName, qName); } }
Example #21
Source File: SAXDocumentParser.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
/** Creates a new instance of DocumetParser2 */ public SAXDocumentParser() { DefaultHandler handler = new DefaultHandler(); _attributes = new AttributesHolder(_registeredEncodingAlgorithms); _entityResolver = handler; _dtdHandler = handler; _contentHandler = handler; _errorHandler = handler; _lexicalHandler = new LexicalHandlerImpl(); _declHandler = new DeclHandlerImpl(); }
Example #22
Source File: SAXParserImpl.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException { if (is == null) { throw new IllegalArgumentException(); } if (dh != null) { xmlReader.setContentHandler(dh); xmlReader.setEntityResolver(dh); xmlReader.setErrorHandler(dh); xmlReader.setDTDHandler(dh); xmlReader.setDocumentHandler(null); } xmlReader.parse(is); }
Example #23
Source File: HostIdentifierCreate.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
public static void main(String args[]) throws Exception { File testcases = new File(System.getProperty("test.src", "."), "testcases"); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); DefaultHandler dh = new HostIdentifierTestHandler(); sp.parse(testcases, dh); }
Example #24
Source File: Report.java From tomee with Apache License 2.0 | 5 votes |
private void main() throws Exception { // final File file = new File("/Users/dblevins/work/uber/geronimo-tck-public-trunk/jcdi-tck-runner/target/surefire-reports/testng-results.xml"); final File file = new File("/Users/dblevins/work/all/trunk/openejb/tck/cdi-tomee/target/failsafe-reports/testng-results.xml"); // final File file = new File("/Users/dblevins/work/uber/testng-results.xml"); final SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); parser.parse(file, new DefaultHandler() { @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { final String name = qName; if ("class".equals(name)) { classes.add(new TestClass(attributes.getValue("name"))); } if ("test-method".equals(name)) { classes.getLast().addStatus(attributes.getValue("status"), attributes.getValue("name")); } } }); Collections.sort(classes); textReport(file); passingXml(file); failingXml(file); }
Example #25
Source File: SafeXMLConfiguration.java From nifi with Apache License 2.0 | 5 votes |
/** * This overridden createDocumentBuilder() method sets the appropriate factory attributes to disable XXE parsing. * * @return Returns a safe DocumentBuilder * @throws ParserConfigurationException A configuration error */ @Override public DocumentBuilder createDocumentBuilder() throws ParserConfigurationException { if (getDocumentBuilder() != null) { return getDocumentBuilder(); } final DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); if (isValidating()) { factory.setValidating(true); if (isSchemaValidation()) { factory.setNamespaceAware(true); factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); } } // Disable DTDs and external entities to protect against XXE factory.setAttribute(DISALLOW_DOCTYPES, true); factory.setAttribute(ALLOW_EXTERNAL_GENERAL_ENTITIES, false); factory.setAttribute(ALLOW_EXTERNAL_PARAM_ENTITIES, false); factory.setAttribute(ALLOW_EXTERNAL_DTD, false); final DocumentBuilder result; result = factory.newDocumentBuilder(); result.setEntityResolver(super.getEntityResolver()); if (isValidating()) { // register an error handler which detects validation errors result.setErrorHandler(new DefaultHandler() { @Override public void error(SAXParseException ex) throws SAXException { throw ex; } }); } return result; }
Example #26
Source File: JAXBValidationBuilder.java From ph-commons with Apache License 2.0 | 5 votes |
public void validate (@Nonnull final JAXBTYPE aJAXBDocument, @Nonnull final ErrorList aErrorList) { ValueEnforcer.notNull (aJAXBDocument, "JAXBDocument"); ValueEnforcer.notNull (aErrorList, "ErrorList"); // Avoid class cast exception later on if (!m_aDocType.getImplementationClass ().getPackage ().equals (aJAXBDocument.getClass ().getPackage ())) { throw new IllegalArgumentException ("You cannot validate a '" + aJAXBDocument.getClass () + "' as a " + m_aDocType.getImplementationClass ().getPackage ().getName ()); } final WrappedCollectingValidationEventHandler aEventHandler = new WrappedCollectingValidationEventHandler (aErrorList); try { // create a Marshaller final Marshaller aMarshaller = createMarshaller (); aMarshaller.setEventHandler (aEventHandler); // Customize on demand final Consumer <? super Marshaller> aCustomizer = getMarshallerCustomizer (); if (aCustomizer != null) aCustomizer.accept (aMarshaller); // start marshalling final JAXBElement <?> aJAXBElement = createJAXBElement (aJAXBDocument); // DefaultHandler has very little overhead and does nothing aMarshaller.marshal (aJAXBElement, new DefaultHandler ()); } catch (final JAXBException ex) { // Should already be contained as an entry in the event handler } }
Example #27
Source File: AttachmentDeserializerTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testCXF2542() throws Exception { StringBuilder buf = new StringBuilder(512); buf.append("------=_Part_0_2180223.1203118300920\n"); buf.append("Content-Type: application/xop+xml; charset=UTF-8; type=\"text/xml\"\n"); buf.append("Content-Transfer-Encoding: 8bit\n"); buf.append("Content-ID: <[email protected]>\n"); buf.append('\n'); buf.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<soap:Body><getNextMessage xmlns=\"http://foo.bar\" /></soap:Body>" + "</soap:Envelope>\n"); buf.append("------=_Part_0_2180223.1203118300920--\n"); InputStream rawInputStream = new ByteArrayInputStream(buf.toString().getBytes()); MessageImpl message = new MessageImpl(); message.setContent(InputStream.class, rawInputStream); message.put(Message.CONTENT_TYPE, "multipart/related; type=\"application/xop+xml\"; " + "start=\"<[email protected]>\"; " + "start-info=\"text/xml\"; boundary=\"----=_Part_0_2180223.1203118300920\""); new AttachmentDeserializer(message).initializeAttachments(); InputStream inputStreamWithoutAttachments = message.getContent(InputStream.class); SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); parser.parse(inputStreamWithoutAttachments, new DefaultHandler()); inputStreamWithoutAttachments.close(); rawInputStream.close(); }
Example #28
Source File: ItemHandler.java From buffer_bci with GNU General Public License v3.0 | 5 votes |
/** * Creates a new item handler. * * @param root the root handler. * @param parent the parent handler. */ public ItemHandler(RootHandler root, DefaultHandler parent) { this.root = root; this.parent = parent; this.key = null; this.value = null; }
Example #29
Source File: CategoryDatasetHandler.java From SIMVA-SoS with Apache License 2.0 | 5 votes |
/** * The end of an element. * * @param namespaceURI the namespace. * @param localName the element name. * @param qName the element name. * * @throws SAXException for errors. */ @Override public void endElement(String namespaceURI, String localName, String qName) throws SAXException { DefaultHandler current = getCurrentHandler(); if (current != this) { current.endElement(namespaceURI, localName, qName); } }
Example #30
Source File: UTF8ReaderBug.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
private static void sendToParser(String b) throws Throwable { byte[] input = b.getBytes("UTF-8"); ByteArrayInputStream in = new ByteArrayInputStream(input); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser p = spf.newSAXParser(); p.parse(new InputSource(in), new DefaultHandler()); }