List of usage examples for javax.xml.stream XMLInputFactory newInstance
public static XMLInputFactory newInstance() throws FactoryConfigurationError
From source file:net.landora.animeinfo.anidb.AniDBHTTPManager.java
public Anime downloadAnime(int aid) { InputStream is = null;/*from w w w .ja va2 s . c o m*/ try { // URL url = new URL(String.format("%s&request=anime&aid=%d", HTTP_URL, aid)); // is = new GZIPInputStream(url.openStream()); is = new BufferedInputStream(new FileInputStream("/home/bdickie/anidb/http_test.xml")); XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(is); reader.nextTag(); reader.require(XMLStreamReader.START_ELEMENT, null, "anime"); Anime anime = new Anime(); anime.setAnimeId(Integer.parseInt(reader.getAttributeValue(null, "id"))); anime.setHentai(Boolean.parseBoolean(reader.getAttributeValue(null, "restricted"))); anime.setLastLoaded(Calendar.getInstance()); while (reader.nextTag() != XMLStreamReader.END_ELEMENT) { String tagName = reader.getLocalName(); if (tagName.equals("type")) { anime.setType(nextString(reader)); } else if (tagName.equals("episodecount")) { anime.setEpisodeCount(Integer.parseInt(nextString(reader))); } else if (tagName.equals("startdate")) { // anime.setStartDate(nextString(reader)); } else if (tagName.equals("enddate")) { // anime.setEndDate(nextString(reader)); } else if (tagName.equals("description")) { anime.setDescription(nextString(reader)); } else if (tagName.equals("picture")) { anime.setPictureFileName(nextString(reader)); } else if (tagName.equals("titles")) { List<AnimeName> names = new ArrayList<AnimeName>(); while (reader.nextTag() != XMLStreamReader.END_ELEMENT) { reader.require(XMLStreamReader.START_ELEMENT, null, "title"); AnimeName name = new AnimeName(); for (int i = 0; i < reader.getAttributeCount(); i++) { String aname = reader.getAttributeLocalName(i); if (aname.equals("type")) { name.setType(reader.getAttributeValue(i)); } else if (aname.equals("lang")) { name.setLanguage(reader.getAttributeValue(i)); } } name.setName(nextString(reader)); name.setAnime(anime); names.add(name); } for (AnimeName name : names) { if (name.getType().equalsIgnoreCase("main")) { anime.setNameMain(name.getName()); } else if (name.getType().equalsIgnoreCase("official") && name.getLanguage().equalsIgnoreCase("en")) { anime.setNameEnglish(name.getName()); } } anime.setNames(names); } else if (tagName.equals("ratings")) { while (reader.nextTag() != XMLStreamReader.END_ELEMENT) { String tagName2 = reader.getLocalName(); int count = Integer.parseInt(reader.getAttributeValue(null, "count")); float value = Float.parseFloat(nextString(reader)); if (tagName2.equals("permanent")) { anime.setRatingPermanent(value); anime.setRatingPermanentVotes(count); } else if (tagName2.equals("temporary")) { anime.setRatingTemporary(value); anime.setRatingTemporaryVotes(count); } } } else if (tagName.equals("categories")) { while (reader.nextTag() != XMLStreamReader.END_ELEMENT) { reader.require(XMLStreamReader.START_ELEMENT, null, "category"); int categoryid = Integer.parseInt(reader.getAttributeValue(null, "id")); int weight = Integer.parseInt(reader.getAttributeValue(null, "weight")); AnimeCategory category = AnimeDBA.getAnimeCategory(categoryid); if (category == null) { return null; } ignoreTag(reader); } } else { ignoreTag(reader); } } reader.close(); return anime; } catch (Exception e) { log.error("Error downloading anime: " + aid, e); return null; } finally { if (is != null) { IOUtils.closeQuietly(is); } } }
From source file:com.predic8.membrane.integration.ProxyRuleTest.java
@Test public void testReadRuleFromByteBuffer() throws Exception { ProxyRule rule2 = new ProxyRule(); rule2.setRouter(router);//from w ww . j av a 2s . co m XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader reader = factory.createXMLStreamReader((new ByteArrayInputStream(buffer)), Constants.UTF_8); while (reader.next() != XMLStreamReader.START_ELEMENT) ; rule2.parse(reader); assertEquals(8888, rule2.getKey().getPort()); assertEquals("Rule 1", rule2.getName()); assertNull(rule2.getLocalHost()); assertEquals(true, rule2.isInboundTLS()); assertFalse(rule2.isOutboundTLS()); List<Interceptor> inters = rule2.getInterceptors(); assertFalse(inters.isEmpty()); assertTrue(inters.size() == 2); inters.get(0).getId().equals("roundRobinBalancer"); inters.get(1).getId().equals("accessControlInterceptor"); assertEquals(true, rule2.isBlockResponse()); assertFalse(rule2.isBlockRequest()); }
From source file:edu.jhu.hlt.concrete.ingesters.bolt.BoltForumPostIngester.java
/** * */// ww w.j ava 2s.c om public BoltForumPostIngester() { this.inF = XMLInputFactory.newInstance(); // this.inF.setProperty(XMLInputFactory.IS_COALESCING, true); // this.inF.setProperty(XMLInputFactory.IS_VALIDATING, false); }
From source file:de.tuebingen.uni.sfs.germanet.api.WiktionaryLoader.java
/** * Loads <code>WiktionaryParaphrases</code> from the specified file into this * <code>WiktionaryLoader</code>'s <code>GermaNet</code> object. * @param wiktionaryFile the file containing <code>WiktionaryParaphrases</code> data * @throws java.io.FileNotFoundException * @throws javax.xml.stream.XMLStreamException *///from w ww . jav a2 s.com protected void loadWiktionary(File wiktionaryFile) throws FileNotFoundException, XMLStreamException { wikiDir = wiktionaryFile; FilenameFilter filter = new WikiFilter(); //get only wiktionary files File[] wikiFiles = wikiDir.listFiles(filter); if (wikiFiles == null || wikiFiles.length == 0) { throw new FileNotFoundException( "Unable to load Wiktionary Paraphrases from \"" + this.wikiDir.getPath() + "\""); } for (File wikiFile : wikiFiles) { logger.debug("Loading " + wikiFile.getName() + "..."); InputStream in = new FileInputStream(wikiFile); XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader parser = factory.createXMLStreamReader(in); int event; String nodeName; //Parse entire file, looking for Wiktionary paraphrase start elements while (parser.hasNext()) { event = parser.next(); switch (event) { case XMLStreamConstants.START_DOCUMENT: namespace = parser.getNamespaceURI(); break; case XMLStreamConstants.START_ELEMENT: nodeName = parser.getLocalName(); if (nodeName.equals(GermaNet.XML_WIKTIONARY_PARAPHRASE)) { WiktionaryParaphrase wiki = processWiktionaryParaphrase(parser); germaNet.addWiktionaryParaphrase(wiki); } break; } } parser.close(); } logger.debug("Done."); }
From source file:com.predic8.membrane.core.interceptor.acl.AccessControlInterceptor.java
protected AccessControl parse(String fileName, Router router) throws Exception { try {/* w w w . j a va2 s .co m*/ XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader reader = new FixedStreamReader(factory.createXMLStreamReader(router.getResolverMap() .resolve(ResolverMap.combine(router == null ? null : router.getBaseLocation(), fileName)))); AccessControl res = (AccessControl) new AccessControl(router).parse(reader); res.init(router); return res; } catch (Exception e) { log.error("Error initializing accessControl.", e); System.err.println("Error initializing accessControl: terminating."); throw new RuntimeException(e); } }
From source file:StreamSrcStAXRst.java
private static XMLEventReader getXMLEventReader(String filename) { XMLInputFactory xmlif = null; XMLEventReader xmlr = null;/*w w w . j a v a 2 s . co m*/ try { xmlif = XMLInputFactory.newInstance(); xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE); xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE); xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); FileInputStream fis = new FileInputStream(filename); xmlr = xmlif.createXMLEventReader(filename, fis); } catch (Exception ex) { ex.printStackTrace(); } return xmlr; }
From source file:hudson.plugins.report.jck.parsers.JtregReportParser.java
@Override public Suite parsePath(Path path) { List<Test> testsList = new ArrayList<>(); try (ArchiveInputStream in = streamPath(path)) { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); ArchiveEntry entry;//from w ww.j a v a2 s. co m while ((entry = in.getNextEntry()) != null) { String entryName = entry.getName(); if (entryName == null || !entryName.endsWith(".jtr.xml")) { continue; } try { XMLStreamReader reader = inputFactory.createXMLStreamReader(new CloseShieldInputStream(in), "UTF-8"); testsList.addAll(parseTestsuites(reader)); } catch (Exception ex) { ex.printStackTrace(); } } } catch (Exception ex) { ex.printStackTrace(); } if (testsList.isEmpty()) { return null; } List<String> testNames = testsList.stream().sequential().map(t -> t.getName()).sorted() .collect(Collectors.toList()); List<Test> testProblems = testsList.stream().sequential() .filter(t -> t.getStatus() == TestStatus.ERROR || t.getStatus() == TestStatus.FAILED).sorted() .collect(Collectors.toList()); ReportFull fullReport = new ReportFull( (int) testsList.stream().sequential().filter(t -> t.getStatus() == TestStatus.PASSED).count(), (int) testsList.stream().sequential().filter(t -> t.getStatus() == TestStatus.NOT_RUN).count(), (int) testsList.stream().sequential().filter(t -> t.getStatus() == TestStatus.FAILED).count(), (int) testsList.stream().sequential().filter(t -> t.getStatus() == TestStatus.ERROR).count(), testsList.size(), testProblems, testNames); return new Suite(suiteName(path), fullReport); }
From source file:net.bulletin.pdi.xero.step.support.XMLChunkerTest.java
/** * <p>The input document contains a number of artists in a structure. This test will break the * data into individual artist such that each artist is in their own document.</p> *///from w ww .j a v a2s. com @Test public void testPullNextXmlChunk_withContainerElements() throws Exception { byte[] sampleXml = readSampleXml(); XMLChunker chunker = new XMLChunkerImpl( XMLInputFactory.newInstance().createXMLStreamReader(new ByteArrayInputStream(sampleXml)), // all in-memory createExpectedContainerElementsStack()); // --------------------------------- String actuals[] = new String[] { chunker.pullNextXmlChunk(), chunker.pullNextXmlChunk(), chunker.pullNextXmlChunk(), chunker.pullNextXmlChunk() }; // --------------------------------- // This will work through the chunks and check specific information it knows in the sample. checkChunkForArtistDetails(actuals[0], "Len Lye", new String[] { "Kinetic Sculpture", "Poetry", "Film" }); checkChunkForArtistDetails(actuals[1], "Alexander Caulder", new String[] { "Sculpture" }); checkChunkForArtistDetails(actuals[2], "Marc Chagall", new String[] { "Painting" }); Assert.assertNull("expected the last chunk to be null", actuals[3]); }
From source file:di.uniba.it.tee2.wiki.WikipediaDumpIterator.java
public WikipediaDumpIterator(File xmlFile, String encoding) throws XMLStreamException, FileNotFoundException, CompressorException, IOException { MediaWikiParserFactory parserFactory = new MediaWikiParserFactory(WikiConstants.Language.english); parserFactory.setTemplateParserClass(FlushTemplates.class); parserFactory.setShowImageText(false); parserFactory.setShowMathTagContent(false); parser = parserFactory.createParser(); XMLInputFactory inputFactory = XMLInputFactory.newInstance(); if (xmlFile.getName().endsWith(".bz2")) { logger.log(Level.INFO, "Trying to open Wikipedia compress dump (bzip2)..."); BZip2CompressorInputStream compressIS = new BZip2CompressorInputStream( new BufferedInputStream(new FileInputStream(xmlFile))); xmlStreamReader = inputFactory.createXMLStreamReader(compressIS, encoding); } else if (xmlFile.getName().endsWith(".gz")) { logger.log(Level.INFO, "Trying to open Wikipedia compress dump (gzip)..."); GZIPInputStream compressIS = new GZIPInputStream(new BufferedInputStream(new FileInputStream(xmlFile))); xmlStreamReader = inputFactory.createXMLStreamReader(compressIS, encoding); } else {//from ww w . ja v a2 s . c o m logger.log(Level.INFO, "Trying to open Wikipedia plain text dump..."); xmlStreamReader = inputFactory .createXMLStreamReader(new BufferedInputStream(new FileInputStream(xmlFile)), encoding); } }