List of usage examples for javax.xml.parsers SAXParser getXMLReader
public abstract org.xml.sax.XMLReader getXMLReader() throws SAXException;
From source file:org.castor.xmlctf.xmldiff.xml.XMLFileReader.java
/** * Creates a new XMLReader for the given URILocation. * * @param filename the URILocation to create this reader for. *//*from w w w. java 2s .c o m*/ public XMLFileReader(final String filename) { if (filename == null) { throw new IllegalArgumentException("You must give a non-null fliename"); } _file = new File(filename); if (!_file.exists()) { throw new IllegalArgumentException("File '" + filename + "' does not exist"); } _location = getUrlFromFile(); SAXParser saxParser = XMLParserUtils.getSAXParser(false, true); try { _parser = saxParser.getXMLReader(); } catch (org.xml.sax.SAXException sx) { LOG.error(Messages.format("conf.configurationError", sx)); } if (_parser == null) { _parser = XMLParserUtils.instantiateXMLReader("org.apache.xerces.parsers.SAXParser"); } }
From source file:org.chiba.xml.xforms.connector.SchemaValidator.java
/** * validate the instance according to the schema specified on the model * * @return false if the instance is not valid *//*from ww w . j ava 2s . c o m*/ public boolean validateSchema(Model model, Node instance) throws XFormsException { boolean valid = true; String message; if (LOGGER.isDebugEnabled()) LOGGER.debug("SchemaValidator.validateSchema: validating instance"); //needed if we want to load schemas from Model + set it as "schemaLocation" attribute String schemas = model.getElement().getAttributeNS(NamespaceConstants.XFORMS_NS, "schema"); if (schemas != null && !schemas.equals("")) { // valid=false; //add schemas to element //shouldn't it be done on a copy of the doc ? Element el = null; if (instance.getNodeType() == Node.ELEMENT_NODE) el = (Element) instance; else if (instance.getNodeType() == Node.DOCUMENT_NODE) el = ((Document) instance).getDocumentElement(); else { if (LOGGER.isDebugEnabled()) LOGGER.debug("instance node type is: " + instance.getNodeType()); } String prefix = NamespaceResolver.getPrefix(el, XMLSCHEMA_INSTANCE_NS); //test if with targetNamespace or not //if more than one schema : namespaces are mandatory ! (optional only for 1) StringTokenizer tokenizer = new StringTokenizer(schemas, " ", false); String schemaLocations = null; String noNamespaceSchemaLocation = null; while (tokenizer.hasMoreElements()) { String token = (String) tokenizer.nextElement(); //check that it is an URL URI uri = null; try { uri = new java.net.URI(token); } catch (java.net.URISyntaxException ex) { if (LOGGER.isDebugEnabled()) LOGGER.debug(token + " is not an URI"); } if (uri != null) { String ns; try { ns = this.getSchemaNamespace(uri); if (ns != null && !ns.equals("")) { if (schemaLocations == null) schemaLocations = ns + " " + token; else schemaLocations = schemaLocations + " " + ns + " " + token; ///add the namespace declaration if it is not on the instance? //TODO: how to know with which prefix ? String nsPrefix = NamespaceResolver.getPrefix(el, ns); if (nsPrefix == null) { //namespace not declared ! LOGGER.warn("SchemaValidator: targetNamespace " + ns + " of schema " + token + " is not declared in instance: declaring it as default..."); el.setAttributeNS(NamespaceConstants.XMLNS_NS, NamespaceConstants.XMLNS_PREFIX, ns); } } else if (noNamespaceSchemaLocation == null) noNamespaceSchemaLocation = token; else { //we have more than one schema without namespace LOGGER.warn("SchemaValidator: There is more than one schema without namespace !"); } } catch (Exception ex) { LOGGER.warn( "Exception while trying to load schema: " + uri.toString() + ": " + ex.getMessage(), ex); //in case there was an exception: do nothing, do not set the schema } } } //write schemaLocations found if (schemaLocations != null && !schemaLocations.equals("")) el.setAttributeNS(XMLSCHEMA_INSTANCE_NS, prefix + ":schemaLocation", schemaLocations); if (noNamespaceSchemaLocation != null) el.setAttributeNS(XMLSCHEMA_INSTANCE_NS, prefix + ":noNamespaceSchemaLocation", noNamespaceSchemaLocation); //save and parse the doc ValidationErrorHandler handler = null; File f; try { //save document f = File.createTempFile("instance", ".xml"); f.deleteOnExit(); TransformerFactory trFact = TransformerFactory.newInstance(); Transformer trans = trFact.newTransformer(); DOMSource source = new DOMSource(el); StreamResult result = new StreamResult(f); trans.transform(source, result); if (LOGGER.isDebugEnabled()) LOGGER.debug("Validator.validateSchema: file temporarily saved in " + f.getAbsolutePath()); //parse it with error handler to validate it handler = new ValidationErrorHandler(); SAXParserFactory parserFact = SAXParserFactory.newInstance(); parserFact.setValidating(true); parserFact.setNamespaceAware(true); SAXParser parser = parserFact.newSAXParser(); XMLReader reader = parser.getXMLReader(); //validation activated reader.setFeature("http://xml.org/sax/features/validation", true); //schema validation activated reader.setFeature("http://apache.org/xml/features/validation/schema", true); //used only to validate the schema, not the instance //reader.setFeature( "http://apache.org/xml/features/validation/schema-full-checking", true); //validate only if there is a grammar reader.setFeature("http://apache.org/xml/features/validation/dynamic", true); parser.parse(f, handler); } catch (Exception ex) { LOGGER.warn("Validator.validateSchema: Exception in XMLSchema validation: " + ex.getMessage(), ex); //throw new XFormsException("XMLSchema validation failed. "+message); } //if no exception if (handler != null && handler.isValid()) valid = true; else { message = handler.getMessage(); //TODO: find a way to get the error message displayed throw new XFormsException("XMLSchema validation failed. " + message); } if (LOGGER.isDebugEnabled()) LOGGER.debug("Validator.validateSchema: result=" + valid); } return valid; }
From source file:org.corpus_tools.pepper.core.PepperJobImpl.java
/** * {@inheritDoc PepperJob#load(URI)}/*from www . ja v a2 s . c o m*/ */ @Override public void load(URI uri) { if (uri.isFile()) { File wdFile = new File(uri.toFileString()); // set folder containing workflow description as base dir setBaseDir(uri.trimSegments(1)); SAXParser parser; XMLReader xmlReader; SAXParserFactory factory = SAXParserFactory.newInstance(); WorkflowDescriptionReader contentHandler = new WorkflowDescriptionReader(); contentHandler.setPepperJob(this); contentHandler.setLocation(uri); // remove all existing steps clear(); try { parser = factory.newSAXParser(); xmlReader = parser.getXMLReader(); xmlReader.setContentHandler(contentHandler); } catch (ParserConfigurationException e) { throw new PepperModuleXMLResourceException("Cannot load Pepper workflow description file '" + wdFile.getAbsolutePath() + "': " + e.getMessage() + ". ", e); } catch (Exception e) { throw new PepperModuleXMLResourceException("Cannot load Pepper workflow description file '" + wdFile.getAbsolutePath() + "': " + e.getMessage() + ". ", e); } try { InputStream inputStream = new FileInputStream(wdFile); Reader reader = new InputStreamReader(inputStream, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); xmlReader.parse(is); } catch (SAXException e) { try { parser = factory.newSAXParser(); xmlReader = parser.getXMLReader(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(wdFile.getAbsolutePath()); } catch (Exception e1) { throw new PepperModuleXMLResourceException("Cannot load Pepper workflow description file '" + wdFile.getAbsolutePath() + "': " + e1.getMessage() + ". ", e1); } } catch (Exception e) { if (e instanceof PepperModuleException) { throw (PepperModuleException) e; } else { throw new PepperModuleXMLResourceException("Cannot load Pepper workflow description file'" + wdFile + "', because of a nested exception: " + e.getMessage() + ". ", e); } } } else { throw new UnsupportedOperationException( "Currently Pepper can only load workflow description from local files."); } }
From source file:org.corpus_tools.salt.util.SaltUtil.java
/** * Loads a list of root objects coming from a SaltXML (.{@link #FILE_ENDING_SALT_XML}) * and returns it.// ww w. j av a 2s .c o m * * @param objectURI * {@link URI} to SaltXML file containing the object * @return loaded objects */ public static List<Object> loadObjects(URI location) { if (location == null) { throw new SaltResourceException("Cannot load Salt object, because the given uri is null."); } File objectFile = new File( (location.toFileString() == null) ? location.toString() : location.toFileString()); if (!objectFile.exists()) { throw new SaltResourceException("Cannot load Salt object, because the file '" + objectFile.getAbsolutePath() + "' does not exist."); } SAXParser parser; XMLReader xmlReader; SAXParserFactory factory = SAXParserFactory.newInstance(); SaltXML10Handler contentHandler = new SaltXML10Handler(); try { parser = factory.newSAXParser(); xmlReader = parser.getXMLReader(); xmlReader.setContentHandler(contentHandler); } catch (ParserConfigurationException e) { throw new SaltResourceException( "Cannot load Salt object from file '" + objectFile.getAbsolutePath() + "'.", e); } catch (Exception e) { throw new SaltResourceException( "Cannot load Salt object from file '" + objectFile.getAbsolutePath() + "'.", e); } try { InputStream inputStream = new FileInputStream(objectFile); Reader reader = new InputStreamReader(inputStream, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); xmlReader.parse(is); } catch (SAXException e) { try { parser = factory.newSAXParser(); xmlReader = parser.getXMLReader(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(objectFile.getAbsolutePath()); } catch (Exception e1) { throw new SaltResourceException( "Cannot load Salt object from file '" + objectFile.getAbsolutePath() + "'.", e1); } } catch (Exception e) { if (e instanceof SaltException) { throw (SaltException) e; } else { throw new SaltResourceException( "Cannot load Salt object from file'" + objectFile + "', because of a nested exception. ", e); } } return contentHandler.getRootObjects(); }
From source file:org.dspace.content.authority.LCNameAuthority.java
/** * Guts of the implementation, returns a complete Choices result, or * null for a failure.//from www . j a v a 2s. com */ private Choices queryPerson(String text, int start, int limit) { // punt if there is no query text if (text == null || text.trim().length() == 0) { return new Choices(true); } // 1. build CQL query DCPersonName pn = new DCPersonName(text); StringBuilder query = new StringBuilder(); query.append("local.FirstName = \"").append(pn.getFirstNames()).append("\" and local.FamilyName = \"") .append(pn.getLastName()).append("\""); // XXX arbitrary default limit - should be configurable? if (limit == 0) { limit = 50; } NameValuePair args[] = new NameValuePair[6]; args[0] = new NameValuePair("operation", "searchRetrieve"); args[1] = new NameValuePair("version", "1.1"); args[2] = new NameValuePair("recordSchema", "info:srw/schema/1/marcxml-v1.1"); args[3] = new NameValuePair("query", query.toString()); args[4] = new NameValuePair("maximumRecords", String.valueOf(limit)); args[5] = new NameValuePair("startRecord", String.valueOf(start + 1)); HttpClient hc = new HttpClient(); String srUrl = url + "?" + EncodingUtil.formUrlEncode(args, "UTF8"); GetMethod get = new GetMethod(srUrl); log.debug("Trying SRU query, URL=" + srUrl); // 2. web request try { int status = hc.executeMethod(get); if (status == 200) { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); SRUHandler handler = new SRUHandler(); // XXX FIXME: should turn off validation here explicitly, but // it seems to be off by default. xr.setFeature("http://xml.org/sax/features/namespaces", true); xr.setContentHandler(handler); xr.setErrorHandler(handler); xr.parse(new InputSource(get.getResponseBodyAsStream())); // this probably just means more results available.. if (handler.hits != handler.result.size()) { log.warn("Discrepency in results, result.length=" + handler.result.size() + ", yet expected results=" + handler.hits); } boolean more = handler.hits > (start + handler.result.size()); // XXX add non-auth option; perhaps the UI should do this? // XXX it's really a policy matter if they allow unauth result. // XXX good, stop it. // handler.result.add(new Choice("", text, "Non-Authority: \""+text+"\"")); int confidence; if (handler.hits == 0) { confidence = Choices.CF_NOTFOUND; } else if (handler.hits == 1) { confidence = Choices.CF_UNCERTAIN; } else { confidence = Choices.CF_AMBIGUOUS; } return new Choices(handler.result.toArray(new Choice[handler.result.size()]), start, handler.hits, confidence, more); } } catch (HttpException e) { log.error("SRU query failed: ", e); return new Choices(true); } catch (IOException e) { log.error("SRU query failed: ", e); return new Choices(true); } catch (ParserConfigurationException e) { log.warn("Failed parsing SRU result: ", e); return new Choices(true); } catch (SAXException e) { log.warn("Failed parsing SRU result: ", e); return new Choices(true); } finally { get.releaseConnection(); } return new Choices(true); }
From source file:org.dspace.content.authority.SHERPARoMEOProtocol.java
protected Choices query(String result, String label, String authority, NameValuePair[] args, int start, int limit) { HttpClient hc = new HttpClient(); String srUrl = url + "?" + EncodingUtil.formUrlEncode(args, "UTF8"); GetMethod get = new GetMethod(srUrl); log.debug("Trying SHERPA/RoMEO Query, URL=" + srUrl); try {//from w w w. j a v a2s . co m int status = hc.executeMethod(get); if (status == 200) { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); SRHandler handler = new SRHandler(result, label, authority); // XXX FIXME: should turn off validation here explicitly, but // it seems to be off by default. xr.setFeature("http://xml.org/sax/features/namespaces", true); xr.setContentHandler(handler); xr.setErrorHandler(handler); xr.parse(new InputSource(get.getResponseBodyAsStream())); int confidence; if (handler.total == 0) { confidence = Choices.CF_NOTFOUND; } else if (handler.total == 1) { confidence = Choices.CF_UNCERTAIN; } else { confidence = Choices.CF_AMBIGUOUS; } return new Choices(handler.result, start, handler.total, confidence, false); } } catch (HttpException e) { log.error("SHERPA/RoMEO query failed: ", e); return null; } catch (IOException e) { log.error("SHERPA/RoMEO query failed: ", e); return null; } catch (ParserConfigurationException e) { log.warn("Failed parsing SHERPA/RoMEO result: ", e); return null; } catch (SAXException e) { log.warn("Failed parsing SHERPA/RoMEO result: ", e); return null; } finally { get.releaseConnection(); } return null; }
From source file:org.dspace.content.authority.SkylightAPIProtocol.java
protected Choices query(String result, String label, String authority, String args, int start, int limit) throws UnsupportedEncodingException { String encodedArgs = new String(args.getBytes("UTF-8"), "UTF-8"); HttpClient hc = new HttpClient(); String srUrl = url + "/search/" + encodedArgs + ".xml?field=" + label; GetMethod get = new GetMethod(srUrl); log.info("Trying Skylight Query, URL=" + srUrl); try {//from w w w .j a v a2 s . c om int status = hc.executeMethod(get); log.info("Status" + status); if (status == 200) { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); SRHandler handler = new SRHandler(result, label, authority); // XXX FIXME: should turn off validation here explicitly, but // it seems to be off by default. xr.setFeature("http://xml.org/sax/features/namespaces", true); xr.setContentHandler(handler); xr.setErrorHandler(handler); InputSource src = new InputSource(get.getResponseBodyAsStream()); log.info("Source from sklightui :: " + src.toString()); xr.parse(src); int confidence; if (handler.total == 0) { confidence = Choices.CF_NOTFOUND; } else if (handler.total == 1) { confidence = Choices.CF_UNCERTAIN; } else { confidence = Choices.CF_AMBIGUOUS; } return new Choices(handler.result, start, handler.total, confidence, false); } } catch (HttpException e) { log.error("Skylight query failed: ", e); return null; } catch (IOException e) { log.error("Skylight query failed: ", e); return null; } catch (ParserConfigurationException e) { log.warn("Failed parsing SHERPA/RoMEO result: ", e); return null; } catch (SAXException e) { log.warn("Failed parsing Skylight result: ", e); return null; } finally { get.releaseConnection(); } return null; }
From source file:org.exist.http.SOAPServer.java
/** * Builds an XML Document from a string representation * //from ww w . j a v a2s . c om * @param buf The XML Document content * * @return DOM XML Document */ private Document BuildXMLDocument(byte[] buf) throws SAXException, ParserConfigurationException, IOException { //try and construct xml document from input stream, we use eXist's in-memory DOM implementation final SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); //TODO we should be able to cope with context.getBaseURI() final InputSource src = new InputSource(new ByteArrayInputStream(buf)); final SAXParser parser = factory.newSAXParser(); final XMLReader reader = parser.getXMLReader(); final SAXAdapter adapter = new SAXAdapter(); reader.setContentHandler(adapter); reader.setContentHandler(adapter); reader.parse(src); //return receiver.getDocument(); return adapter.getDocument(); }
From source file:org.exist.mongodb.xquery.gridfs.Get.java
/** * Parse an byte-array containing (compressed) XML data into an eXist-db * document.//ww w . j a v a2 s . c o m * * @param data Byte array containing the XML data. * @return Sequence containing the XML as DocumentImpl * * @throws XPathException Something bad happened. */ private Sequence processXML(XQueryContext xqueryContext, InputStream is) throws XPathException { Sequence content = null; try { final ValidationReport validationReport = new ValidationReport(); final SAXAdapter adapter = new SAXAdapter(xqueryContext); final SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); final InputSource src = new InputSource(is); final SAXParser parser = factory.newSAXParser(); XMLReader xr = parser.getXMLReader(); xr.setErrorHandler(validationReport); xr.setContentHandler(adapter); xr.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter); xr.parse(src); // Cleanup IOUtils.closeQuietly(is); if (validationReport.isValid()) { content = (DocumentImpl) adapter.getDocument(); } else { String txt = String.format("Received document is not valid: %s", validationReport.toString()); LOG.debug(txt); throw new XPathException(txt); } } catch (SAXException | ParserConfigurationException | IOException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(ex.getMessage()); } return content; }
From source file:org.exist.util.XMLReaderObjectFactory.java
/** * Create Xmlreader and setup validation *//* ww w . j ava2s . co m*/ public static XMLReader createXmlReader(VALIDATION_SETTING validation, GrammarPool grammarPool, eXistXMLCatalogResolver resolver) throws ParserConfigurationException, SAXException { // Create a xmlreader final SAXParserFactory saxFactory = ExistSAXParserFactory.getSAXParserFactory(); if (validation == VALIDATION_SETTING.AUTO || validation == VALIDATION_SETTING.ENABLED) { saxFactory.setValidating(true); } else { saxFactory.setValidating(false); } saxFactory.setNamespaceAware(true); final SAXParser saxParser = saxFactory.newSAXParser(); final XMLReader xmlReader = saxParser.getXMLReader(); // Setup grammar cache if (grammarPool != null) { setReaderProperty(xmlReader, APACHE_PROPERTIES_INTERNAL_GRAMMARPOOL, grammarPool); } // Setup xml catalog resolver if (resolver != null) { setReaderProperty(xmlReader, APACHE_PROPERTIES_ENTITYRESOLVER, resolver); } return xmlReader; }