List of usage examples for org.xml.sax.helpers XMLReaderFactory createXMLReader
public static XMLReader createXMLReader() throws SAXException
From source file:com.atguigu.bibiq.gsyvideoplay.BiliDanmukuParser.java
@Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try {/* ww w .j a v a2 s .c om*/ XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; }
From source file:com.bradmcevoy.http.webdav.DefaultPropFindRequestFieldParser.java
@Override public PropertiesRequest getRequestedFields(InputStream in) { try {//from w w w . j a va 2 s. c o m final Set<QName> set = new LinkedHashSet<QName>(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); StreamUtils.readTo(in, bout, false, true); byte[] arr = bout.toByteArray(); if (arr.length > 1) { ByteArrayInputStream bin = new ByteArrayInputStream(arr); XMLReader reader = XMLReaderFactory.createXMLReader(); PropFindSaxHandler handler = new PropFindSaxHandler(); reader.setContentHandler(handler); try { reader.parse(new InputSource(bin)); if (handler.isAllProp()) { return new PropertiesRequest(); } else { set.addAll(handler.getAttributes().keySet()); } } catch (IOException e) { log.warn("exception parsing request body", e); // ignore } catch (SAXException e) { log.warn("exception parsing request body", e); // ignore } } return PropertiesRequest.toProperties(set); } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:io.milton.http.webdav.DefaultPropFindRequestFieldParser.java
@Override public PropertiesRequest getRequestedFields(InputStream in) { final Set<QName> set = new LinkedHashSet<QName>(); try {/*from ww w. ja v a 2s .c om*/ ByteArrayOutputStream bout = new ByteArrayOutputStream(); StreamUtils.readTo(in, bout, false, true); byte[] arr = bout.toByteArray(); if (arr.length > 1) { ByteArrayInputStream bin = new ByteArrayInputStream(arr); XMLReader reader = XMLReaderFactory.createXMLReader(); reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false); PropFindSaxHandler handler = new PropFindSaxHandler(); reader.setContentHandler(handler); try { reader.parse(new InputSource(bin)); if (handler.isAllProp()) { return new PropertiesRequest(); } else { set.addAll(handler.getAttributes().keySet()); } } catch (IOException e) { log.warn("exception parsing request body", e); // ignore } catch (SAXException e) { log.warn("exception parsing request body", e); // ignore } } } catch (Exception ex) { // There's a report of an exception being thrown here by IT Hit Webdav client // Perhaps we can just log the error and return an empty set. Usually this // class is wrapped by the MsPropFindRequestFieldParser which will use a default // set of properties if this returns an empty set log.warn("Exception parsing PROPFIND request fields. Returning empty property set", ex); //throw new RuntimeException( ex ); } return PropertiesRequest.toProperties(set); }
From source file:de.tbuchloh.kiskis.persistence.exporter.XMLTransformer.java
/** * @param stylesheet/*from w ww .jav a 2 s .c om*/ * is the stylesheet to use. * @param is * is the input source. * @param os * is the output sink. */ public void transform(final URL stylesheet, final InputStream is, final OutputStream os) throws TransformException { try { LOG.debug("transforming with stylesheet=" + stylesheet); final XMLReader reader = XMLReaderFactory.createXMLReader(); reader.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(final String publicId, final String systemId) throws SAXException, IOException { if (systemId == null) { throw new IllegalArgumentException("systemId must not be null!"); } final int pos = systemId.lastIndexOf(File.separator); String resource = systemId; if (pos >= 0) { resource = resource.substring(pos + 1); } final URL dtd = ClassLoader.getSystemResource(resource); LOG.debug("Resolving " + "systemId=" + systemId + ", resource=" + resource + ", dtd=" + dtd); return new InputSource(dtd.openStream()); } }); try { final Builder builder = new Builder(reader, false); final Document s = builder.build(stylesheet.openStream()); final Document source = builder.build(is); final XSLTransform transform = new XSLTransform(s); final Document target = XSLTransform.toDocument(transform.transform(source)); final Serializer ser = new Serializer(os); ser.setIndent(2); ser.write(target); } catch (final Exception e) { LOG.error(e, e); throw new XMLException(e.getMessage()); } } catch (final Exception e) { throw new TransformException(e.getMessage(), e); } }
From source file:com.bradmcevoy.http.webdav.DefaultPropPatchParser.java
private ParseResult parseContent(byte[] arr) throws IOException, SAXException { if (arr.length > 0) { log.debug("processing content"); ByteArrayInputStream bin = new ByteArrayInputStream(arr); XMLReader reader = XMLReaderFactory.createXMLReader(); PropPatchSaxHandler handler = new PropPatchSaxHandler(); reader.setContentHandler(handler); reader.parse(new InputSource(bin)); log.debug("toset: " + handler.getAttributesToSet().size()); return new ParseResult(handler.getAttributesToSet(), handler.getAttributesToRemove().keySet()); } else {/*from w ww . j a va2 s. c o m*/ log.debug("empty content"); return new ParseResult(new HashMap<QName, String>(), new HashSet<QName>()); } }
From source file:net.i2cat.netconf.transport.SSHTransport.java
public SSHTransport() { listeners = new Vector<TransportListener>(); xmlHandler = new TransportContentParser(); // populate//from w w w . j av a 2 s .c om try { parser = XMLReaderFactory.createXMLReader(); parser.setContentHandler(xmlHandler); parser.setErrorHandler(xmlHandler); } catch (SAXException e) { log.error(e.getMessage()); } }
From source file:SAXLister.java
public SAXLister(String[] args) throws SAXException, IOException { XMLReader parser = XMLReaderFactory.createXMLReader(); // should load properties rather than hardcoding class name parser.setContentHandler(new PeopleHandler()); parser.parse(args.length == 1 ? args[0] : "people.xml"); }
From source file:io.milton.http.webdav.DefaultPropPatchParser.java
private PropPatchParseResult parseContent(byte[] arr) throws IOException, SAXException { if (arr.length > 0) { log.debug("processing content"); ByteArrayInputStream bin = new ByteArrayInputStream(arr); XMLReader reader = XMLReaderFactory.createXMLReader(); reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false); PropPatchSaxHandler handler = new PropPatchSaxHandler(); reader.setContentHandler(handler); reader.parse(new InputSource(bin)); log.debug("toset: " + handler.getAttributesToSet().size()); return new PropPatchParseResult(handler.getAttributesToSet(), handler.getAttributesToRemove().keySet()); } else {/* w w w.j a va 2 s . c om*/ log.debug("empty content"); return new PropPatchParseResult(new HashMap<QName, String>(), new HashSet<QName>()); } }
From source file:codeswarm.XMLQueueLoader.java
public void run() { XMLReader reader = null;/* w ww . j a va2 s . c o m*/ try { reader = XMLReaderFactory.createXMLReader(); } catch (SAXException e) { logger.error("Couldn't find/create an XML SAX Reader", e); System.exit(1); } reader.setContentHandler(new DefaultHandler() { public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException { if (name.equals("event")) { String eventFilename = atts.getValue("filename"); String eventDatestr = atts.getValue("date"); long eventDate = Long.parseLong(eventDatestr); String eventWeightStr = atts.getValue("weight"); int eventWeight = 1; if (eventWeightStr != null) { eventWeight = Integer.parseInt(eventWeightStr); } //It's difficult for the user to tell that they're missing events, //so we should crash in this case if (isXMLSorted) { if (eventDate < maximumDateSeenSoFar) { logger.error( "Input not sorted, you must set IsInputSorted to false in your config file"); System.exit(1); } else maximumDateSeenSoFar = eventDate; } String eventAuthor = atts.getValue("author"); // int eventLinesAdded = atts.getValue( "linesadded" ); // int eventLinesRemoved = atts.getValue( "linesremoved" ); FileEvent evt = new FileEvent(eventDate, eventAuthor, "", eventFilename, eventWeight); try { queue.put(evt); fireEventAddedEvent(); } catch (InterruptedException e) { // TODO Auto-generated catch block logger.error("Interrupted while trying to put into eventsQueue", e); System.exit(1); } } } /* * (non-Javadoc) * @see org.xml.sax.helpers.DefaultHandler#endDocument() * * Notify any listeners that the Document has finished parsing. */ public void endDocument() { fireTaskDoneEvent(); } }); try { reader.parse(fullFilename); } catch (Exception e) { logger.error("Error parsing xml:", e); System.exit(1); } }
From source file:net.sf.janos.model.xml.ResultParser.java
public static Map<AVTransportEventHandler.AVTransportEventType, String> parseAVTransportEvent(String xml) throws SAXException { XMLReader reader = XMLReaderFactory.createXMLReader(); AVTransportEventHandler handler = new AVTransportEventHandler(); reader.setContentHandler(handler);/*from w w w . j ava2 s .c om*/ try { reader.parse(new InputSource(new StringReader(xml))); } catch (IOException e) { // This should never happen - we're not performing I/O! LOG.error("Could not parse AV Transport Event: ", e); } return handler.getChanges(); }