/** * Parse the content of the file specified as XML using the * specified {@link org.xml.sax.HandlerBase}. * <i> Use of the DefaultHandler version of this method is recommended as * the HandlerBase class has been deprecated in SAX 2.0</i> * * @param f The file containing the XML to parse * @param hb The SAX HandlerBase to use. * * @throws IllegalArgumentException If the File object is null. * @throws IOException If any IO errors occur. * @throws SAXException If any SAX errors occur during processing. * * @see org.xml.sax.DocumentHandler */ public void parse(File f, HandlerBase hb) throws SAXException, IOException { if (f == null) { throw new IllegalArgumentException("File cannot be null"); } String escapedURI = FilePathToURI.filepath2URI(f.getAbsolutePath()); if (DEBUG) { System.out.println("Escaped URI = " + escapedURI); } InputSource input = new InputSource(escapedURI); this.parse(input, hb); }
public void parse(InputSource is, HandlerBase hb) throws SAXException, IOException { if (is == null) { throw new IllegalArgumentException(); } if (hb != null) { xmlReader.setDocumentHandler(hb); xmlReader.setEntityResolver(hb); xmlReader.setErrorHandler(hb); xmlReader.setDTDHandler(hb); xmlReader.setContentHandler(null); } xmlReader.parse(is); }
/** * To test the parse method. The specification says that this method * will throw an exception if the embedded XMLReader does not support * the http://xml.org/sax/features/namespace-prefixes property. * * @throws Exception If any errors occur. */ @Test public void parse01() throws Exception { try (FileInputStream fis = new FileInputStream(XML_DIR + "namespace1.xml")) { XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); if (!xmlReader.getFeature(NM_PREFIXES_PROPERTY)) { xmlReader.setFeature(NM_PREFIXES_PROPERTY, true); } XMLReaderAdapter xmlRA = new XMLReaderAdapter(xmlReader); xmlRA.setDocumentHandler(new HandlerBase()); xmlRA.parse(new InputSource(fis)); } }
/** * Test with an error in XML file, parsing should fail and throw * SAXException. * * @param saxparser a SAXParser instance. * @throws Exception If any errors occur. */ @Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider") public void testParse13(SAXParser saxparser) throws Exception { try (FileInputStream instream = new FileInputStream(new File( XML_DIR, "invalid.xml"))) { saxparser.parse(instream, new HandlerBase()); } }
/** * Test with valid input stream, parser should parse the XML document * successfully. * * @param saxparser a SAXParser instance. * @throws Exception If any errors occur. */ @Test(dataProvider = "parser-provider") public void testParse15(SAXParser saxparser) throws Exception { try (FileInputStream instream = new FileInputStream(new File(XML_DIR, "correct.xml"))) { saxparser.parse(instream, new HandlerBase()); } }
/** * Test with valid input source, parser should parse the XML document * successfully. * * @param saxparser a SAXParser instance. * @throws Exception If any errors occur. */ @Test(dataProvider = "parser-provider") public void testParse16(SAXParser saxparser) throws Exception { try (FileInputStream instream = new FileInputStream( new File(XML_DIR, "parsertest.xml"))) { saxparser.parse(instream, new HandlerBase(), new File(XML_DIR).toURI().toASCIIString()); } }
/** * Test with input source attached an invalid XML, parsing should fail * and throw SAXException. * * @param saxparser a SAXParser instance. * @throws Exception If any errors occur. */ @Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider") public void testParse20(SAXParser saxparser) throws Exception { try(FileInputStream instream = new FileInputStream(new File(XML_DIR, "invalid.xml"))) { saxparser.parse(new InputSource(instream), new HandlerBase()); } }
/** * Test with input source attached an valid XML, parser should * successfully parse the XML document. * * @param saxparser a SAXParser instance. * @throws Exception If any errors occur. */ @Test(dataProvider = "parser-provider") public void testParse21(SAXParser saxparser) throws Exception { try (FileInputStream instream = new FileInputStream(new File(XML_DIR, "correct.xml"))) { saxparser.parse(new InputSource(instream), new HandlerBase()); } }
/** * Test case to parse an XML file that uses namespaces. * * @throws Exception If any errors occur. */ @Test public void testParse31() throws Exception { try (FileInputStream instream = new FileInputStream( new File(XML_DIR, "ns4.xml"))) { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); spf.newSAXParser().parse(instream, new HandlerBase()); } }
/** * To test the parse method. The specification says that this method * will throw an exception if the embedded XMLReader does not support * the http://xml.org/sax/features/namespace-prefixes property. * * @throws Exception If any errors occur. */ @Test public void parse01() throws Exception { setPermissions(new FilePermission(XML_DIR + "/-", "read")); try (FileInputStream fis = new FileInputStream(XML_DIR + "namespace1.xml")) { XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); if (!xmlReader.getFeature(NM_PREFIXES_PROPERTY)) { xmlReader.setFeature(NM_PREFIXES_PROPERTY, true); } XMLReaderAdapter xmlRA = new XMLReaderAdapter(xmlReader); xmlRA.setDocumentHandler(new HandlerBase()); xmlRA.parse(new InputSource(fis)); } setPermissions(); }
/** * Test with non-existence URI, parsing should fail and throw IOException. * * @param saxparser a SAXParser instance. * @throws Exception If any errors occur. */ @Test(expectedExceptions = { SAXException.class }, dataProvider = "parser-provider") public void testParse03(SAXParser saxparser) throws Exception { String workingDir = getSystemProperty("user.dir"); setPermissions(new FilePermission(workingDir, "read")); try { saxparser.parse("", new HandlerBase()); } finally { setPermissions(); } }
/** * Test with empty string as File, parsing should fail and throw * SAXException. * * @param saxparser a SAXParser instance. * @throws Exception If any errors occur. */ @Test(expectedExceptions = SAXException.class, dataProvider = "parser-provider") public void testParse05(SAXParser saxparser) throws Exception { String workingDir = getSystemProperty("user.dir"); setPermissions(new FilePermission(workingDir, "read")); try { saxparser.parse(new File(""), new HandlerBase()); } finally { setPermissions(); } }
/** * Test with an error in XML file, parsing should fail and throw * SAXException. * * @param saxparser a SAXParser instance. * @throws Exception If any errors occur. */ @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class, dataProvider = "parser-provider") public void testParse13(SAXParser saxparser) throws Exception { try (FileInputStream instream = new FileInputStream(new File( XML_DIR, "invalid.xml"))) { saxparser.parse(instream, new HandlerBase()); } }
/** * Test with valid input stream, parser should parse the XML document * successfully. * * @param saxparser a SAXParser instance. * @throws Exception If any errors occur. */ @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider") public void testParse15(SAXParser saxparser) throws Exception { try (FileInputStream instream = new FileInputStream(new File(XML_DIR, "correct.xml"))) { saxparser.parse(instream, new HandlerBase()); } }
/** * Test with valid input source, parser should parse the XML document * successfully. * * @param saxparser a SAXParser instance. * @throws Exception If any errors occur. */ @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider") public void testParse16(SAXParser saxparser) throws Exception { try (FileInputStream instream = new FileInputStream( new File(XML_DIR, "parsertest.xml"))) { saxparser.parse(instream, new HandlerBase(), new File(XML_DIR).toURI().toASCIIString()); } }
/** * Test with input source attached an invalid XML, parsing should fail * and throw SAXException. * * @param saxparser a SAXParser instance. * @throws Exception If any errors occur. */ @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class, dataProvider = "parser-provider") public void testParse20(SAXParser saxparser) throws Exception { try(FileInputStream instream = new FileInputStream(new File(XML_DIR, "invalid.xml"))) { saxparser.parse(new InputSource(instream), new HandlerBase()); } }
/** * Test with input source attached an valid XML, parser should * successfully parse the XML document. * * @param saxparser a SAXParser instance. * @throws Exception If any errors occur. */ @Test(groups = {"readLocalFiles"}, dataProvider = "parser-provider") public void testParse21(SAXParser saxparser) throws Exception { try (FileInputStream instream = new FileInputStream(new File(XML_DIR, "correct.xml"))) { saxparser.parse(new InputSource(instream), new HandlerBase()); } }
/** * Test case to parse an XML file that uses namespaces. * * @throws Exception If any errors occur. */ @Test(groups = {"readLocalFiles"}) public void testParse31() throws Exception { try (FileInputStream instream = new FileInputStream( new File(XML_DIR, "ns4.xml"))) { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); spf.newSAXParser().parse(instream, new HandlerBase()); } }
/** * Parse the specified input stream, reporting SAX1 events to the given * handler. * Prefer the SAX2 version of this method, since the HandlerBase class is * now deprecated. * @param is an XML input stream * @param hb the SAX1 handler * @param systemId the system ID of the XML document * @exception IllegalArgumentException if the input stream is null * @see #parse(java.io.InputStream,org.xml.sax.helpers.DefaultHandler,java.lang.String) */ public void parse(InputStream is, HandlerBase hb, String systemId) throws SAXException, IOException { if (is == null) { throw new IllegalArgumentException("input stream is null"); } InputSource source = new InputSource(is); source.setSystemId(systemId); parse(source, hb); }
/** * Parse the content of the specified file, reporting SAX1 events to the * given handler. * Prefer the SAX2 version of this method, since the HandlerBase class is * now deprecated. * @param f an XML file * @param hb the SAX1 handler * @exception IllegalArgumentException if the file is null * @see #parse(java.io.File,org.xml.sax.helpers.DefaultHandler) */ public void parse(File f, HandlerBase hb) throws SAXException, IOException { if (f == null) { throw new IllegalArgumentException("file is null"); } InputSource source = new InputSource(new FileInputStream(f)); source.setSystemId(f.toURL().toString()); parse(source, hb); }
/** * Parse the specified input source, reporting SAX1 events to the * given handler. * Prefer the SAX2 version of this method, since the HandlerBase class is * now deprecated. * @param is the SAX input source * @param hb the SAX1 handler * @exception IllegalArgumentException if the input source is null * @see #parse(org.xml.sax.InputSource,org.xml.sax.helpers.DefaultHandler) */ public void parse(InputSource is, HandlerBase hb) throws SAXException, IOException { if (is == null) { throw new IllegalArgumentException("input source is null"); } Parser parser = getParser(); parser.setDocumentHandler(hb); parser.setDTDHandler(hb); parser.setEntityResolver(hb); parser.setErrorHandler(hb); parser.parse(is); }