List of usage examples for javax.xml.parsers SAXParser parse
public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException
From source file:MainClass.java
public static void main(String args[]) throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser parser = null; spf.setNamespaceAware(true);//w w w .ja v a2s . c om try { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); spf.setSchema(sf.newSchema(new SAXSource(new InputSource(new StringReader(schemaString))))); parser = spf.newSAXParser(); } catch (SAXException e) { e.printStackTrace(System.err); System.exit(1); } catch (ParserConfigurationException e) { e.printStackTrace(System.err); System.exit(1); } MySAXHandler handler = new MySAXHandler(); System.out.println(schemaString); parser.parse(new InputSource(new StringReader(xmlString)), handler); }
From source file:MainClass.java
public static void main(String args[]) { try {//from w w w . j av a2 s . c om SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean name = false; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("NAME")) { name = true; } } public void characters(char ch[], int start, int length) throws SAXException { if (name) { System.out.println("Name: " + new String(ch, start, length)); name = false; } } }; saxParser.parse(new InputSource(new StringReader(xmlString)), handler); } catch (Exception e) { e.printStackTrace(); } }
From source file:NameLister.java
public static void main(String args[]) { if (args.length != 1) { System.err.println("Usage: java NameLister xmlfile.xml"); System.exit(-1);/* w w w. ja va 2 s. c o m*/ } try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean name = false; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("NAME")) { name = true; } } public void characters(char ch[], int start, int length) throws SAXException { if (name) { System.out.println("Name: " + new String(ch, start, length)); name = false; } } }; saxParser.parse(args[0], handler); } catch (Exception e) { e.printStackTrace(); } }
From source file:it.wami.map.mongodeploy.OsmToMongoDB.java
/** * @param args/*from w ww . j av a 2s . co m*/ */ public static void main(String[] args) { if (args == null || args.length == 0 || args.length < 2) { System.out.println(Options.USAGE); return; } parseCommandOptions(args); MongoClientOptions mco = new MongoClientOptions.Builder().connectionsPerHost(150000) .threadsAllowedToBlockForConnectionMultiplier(10000).build(); MongoClient mongoClient = createMongo(options.getHost(), options.getPort(), mco); DB db = mongoClient.getDB(options.getDbName()); DBCollection nodes = db.createCollection(OsmSaxHandler.COLL_NODES, null); createNodesIndex(nodes); /*DBCollection ways = */ DBCollection ways = db.createCollection(OsmSaxHandler.COLL_WAYS, null); createWaysIndex(ways); /*DBCollection relations = */ DBCollection relations = db.createCollection(OsmSaxHandler.COLL_RELATIONS, null); createRelationsIndex(relations); db.createCollection(OsmSaxHandler.COLL_TAGS, null); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = null; try { saxParser = factory.newSAXParser(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } DefaultHandler handler = new OsmSaxHandler(db, options); try { if (options.getInput().contains(".bz2")) { try { saxParser.parse(getInputStreamForBZ2File(options.getInput()), handler); } catch (CompressorException e) { e.printStackTrace(); } } else if (options.getInput().contains(".osm")) { saxParser.parse(options.getInput(), handler); } else { throw new IllegalArgumentException("input must be an osm file or a bz2."); } } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.jkoolcloud.jesl.simulator.TNT4JSimulator.java
public static void main(String[] args) { boolean isTTY = (System.console() != null); long startTime = System.currentTimeMillis(); try {// w ww . j a v a 2 s. c om SAXParserFactory parserFactory = SAXParserFactory.newInstance(); SAXParser theParser = parserFactory.newSAXParser(); TNT4JSimulatorParserHandler xmlHandler = new TNT4JSimulatorParserHandler(); processArgs(xmlHandler, args); TrackerConfig simConfig = DefaultConfigFactory.getInstance().getConfig(TNT4JSimulator.class.getName()); logger = TrackingLogger.getInstance(simConfig.build()); if (logger.isSet(OpLevel.TRACE)) traceLevel = OpLevel.TRACE; else if (logger.isSet(OpLevel.DEBUG)) traceLevel = OpLevel.DEBUG; if (runType == SimulatorRunType.RUN_SIM) { if (StringUtils.isEmpty(simFileName)) { simFileName = "tnt4j-sim.xml"; String fileName = readFromConsole("Simulation file [" + simFileName + "]: "); if (!StringUtils.isEmpty(fileName)) simFileName = fileName; } StringBuffer simDef = new StringBuffer(); BufferedReader simLoader = new BufferedReader(new FileReader(simFileName)); String line; while ((line = simLoader.readLine()) != null) simDef.append(line).append("\n"); simLoader.close(); info("jKool Activity Simulator Run starting: file=" + simFileName + ", iterations=" + numIterations + ", ttl.sec=" + ttl); startTime = System.currentTimeMillis(); if (isTTY && numIterations > 1) System.out.print("Iteration: "); int itTrcWidth = 0; for (iteration = 1; iteration <= numIterations; iteration++) { itTrcWidth = printProgress("Executing Iteration", iteration, itTrcWidth); theParser.parse(new InputSource(new StringReader(simDef.toString())), xmlHandler); if (!Utils.isEmpty(jkFileName)) { PrintWriter gwFile = new PrintWriter(new FileOutputStream(jkFileName, true)); gwFile.println(""); gwFile.close(); } } if (numIterations > 1) System.out.println(""); info("jKool Activity Simulator Run finished, elapsed time = " + DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - startTime)); printMetrics(xmlHandler.getSinkStats(), "Total Sink Statistics"); } else if (runType == SimulatorRunType.REPLAY_SIM) { info("jKool Activity Simulator Replay starting: file=" + jkFileName + ", iterations=" + numIterations); connect(); startTime = System.currentTimeMillis(); // Determine number of lines in file BufferedReader gwFile = new BufferedReader(new java.io.FileReader(jkFileName)); for (numIterations = 0; gwFile.readLine() != null; numIterations++) ; gwFile.close(); // Reopen the file and gwFile = new BufferedReader(new java.io.FileReader(jkFileName)); if (isTTY && numIterations > 1) System.out.print("Processing Line: "); int itTrcWidth = 0; String gwMsg; iteration = 0; while ((gwMsg = gwFile.readLine()) != null) { iteration++; if (isTTY) itTrcWidth = printProgress("Processing Line", iteration, itTrcWidth); gwConn.write(gwMsg); } if (isTTY && numIterations > 1) System.out.println(""); long endTime = System.currentTimeMillis(); info("jKool Activity Simulator Replay finished, elasped.time = " + DurationFormatUtils.formatDurationHMS(endTime - startTime)); } } catch (Exception e) { if (e instanceof SAXParseException) { SAXParseException spe = (SAXParseException) e; error("Error at line: " + spe.getLineNumber() + ", column: " + spe.getColumnNumber(), e); } else { error("Error running simulator", e); } } finally { try { Thread.sleep(1000L); } catch (Exception e) { } TNT4JSimulator.disconnect(); } System.exit(0); }
From source file:efen.parsewiki.WikipediaDocumentSequence.java
public static void main(final String arg[]) throws ParserConfigurationException, SAXException, IOException, JSAPException, ClassNotFoundException { SimpleJSAP jsap = new SimpleJSAP(WikipediaDocumentSequence.class.getName(), "Computes the redirects of a Wikipedia dump and integrate them into an existing virtual document resolver for the dump.", new Parameter[] { new Switch("bzip2", 'b', "bzip2", "The file is compressed with bzip2"), new Switch("iso", 'i', "iso", "Use ISO-8859-1 coding internally (i.e., just use the lower eight bits of each character)."), new FlaggedOption("width", JSAP.INTEGER_PARSER, Integer.toString(Long.SIZE), JSAP.NOT_REQUIRED, 'w', "width", "The width, in bits, of the signatures used to sign the function from URIs to their rank."), new UnflaggedOption("file", JSAP.STRING_PARSER, JSAP.REQUIRED, "The file containing the Wikipedia dump."), new UnflaggedOption("baseURL", JSAP.STRING_PARSER, JSAP.REQUIRED, "The base URL for the collection (e.g., http://en.wikipedia.org/wiki/)."), new UnflaggedOption("uris", JSAP.STRING_PARSER, JSAP.REQUIRED, "The URIs of the documents in the collection (generated by ScanMetadata)."), new UnflaggedOption("vdr", JSAP.STRING_PARSER, JSAP.REQUIRED, "The name of a precomputed virtual document resolver for the collection."), new UnflaggedOption("redvdr", JSAP.STRING_PARSER, JSAP.REQUIRED, "The name of the resulting virtual document resolver.") }); JSAPResult jsapResult = jsap.parse(arg); if (jsap.messagePrinted()) return;//from ww w . ja v a2s .c o m final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setNamespaceAware(true); final Object2ObjectOpenHashMap<MutableString, String> redirects = new Object2ObjectOpenHashMap<MutableString, String>(); final String baseURL = jsapResult.getString("baseURL"); final ProgressLogger progressLogger = new ProgressLogger(LOGGER); progressLogger.itemsName = "redirects"; progressLogger.start("Extracting redirects..."); final SAXParser parser = saxParserFactory.newSAXParser(); final DefaultHandler handler = new DefaultHandler() { private boolean inTitle; private MutableString title = new MutableString(); @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if ("page".equals(localName)) { inTitle = false; title.length(0); } else if ("title".equals(localName) && title.length() == 0) inTitle = true; // We catch only the first title element. else if ("redirect".equals(localName) && attributes.getValue("title") != null) { progressLogger.update(); redirects.put(title.copy(), attributes.getValue("title")); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if ("title".equals(localName)) inTitle = false; } @Override public void characters(char[] ch, int start, int length) throws SAXException { if (inTitle) title.append(ch, start, length); } @Override public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { if (inTitle) title.append(ch, start, length); } }; InputStream in = new FileInputStream(jsapResult.getString("file")); if (jsapResult.userSpecified("bzip2")) in = new BZip2CompressorInputStream(in); parser.parse(new InputSource(new InputStreamReader(new FastBufferedInputStream(in), Charsets.UTF_8)), handler); progressLogger.done(); final Object2LongLinkedOpenHashMap<MutableString> resolved = new Object2LongLinkedOpenHashMap<MutableString>(); final VirtualDocumentResolver vdr = (VirtualDocumentResolver) BinIO.loadObject(jsapResult.getString("vdr")); progressLogger.expectedUpdates = redirects.size(); progressLogger.start("Examining redirects..."); for (Map.Entry<MutableString, String> e : redirects.entrySet()) { final MutableString start = new MutableString().append(baseURL) .append(Encoder.encodeTitleToUrl(e.getKey().toString(), true)); final MutableString end = new MutableString().append(baseURL) .append(Encoder.encodeTitleToUrl(e.getValue(), true)); final long s = vdr.resolve(start); if (s == -1) { final long t = vdr.resolve(end); if (t != -1) resolved.put(start.copy(), t); else LOGGER.warn("Failed redirect: " + start + " -> " + end); } else LOGGER.warn("URL " + start + " is already known to the virtual document resolver"); progressLogger.lightUpdate(); } progressLogger.done(); //System.err.println(resolved); final Iterable<MutableString> allURIs = Iterables .concat(new FileLinesCollection(jsapResult.getString("uris"), "UTF-8"), resolved.keySet()); final long numberOfDocuments = vdr.numberOfDocuments(); final TransformationStrategy<CharSequence> transformationStrategy = jsapResult.userSpecified("iso") ? TransformationStrategies.iso() : TransformationStrategies.utf16(); BinIO.storeObject(new URLMPHVirtualDocumentResolver(new SignedRedirectedStringMap(numberOfDocuments, new ShiftAddXorSignedStringMap(allURIs.iterator(), new MWHCFunction.Builder<CharSequence>().keys(allURIs).transform(transformationStrategy) .build(), jsapResult.getInt("width")), resolved.values().toLongArray())), jsapResult.getString("redvdr")); }
From source file:it.unimi.di.wikipedia.parsing.NamespacedWikipediaDocumentSequence.java
public static void main(final String arg[]) throws ParserConfigurationException, SAXException, IOException, JSAPException, ClassNotFoundException { SimpleJSAP jsap = new SimpleJSAP(NamespacedWikipediaDocumentSequence.class.getName(), "Computes the redirects of a Wikipedia dump and integrate them into an existing virtual document resolver for the dump.", new Parameter[] { new Switch("bzip2", 'b', "bzip2", "The file is compressed with bzip2"), new Switch("iso", 'i', "iso", "Use ISO-8859-1 coding internally (i.e., just use the lower eight bits of each character)."), new FlaggedOption("width", JSAP.INTEGER_PARSER, Integer.toString(Long.SIZE), JSAP.NOT_REQUIRED, 'w', "width", "The width, in bits, of the signatures used to sign the function from URIs to their rank."), new UnflaggedOption("file", JSAP.STRING_PARSER, JSAP.REQUIRED, "The file containing the Wikipedia dump."), new UnflaggedOption("baseURL", JSAP.STRING_PARSER, JSAP.REQUIRED, "The base URL for the collection (e.g., http://en.wikipedia.org/wiki/)."), new UnflaggedOption("uris", JSAP.STRING_PARSER, JSAP.REQUIRED, "The URIs of the documents in the collection (generated by ScanMetadata)."), new UnflaggedOption("vdr", JSAP.STRING_PARSER, JSAP.REQUIRED, "The name of a precomputed virtual document resolver for the collection."), new UnflaggedOption("redvdr", JSAP.STRING_PARSER, JSAP.REQUIRED, "The name of the resulting virtual document resolver.") }); JSAPResult jsapResult = jsap.parse(arg); if (jsap.messagePrinted()) return;/* w w w .j av a2s .c o m*/ final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setNamespaceAware(true); final Object2ObjectOpenHashMap<MutableString, String> redirects = new Object2ObjectOpenHashMap<MutableString, String>(); final String baseURL = jsapResult.getString("baseURL"); final ProgressLogger progressLogger = new ProgressLogger(LOGGER); progressLogger.itemsName = "redirects"; progressLogger.start("Extracting redirects..."); final SAXParser parser = saxParserFactory.newSAXParser(); final DefaultHandler handler = new DefaultHandler() { private boolean inTitle; private MutableString title = new MutableString(); @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if ("page".equals(localName)) { inTitle = false; title.length(0); } else if ("title".equals(localName) && title.length() == 0) inTitle = true; // We catch only the first title element. else if ("redirect".equals(localName) && attributes.getValue("title") != null) { progressLogger.update(); redirects.put(title.copy(), attributes.getValue("title")); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if ("title".equals(localName)) inTitle = false; } @Override public void characters(char[] ch, int start, int length) throws SAXException { if (inTitle) title.append(ch, start, length); } @Override public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { if (inTitle) title.append(ch, start, length); } }; InputStream in = new FileInputStream(jsapResult.getString("file")); if (jsapResult.userSpecified("bzip2")) in = new BZip2CompressorInputStream(in); parser.parse(new InputSource(new InputStreamReader(new FastBufferedInputStream(in), Charsets.UTF_8)), handler); progressLogger.done(); final Object2LongLinkedOpenHashMap<MutableString> resolved = new Object2LongLinkedOpenHashMap<MutableString>(); final VirtualDocumentResolver vdr = (VirtualDocumentResolver) BinIO.loadObject(jsapResult.getString("vdr")); progressLogger.expectedUpdates = redirects.size(); progressLogger.start("Examining redirects..."); for (Map.Entry<MutableString, String> e : redirects.entrySet()) { final MutableString start = new MutableString().append(baseURL) .append(Encoder.encodeTitleToUrl(e.getKey().toString(), true)); final MutableString end = new MutableString().append(baseURL) .append(Encoder.encodeTitleToUrl(e.getValue(), true)); final long s = vdr.resolve(start); if (s == -1) { final long t = vdr.resolve(end); if (t != -1) resolved.put(start.copy(), t); else LOGGER.warn("Failed redirect: " + start + " -> " + end); } else LOGGER.warn("URL " + start + " is already known to the virtual document resolver"); progressLogger.lightUpdate(); } progressLogger.done(); //System.err.println(resolved); final Iterable<MutableString> allURIs = Iterables .concat(new FileLinesCollection(jsapResult.getString("uris"), "UTF-8"), resolved.keySet()); final long numberOfDocuments = vdr.numberOfDocuments(); final TransformationStrategy<CharSequence> transformationStrategy = jsapResult.userSpecified("iso") ? TransformationStrategies.iso() : TransformationStrategies.utf16(); BinIO.storeObject(new URLMPHVirtualDocumentResolver(new SignedRedirectedStringMap(numberOfDocuments, new ShiftAddXorSignedStringMap(allURIs.iterator(), new MWHCFunction.Builder<CharSequence>().keys(allURIs).transform(transformationStrategy) .build(), jsapResult.getInt("width")), resolved.values().toLongArray())), jsapResult.getString("redvdr")); }
From source file:Main.java
public static void parseDocumentByString(String result, DefaultHandler defaultHandler) { try {//from w w w . j a v a2s. c o m SAXParserFactory sf = SAXParserFactory.newInstance(); SAXParser sp = sf.newSAXParser(); sp.parse(new ByteArrayInputStream(result.getBytes("UTF-8")), defaultHandler); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:org.epics.archiverappliance.retrieval.channelarchiver.XMLRPCClient.java
/** * Internal method to make a XML_RPC post call and call the SAX handler on the returned document. * @param serverURL The Server URL/*ww w .jav a 2s . c o m*/ * @param handler DefaultHandler * @param postEntity StringEntity * @throws IOException   * @throws SAXException   */ private static void doHTTPPostAndCallSAXHandler(String serverURL, DefaultHandler handler, StringEntity postEntity) throws IOException, SAXException { logger.debug("Executing doHTTPPostAndCallSAXHandler with the server URL " + serverURL); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost postMethod = new HttpPost(serverURL); postMethod.addHeader("Content-Type", "text/xml"); postMethod.setEntity(postEntity); logger.debug("Executing the HTTP POST" + serverURL); HttpResponse response = httpclient.execute(postMethod); HttpEntity entity = response.getEntity(); if (entity != null) { logger.debug("Obtained a HTTP entity of length " + entity.getContentLength()); try (InputStream is = entity.getContent()) { SAXParserFactory sfac = SAXParserFactory.newInstance(); sfac.setNamespaceAware(false); sfac.setValidating(false); SAXParser parser = sfac.newSAXParser(); parser.parse(is, handler); } catch (ParserConfigurationException pex) { throw new IOException(pex); } } else { throw new IOException("HTTP response did not have an entity associated with it"); } }
From source file:com.microsoft.tfs.core.clients.build.internal.utils.BuildTypeUtil.java
/** * Attempt to parse the specified file for values required to populated the * returned {@link BuildTypeInfo}// w ww . j a v a2s. c om * * @param buildTypeName * name of the build type downloading. * @param localBuildFile * local file to parse. * @param encoding * suggested file encoding to use, passing <code>null</code> will * mean a suggesting encoding of <code>UTF-8</code> will be used. * @return parsed values for the passed file. * @throws IOException * @throws ParserConfigurationException * @throws SAXNotSupportedException * @throws SAXNotRecognizedException */ public static BuildTypeInfo parseBuildTypeInfo(final String buildTypeName, final File localBuildFile, final FileEncoding encoding) throws IOException { final BasicBuildTypeParseHandler handler = new BasicBuildTypeParseHandler(buildTypeName); try { final SAXParser saxParser = SAXUtils.newSAXParser(); saxParser.parse(localBuildFile, handler); } catch (final SAXException e) { // We did our best - log and rethrow any exceptions... final String message = MessageFormat.format( Messages.getString("BuildTypeUtil.SAXExceptionParsingFileFormat"), //$NON-NLS-1$ localBuildFile.getPath(), e.getLocalizedMessage()); log.error(message, e); throw new BuildTypeFileParseException(message, e); } catch (final ParserConfigurationException e) { // We did our best - log and rethrow any exceptions... final String message = MessageFormat.format( Messages.getString("BuildTypeUtil.ParserConfigurationExceptionParsingFileFormat"), //$NON-NLS-1$ localBuildFile.getPath(), e.getLocalizedMessage()); log.error(message, e); throw new BuildTypeFileParseException(message, e); } return handler.getBuildTypeInfo(); }