List of usage examples for org.xml.sax.helpers XMLReaderFactory createXMLReader
public static XMLReader createXMLReader(String className) throws SAXException
From source file:info.magnolia.importexport.DataTransporter.java
/** * Exports the content of the repository, and format it if necessary. * @param stream the stream to write the content to * @param reader the reader to use to parse the xml content (so that we can perform filtering), if null instanciate * a default one/*from w w w .j ava 2 s. c o m*/ * @param repository the repository to export * @param basepath the basepath in the repository * @param session the session to use to export the data from the repository * @param noRecurse * @throws IOException * @throws SAXException * @throws RepositoryException * @throws PathNotFoundException */ public static void parseAndFormat(OutputStream stream, XMLReader reader, String repository, String basepath, Session session, boolean noRecurse) throws IOException, SAXException, PathNotFoundException, RepositoryException { if (reader == null) { reader = XMLReaderFactory.createXMLReader(org.apache.xerces.parsers.SAXParser.class.getName()); } // write to a temp file and then re-read it to remove version history File tempFile = File.createTempFile("export-" + repository + session.getUserID(), ".xml"); OutputStream fileStream = new FileOutputStream(tempFile); try { session.exportSystemView(basepath, fileStream, false, noRecurse); } finally { IOUtils.closeQuietly(fileStream); } readFormatted(reader, tempFile, stream); if (!tempFile.delete()) { log.warn("Could not delete temporary export file {}", tempFile.getAbsolutePath()); } }
From source file:info.magnolia.cms.exchange.simple.SimpleSyndicator.java
/** * @param resourceElement/*from w w w. j av a2 s .co m*/ * @param session * @param content * @param filter * @param activationContent * @throws IOException * @throws RepositoryException */ private void addResources(Element resourceElement, Session session, Content content, Content.ContentFilter filter, ActivationContent activationContent) throws IOException, RepositoryException, SAXException, Exception { File file = File.createTempFile("exchange" + content.getName(), "", Path.getTempDirectory()); GZIPOutputStream gzipOutputStream = new GZIPOutputStream(new FileOutputStream(file)); XMLReader elementfilter; if (content.getWorkspace().getName().equalsIgnoreCase(ContentRepository.VERSION_STORE)) { elementfilter = new FrozenElementFilter( XMLReaderFactory.createXMLReader(org.apache.xerces.parsers.SAXParser.class.getName())); ((FrozenElementFilter) elementfilter).setNodeName(content.getName()); } else { // use default filter elementfilter = new VersionFilter( XMLReaderFactory.createXMLReader(org.apache.xerces.parsers.SAXParser.class.getName())); } /** * nt:file node type has mandatory sub nodes */ if (content.isNodeType(ItemType.NT_FILE)) { DataTransporter.parseAndFormat(gzipOutputStream, elementfilter, "", content.getHandle(), true, false, session); } else { DataTransporter.parseAndFormat(gzipOutputStream, elementfilter, "", content.getJCRNode().getPath(), true, true, session); } IOUtils.closeQuietly(gzipOutputStream); // add file entry in mapping.xml Element element = new Element(RESOURCE_MAPPING_FILE_ELEMENT); element.setAttribute(RESOURCE_MAPPING_NAME_ATTRIBUTE, content.getName()); element.setAttribute(RESOURCE_MAPPING_UUID_ATTRIBUTE, content.getUUID()); element.setAttribute(RESOURCE_MAPPING_ID_ATTRIBUTE, file.getName()); resourceElement.addContent(element); // add this file element as resource in activation content activationContent.addFile(file.getName(), file); Iterator children = content.getChildren(filter).iterator(); while (children.hasNext()) { Content child = (Content) children.next(); this.addResources(element, session, child, filter, activationContent); } }
From source file:org.esco.grouper.parsing.SGSParsingUtil.java
/** * Starts the parsing process./*from w w w .ja va 2 s. c om*/ * @throws SAXException If there is an error during the parsing (invalid group defintion for instance). * @throws IOException If there is an IO error. */ public void parse() throws IOException, SAXException { XMLReader saxReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); saxReader.setFeature("http://xml.org/sax/features/validation", true); saxReader.setContentHandler(this); saxReader.setErrorHandler(this); saxReader.setEntityResolver(this); if (LOGGER.isInfoEnabled()) { LOGGER.info("---------------------------------"); LOGGER.info("Parsing definition file: "); LOGGER.info(definitionsFileURI); LOGGER.info("---------------------------------"); } InputStream iStream = getClass().getClassLoader().getResourceAsStream(definitionsFileURI); if (iStream == null) { LOGGER.fatal("Unable to load (from classpath) file: " + definitionsFileURI + "."); } //final InputSource is = saxReader.parse(new InputSource(iStream)); if (LOGGER.isInfoEnabled()) { LOGGER.info("---------------------------------"); LOGGER.info("Definition file parsed."); LOGGER.info("---------------------------------"); } }
From source file:org.apache.marmotta.ucuenca.wk.provider.gs.GoogleScholarPageProvider.java
@Override public List<String> parseResponse(String resource, String requestUrl, Model triples, InputStream input, String contentType) throws DataRetrievalException { log.debug("Request Successful to {0}", requestUrl); final GSXMLHandler gsXMLHandler = new GSXMLHandler(); gsXMLHandler.clearGSresultList();/*from ww w. ja va2 s . c o m*/ try { XMLReader xr = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser"); xr.setContentHandler(gsXMLHandler); InputSource gsxml = new InputSource(input); gsxml.setEncoding("iso-8859-1"); xr.parse(gsxml); final Set<GSresult> gsresultlist = gsXMLHandler.getGSresultList(); Gson gson = new Gson(); JsonArray json = new JsonArray(); for (GSresult d : gsresultlist) { json.add(gson.toJsonTree(d).getAsJsonObject()); } JSONtoRDF parser = new JSONtoRDF(resource, GoogleScholarProvider.MAPPINGSCHEMA, json, triples); try { parser.parse(); } catch (Exception e) { throw new DataRetrievalException("I/O exception while retrieving resource: " + requestUrl, e); } } catch (SAXException | IOException e) { throw new DataRetrievalException("I/O exception while retrieving resource: " + requestUrl, e); } // try { // List<String> candidates = new ArrayList<String>(); // ValueFactory factory = ValueFactoryImpl.getInstance(); // final Document doc = new SAXBuilder(XMLReaders.NONVALIDATING).build(input); // for(Element element: queryElements(doc, "/result/hits/hit/info/url")) { // String candidate = element.getText(); // triples.add(factory.createStatement(factory.createURI( resource ), FOAF.member, factory.createURI( candidate ) )); // candidates.add(candidate); // } // ClientConfiguration conf = new ClientConfiguration(); // LDClient ldClient = new LDClient(conf); // if(!candidates.isEmpty()) { // Model candidateModel = null; // for(String author: candidates) { // ClientResponse response = ldClient.retrieveResource(author); // Model authorModel = response.getData(); // if(candidateModel == null) { // candidateModel = authorModel; // } else { // candidateModel.addAll(authorModel); // } // } // triples.addAll(candidateModel); // } // }catch (IOException e) { // throw new DataRetrievalException("I/O error while parsing HTML response", e); // }catch (JDOMException e) { // throw new DataRetrievalException("could not parse XML response. It is not in proper XML format", e); // } return Collections.emptyList(); }
From source file:org.apache.marmotta.ucuenca.wk.provider.gs.GoogleScholarProvider.java
@Override public List<String> parseResponse(String resource, String requestUrl, Model triples, InputStream input, String contentType) throws DataRetrievalException { log.debug("Request Successful to {0}", requestUrl); final GSXMLHandler gsXMLHandler = new GSXMLHandler(); gsXMLHandler.clearGSresultList();/*w w w . j a va 2 s.c o m*/ try { XMLReader xr = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser"); xr.setContentHandler(gsXMLHandler); InputSource gsxml = new InputSource(input); gsxml.setEncoding("iso-8859-1"); xr.parse(gsxml); final Set<GSresult> gsresultlist = gsXMLHandler.getGSresultList(); Gson gson = new Gson(); JsonArray json = new JsonArray(); for (GSresult d : gsresultlist) { json.add(gson.toJsonTree(d).getAsJsonObject()); } JSONtoRDF parser = new JSONtoRDF(resource, MAPPINGSCHEMA, json, triples); try { parser.parse(); } catch (Exception e) { throw new DataRetrievalException("I/O exception while retrieving resource: " + requestUrl, e); } int numPages = (int) ((double) (gsXMLHandler.getNumResults() / 10)) + 1; int pagesLoaded = 1; Model model = null; while (pagesLoaded < numPages) { String pagenumquery = Integer.toString(pagesLoaded * 10); String moreDataUrl = String.format(API, pagenumquery, stringSearch, authorSearch, advancedSearch); ClientConfiguration conf = new ClientConfiguration(); LDClient ldClient = new LDClient(conf); ClientResponse response = ldClient.retrieveResource(moreDataUrl); Model pageModel = response.getData(); if (model == null) { model = pageModel; } else { model.addAll(pageModel); } pagesLoaded++; } triples.addAll(model); } catch (SAXException | IOException e) { throw new DataRetrievalException("I/O exception while retrieving resource: " + requestUrl, e); } // try { // List<String> candidates = new ArrayList<String>(); // ValueFactory factory = ValueFactoryImpl.getInstance(); // final Document doc = new SAXBuilder(XMLReaders.NONVALIDATING).build(input); // for(Element element: queryElements(doc, "/result/hits/hit/info/url")) { // String candidate = element.getText(); // triples.add(factory.createStatement(factory.createURI( resource ), FOAF.member, factory.createURI( candidate ) )); // candidates.add(candidate); // } // ClientConfiguration conf = new ClientConfiguration(); // LDClient ldClient = new LDClient(conf); // if(!candidates.isEmpty()) { // Model candidateModel = null; // for(String author: candidates) { // ClientResponse response = ldClient.retrieveResource(author); // Model authorModel = response.getData(); // if(candidateModel == null) { // candidateModel = authorModel; // } else { // candidateModel.addAll(authorModel); // } // } // triples.addAll(candidateModel); // } // }catch (IOException e) { // throw new DataRetrievalException("I/O error while parsing HTML response", e); // }catch (JDOMException e) { // throw new DataRetrievalException("could not parse XML response. It is not in proper XML format", e); // } return Collections.emptyList(); }
From source file:org.dhatim.delivery.AbstractParser.java
protected XMLReader createXMLReader() throws SAXException { XMLReader reader;/*from w ww. j a va2 s.c o m*/ ExecutionContext execContext = getExecContext(); Source source = FilterSource.getSource(execContext); if (saxDriverConfig != null && saxDriverConfig.getResource() != null) { String className = saxDriverConfig.getResource(); reader = XMLReaderFactory.createXMLReader(className); } else if (source instanceof JavaSource) { JavaSource javaSource = (JavaSource) source; if (isFeatureOn(JavaSource.FEATURE_GENERATE_EVENT_STREAM, saxDriverConfig) && !javaSource.isEventStreamRequired()) { throw new SAXException("Invalid Smooks configuration. Feature '" + JavaSource.FEATURE_GENERATE_EVENT_STREAM + "' is explicitly configured 'on' in the Smooks configuration, while the supplied JavaSource has explicitly configured event streaming to be off (through a call to JavaSource.setEventStreamRequired)."); } // Event streaming must be explicitly turned off. If is on as long as it is (a) not configured "off" in // the smooks config (via the reader features) and (b) not turned off via the supplied JavaSource... boolean eventStreamingOn = (!isFeatureOff(JavaSource.FEATURE_GENERATE_EVENT_STREAM, saxDriverConfig) && javaSource.isEventStreamRequired()); if (eventStreamingOn && javaSource.getSourceObjects() != null) { reader = new XStreamXMLReader(); } else { reader = new NullSourceXMLReader(); } } else { reader = XMLReaderFactory.createXMLReader(); } if (reader instanceof SmooksXMLReader) { if (saxDriverConfig != null) { Configurator.configure(reader, saxDriverConfig, execContext.getContext()); } else { Configurator.initialise(reader); } } reader.setFeature("http://xml.org/sax/features/namespaces", true); reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); setHandlers(reader); setFeatures(reader); return reader; }
From source file:org.jlibrary.core.jcr.modules.JCRImportExportModule.java
private byte[] filterForCompatibility(byte[] content) throws RepositoryException { try {//from ww w .j a v a2 s .c o m //logger.info(IOUtils.toString(content)); XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); CompatibilityFilter1_0 filter = new CompatibilityFilter1_0(); filter.setParent(parser); filter.setContentHandler(new XMLWriter(new OutputStreamWriter(baos))); filter.parse(new InputSource(new ByteArrayInputStream(content))); baos.flush(); content = baos.toByteArray(); //logger.info(IOUtils.toString(content)); return content; } catch (Exception e) { throw new RepositoryException(e); } }
From source file:org.lnicholls.galleon.apps.iTunes.PlaylistParser.java
public PlaylistParser(String path) { try {//ww w . j a v a 2s . c om //path = "D:/galleon/iTunes Music Library.xml"; ArrayList currentPlaylists = new ArrayList(); // Read all tracks XMLReader trackReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); TrackParser trackParser = new TrackParser(); trackReader.setContentHandler(trackParser); trackReader.setErrorHandler(trackParser); trackReader.setFeature("http://xml.org/sax/features/validation", false); File file = new File(path); if (file.exists()) { InputStream inputStream = Tools.getInputStream(file); trackReader.parse(new InputSource(inputStream)); inputStream.close(); } // Read all playlists XMLReader listReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); ListParser listParser = new ListParser(currentPlaylists); listReader.setContentHandler(listParser); listReader.setErrorHandler(listParser); listReader.setFeature("http://xml.org/sax/features/validation", false); file = new File(path); if (file.exists()) { InputStream inputStream = Tools.getInputStream(file); listReader.parse(new InputSource(inputStream)); inputStream.close(); } // Remove old playlists List list = PlaylistsManager.listAll(); if (list != null && list.size() > 0) { Iterator playlistIterator = list.iterator(); while (playlistIterator.hasNext()) { Playlists playlist = (Playlists) playlistIterator.next(); boolean found = false; Iterator iterator = currentPlaylists.iterator(); while (iterator.hasNext()) { String externalId = (String) iterator.next(); if (externalId.equals(playlist.getExternalId())) { found = true; break; } } if (!found) { PlaylistsManager.deletePlaylistsTracks(playlist); PlaylistsManager.deletePlaylists(playlist); log.debug("Removed playlist: " + playlist.getTitle()); } } list.clear(); } currentPlaylists.clear(); } catch (IOException ex) { Tools.logException(PlaylistParser.class, ex); } catch (SAXException ex) { Tools.logException(PlaylistParser.class, ex); } catch (Exception ex) { Tools.logException(PlaylistParser.class, ex); } }
From source file:org.opencms.xml.CmsXmlUtils.java
/** * Validates the structure of a XML document contained in a byte array * with the DTD or XML schema used by the document.<p> * //from ww w. j a v a 2 s . co m * @param xmlStream a source providing a XML document that should be validated * @param resolver the XML entity resolver to use * * @throws CmsXmlException if the validation fails */ public static void validateXmlStructure(InputStream xmlStream, EntityResolver resolver) throws CmsXmlException { XMLReader reader; try { reader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); } catch (SAXException e) { // xerces parser not available - no schema validation possible if (LOG.isWarnEnabled()) { LOG.warn(Messages.get().getBundle().key(Messages.LOG_VALIDATION_INIT_XERXES_SAX_READER_FAILED_0), e); } // no validation of the content is possible return; } // turn on validation try { reader.setFeature("http://xml.org/sax/features/validation", true); // turn on schema validation reader.setFeature("http://apache.org/xml/features/validation/schema", true); // configure namespace support reader.setFeature("http://xml.org/sax/features/namespaces", true); reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false); } catch (SAXNotRecognizedException e) { // should not happen as Xerces 2 support this feature if (LOG.isWarnEnabled()) { LOG.warn(Messages.get().getBundle().key(Messages.LOG_SAX_READER_FEATURE_NOT_RECOGNIZED_0), e); } // no validation of the content is possible return; } catch (SAXNotSupportedException e) { // should not happen as Xerces 2 support this feature if (LOG.isWarnEnabled()) { LOG.warn(Messages.get().getBundle().key(Messages.LOG_SAX_READER_FEATURE_NOT_SUPPORTED_0), e); } // no validation of the content is possible return; } // add an error handler which turns any errors into XML CmsXmlValidationErrorHandler errorHandler = new CmsXmlValidationErrorHandler(); reader.setErrorHandler(errorHandler); if (resolver != null) { // set the resolver for the "opencms://" URIs reader.setEntityResolver(resolver); } try { reader.parse(new InputSource(xmlStream)); } catch (IOException e) { // should not happen since we read form a byte array if (LOG.isErrorEnabled()) { LOG.error(Messages.get().getBundle().key(Messages.LOG_READ_XML_FROM_BYTE_ARR_FAILED_0), e); } return; } catch (SAXException e) { // should not happen since all errors are handled in the XML error handler if (LOG.isErrorEnabled()) { LOG.error(Messages.get().getBundle().key(Messages.LOG_PARSE_SAX_EXC_0), e); } return; } if (errorHandler.getErrors().elements().size() > 0) { // there was at last one validation error, so throw an exception StringWriter out = new StringWriter(256); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(out, format); try { writer.write(errorHandler.getErrors()); writer.write(errorHandler.getWarnings()); writer.close(); } catch (IOException e) { // should not happen since we write to a StringWriter if (LOG.isErrorEnabled()) { LOG.error(Messages.get().getBundle().key(Messages.LOG_STRINGWRITER_IO_EXC_0), e); } } // generate String from XML for display of document in error message throw new CmsXmlException(Messages.get().container(Messages.ERR_XML_VALIDATION_1, out.toString())); } }
From source file:org.portletbridge.portlet.PortletBridgePortlet.java
/** * @return// w ww. j av a 2 s . c o m * @throws PortletException * @throws IllegalAccessException * @throws */ protected BridgeViewPortlet createViewPortlet(ResourceBundle resourceBundle, TemplateFactory templateFactory) throws PortletException { PortletConfig config = this.getPortletConfig(); // get the memento session key String mementoSessionKey = config.getInitParameter("mementoSessionKey"); if (mementoSessionKey == null) { throw new PortletException(resourceBundle.getString("error.mementoSessionKey")); } // get the servlet name String servletName = config.getInitParameter("servletName"); if (servletName == null) { throw new PortletException(resourceBundle.getString("error.servletName")); } // get parserClassName String parserClassName = config.getInitParameter("parserClassName"); if (parserClassName == null) { throw new PortletException(resourceBundle.getString("error.parserClassName")); } // get authenticatorClassName String authenticatorClassName = config.getInitParameter("authenticatorClassName"); if (authenticatorClassName == null) { throw new PortletException(resourceBundle.getString("error.authenticatorClassName")); } BridgeAuthenticator bridgeAuthenticator = null; try { bridgeAuthenticator = PortletBridgeObjects.createBridgeAuthenticator(authenticatorClassName); } catch (Exception e) { if (log.isWarnEnabled()) { log.warn( "Problem constructing BridgeAuthenticator instance, returning instance of DefaultBridgeAuthenticator", e); } bridgeAuthenticator = new DefaultBridgeAuthenticator(); } // try { // Class authenticatorClass = Class.forName(authenticatorClassName); // bridgeAuthenticator = (BridgeAuthenticator) authenticatorClass.newInstance(); // } catch (ClassNotFoundException e) { // log.warn(e, e); // throw new PortletException(resourceBundle // .getString("error.authenticator")); // } catch (InstantiationException e) { // log.warn(e, e); // throw new PortletException(resourceBundle // .getString("error.authenticator")); // } catch (IllegalAccessException e) { // log.warn(e, e); // throw new PortletException(resourceBundle // .getString("error.authenticator")); // } String initUrlFactoryClassName = config.getInitParameter("initUrlFactoryClassName"); InitUrlFactory initUrlFactory = null; if (initUrlFactoryClassName != null && initUrlFactoryClassName.trim().length() > 0) { try { initUrlFactory = PortletBridgeObjects.createInitUrlFactory(initUrlFactoryClassName); } catch (Exception e) { if (log.isWarnEnabled()) { log.warn( "Problem constructing InitUrlFactory instance, returning instance of DefaultInitUrlFactory", e); } initUrlFactory = new DefaultInitUrlFactory(); } } String idParamKey = config.getInitParameter("idParamKey"); // setup parser BridgeTransformer transformer = null; try { String cssRegex = config.getInitParameter("cssRegex"); String javascriptRegex = config.getInitParameter("jsRegex"); XMLReader parser = XMLReaderFactory.createXMLReader(parserClassName); for (Enumeration names = config.getInitParameterNames(); names.hasMoreElements();) { String name = (String) names.nextElement(); if (name.startsWith("parserFeature-")) { parser.setFeature(name.substring("parserFeature-".length()), "true".equalsIgnoreCase(config.getInitParameter(name))); } else if (name.startsWith("parserProperty-")) { parser.setProperty(name.substring("parserProperty-".length()), config.getInitParameter(name)); } } IdGenerator idGenerator = new DefaultIdGenerator(); ContentRewriter javascriptRewriter = new RegexContentRewriter(javascriptRegex); ContentRewriter cssRewriter = new RegexContentRewriter(cssRegex); BridgeFunctionsFactory bridgeFunctionsFactory = new BridgeFunctionsFactory(idGenerator, javascriptRewriter, cssRewriter); transformer = new AltBridgeTransformer(bridgeFunctionsFactory, templateFactory, parser, servletName); } catch (SAXNotRecognizedException e) { throw new PortletException(e); } catch (SAXNotSupportedException e) { throw new PortletException(e); } catch (SAXException e) { throw new PortletException(e); } BridgeViewPortlet bridgeViewPortlet = new BridgeViewPortlet(); bridgeViewPortlet.setInitUrlFactory(initUrlFactory); bridgeViewPortlet.setHttpClientTemplate(new DefaultHttpClientTemplate()); bridgeViewPortlet.setTransformer(transformer); bridgeViewPortlet.setMementoSessionKey(mementoSessionKey); bridgeViewPortlet.setBridgeAuthenticator(bridgeAuthenticator); if (idParamKey != null) { bridgeViewPortlet.setIdParamKey(idParamKey); } return bridgeViewPortlet; }