List of usage examples for org.xml.sax InputSource setSystemId
public void setSystemId(String systemId)
From source file:net.sf.ginp.setup.SetupManagerImpl.java
/** * @throws SetupException if there's a problem reading the doc * note: don't be fooled, we can't report the path of the * offending document because it is supplied as a Stream. * @throws IOException//from w w w .ja v a 2 s . c o m * @see net.sf.ginp.setup.SetupManager#testValidConfig(java.io.InputStream) */ public final Document testValidConfig(final InputStream stream) throws SetupException, IOException { String reader = GinpUtil.readBufferIntoMemory(stream); try { return validateConfig(new ByteArrayInputStream(reader.getBytes())); } catch (DocumentException d) { SAXReader read = new SAXReader(false); try { InputSource source = new InputSource(); source.setCharacterStream(new StringReader(reader)); String ginpFile = this.getClass().getResource("/net/sf/ginp/config/ginp.dtd").toExternalForm(); ginpFile = ginpFile.substring(0, ginpFile.lastIndexOf("/") + 1); source.setSystemId(ginpFile); source.setPublicId("ginp.dtd"); Document document = read.read(source); List list = document.selectNodes("/ginp/collection/users"); list.addAll(document.selectNodes("/ginp/collection/admins")); Iterator iter = list.iterator(); while (iter.hasNext()) { Element e = (Element) iter.next(); String value = e.getText(); String[] users = value.split(","); for (int x = 0; x < users.length; x++) { if (users[x].length() == 0) { continue; } Element element = e.addElement("username"); element.setText(users[x].trim()); } e.setText(""); } try { return validateConfig(new ByteArrayInputStream(document.asXML().getBytes())); } catch (DocumentException e) { //Some XML parsers don't support validation if (e.getMessage().indexOf("not supported") >= 0) { log.warn("Validation Not Supported by your XML parser", e); return document; } else { log.error("Validation Error:", e); } throw e; } } catch (DocumentException e) { log.error("throwing new SetupException", e); throw new SetupException(GinpUtil.message(TROUBLE_PARSING), d); } } }
From source file:com.wooki.services.parsers.XHTMLToFormattingObjects.java
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { InputSource toReturn = null; Element cachedValue = cache.get(systemId); if (cachedValue == null) { if (systemId.contains("http://")) { HttpMethod get = new GetMethod(systemId); httpClient.executeMethod(get); byte[] body = get.getResponseBody(); Element element = new Element(systemId, body); cache.put(element);//w ww .j av a 2 s . c om toReturn = new InputSource(new BufferedInputStream(new ByteArrayInputStream(body))); toReturn.setSystemId(systemId); } else if (systemId.contains("file://")) { String newPath = systemId.substring("file:///".length()).replace('/', File.separatorChar); File baseFile = new File(newPath); try { toReturn = new InputSource(new FileInputStream(baseFile)); } catch (FileNotFoundException e) { e.printStackTrace(); logger.error(e.getLocalizedMessage()); return null; } } } else { toReturn = new InputSource( new BufferedInputStream(new ByteArrayInputStream((byte[]) cachedValue.getObjectValue()))); toReturn.setSystemId(systemId); } return toReturn; }
From source file:org.solmix.runtime.support.spring.ContainerEntityResolver.java
@Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { InputSource source = super.resolveEntity(publicId, systemId); if (null == source && null != systemId) { // try the schema and dtd resolver in turn, ignoring the suffix in publicId LOG.info("Attempting to resolve systemId {}", systemId); source = schemaResolver.resolveEntity(publicId, systemId); if (null == source) { source = dtdResolver.resolveEntity(publicId, systemId); }/*from ww w. j ava 2s . c om*/ } String resourceLocation = schemaMappings.get(systemId); if (resourceLocation != null && publicId == null) { Resource resource = new ClassPathResource(resourceLocation, classLoader); if (resource != null && resource.exists()) { source.setPublicId(systemId); source.setSystemId(resource.getURL().toString()); } } return source; }
From source file:nl.armatiek.xslweb.configuration.WebApp.java
public WebApp(File webAppDefinition) throws Exception { logger.info(String.format("Loading webapp definition \"%s\" ...", webAppDefinition.getAbsolutePath())); Context context = Context.getInstance(); this.definition = webAppDefinition; this.homeDir = webAppDefinition.getParentFile(); this.name = this.homeDir.getName(); this.configuration = new XSLWebConfiguration(this); this.processor = new Processor(this.configuration.getConfiguration()); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*from ww w . j a v a2 s . com*/ dbf.setSchema(context.getWebAppSchema()); dbf.setXIncludeAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); db.setErrorHandler(this); String defXml = IOUtils.toString(new XmlStreamReader(webAppDefinition)); Properties vars = new Properties(System.getProperties()); vars.setProperty("webapp-dir", webAppDefinition.getParentFile().getAbsolutePath().replace('\\', '/')); String resolvedDefXml = XSLWebUtils.resolveProperties(defXml, vars); // Document webAppDoc = db.parse(webAppDefinition); InputSource src = new InputSource(new StringReader(resolvedDefXml)); src.setSystemId(webAppDefinition.getAbsolutePath()); Document webAppDoc = db.parse(src); XPath xpath = new XPathFactoryImpl().newXPath(); xpath.setNamespaceContext(XMLUtils.getNamespaceContext("webapp", Definitions.NAMESPACEURI_XSLWEB_WEBAPP)); Node docElem = webAppDoc.getDocumentElement(); this.title = (String) xpath.evaluate("webapp:title", docElem, XPathConstants.STRING); this.description = (String) xpath.evaluate("webapp:description", docElem, XPathConstants.STRING); String devModeValue = (String) xpath.evaluate("webapp:development-mode", docElem, XPathConstants.STRING); this.developmentMode = XMLUtils.getBooleanValue(devModeValue, false); String maxUploadSizeValue = (String) xpath.evaluate("webapp:max-upload-size", docElem, XPathConstants.STRING); this.maxUploadSize = XMLUtils.getIntegerValue(maxUploadSizeValue, 10); String waitForJobsAtCloseValue = (String) xpath.evaluate("webapp:wait-for-jobs-at-close", docElem, XPathConstants.STRING); this.waitForJobsAtClose = XMLUtils.getBooleanValue(waitForJobsAtCloseValue, true); NodeList resourceNodes = (NodeList) xpath.evaluate("webapp:resources/webapp:resource", docElem, XPathConstants.NODESET); for (int i = 0; i < resourceNodes.getLength(); i++) { resources.add(new Resource((Element) resourceNodes.item(i))); } NodeList paramNodes = (NodeList) xpath.evaluate("webapp:parameters/webapp:parameter", docElem, XPathConstants.NODESET); for (int i = 0; i < paramNodes.getLength(); i++) { parameters.add(new Parameter(processor, (Element) paramNodes.item(i))); } NodeList jobNodes = (NodeList) xpath.evaluate("webapp:jobs/webapp:job", docElem, XPathConstants.NODESET); if (jobNodes.getLength() > 0) { File quartzFile = new File(homeDir, Definitions.FILENAME_QUARTZ); quartzFile = (quartzFile.isFile()) ? quartzFile : new File(context.getHomeDir(), "config" + File.separatorChar + Definitions.FILENAME_QUARTZ); SchedulerFactory sf; if (quartzFile.isFile()) { logger.info(String.format("Initializing Quartz scheduler using properties file \"%s\" ...", quartzFile.getAbsolutePath())); Properties props = XSLWebUtils.readProperties(quartzFile); props.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, name); sf = new StdSchedulerFactory(props); } else { logger.info("Initializing Quartz scheduler ..."); sf = new StdSchedulerFactory(); } scheduler = sf.getScheduler(); for (int i = 0; i < jobNodes.getLength(); i++) { Element jobElem = (Element) jobNodes.item(i); String jobName = XMLUtils.getValueOfChildElementByLocalName(jobElem, "name"); String jobUri = XMLUtils.getValueOfChildElementByLocalName(jobElem, "uri"); String jobCron = XMLUtils.getValueOfChildElementByLocalName(jobElem, "cron"); boolean concurrent = XMLUtils .getBooleanValue(XMLUtils.getValueOfChildElementByLocalName(jobElem, "concurrent"), true); String jobId = "job_" + name + "_" + jobName; JobDetail job = newJob(concurrent ? XSLWebJob.class : NonConcurrentExecutionXSLWebJob.class) .withIdentity(jobId, name).usingJobData("webapp-path", getPath()) .usingJobData("uri", jobUri).build(); Trigger trigger = newTrigger().withIdentity("trigger_" + name + "_" + jobName, name) .withSchedule(cronSchedule(jobCron)).forJob(jobId, name).build(); logger.info(String.format("Scheduling job \"%s\" of webapp \"%s\" ...", jobName, name)); scheduler.scheduleJob(job, trigger); } } NodeList dataSourceNodes = (NodeList) xpath.evaluate("webapp:datasources/webapp:datasource", docElem, XPathConstants.NODESET); for (int i = 0; i < dataSourceNodes.getLength(); i++) { DataSource dataSource = new DataSource((Element) dataSourceNodes.item(i)); dataSources.put(dataSource.getName(), dataSource); } NodeList fopConfigNodes = (NodeList) xpath.evaluate("webapp:fop-configs/webapp:fop-config", docElem, XPathConstants.NODESET); for (int i = 0; i < fopConfigNodes.getLength(); i++) { Element fopConfig = (Element) fopConfigNodes.item(i); Element fopElement = XMLUtils.getFirstChildElement(fopConfig); fopConfigs.put(fopConfig.getAttribute("name"), XMLUtils.nodeToString(fopElement)); } // initClassLoader(); initFileAlterationObservers(); }
From source file:com.servicelibre.jxsl.scenario.XslScenario.java
/** * Apply XSL transformation on XML bytes. * //from w ww . j a v a2s .c om * @param xmlBytes * @return */ public Map<String, String> apply(byte[] xmlBytes, String systemId) { Map<String, String> xslOutputs = new HashMap<String, String>(1); Transformer transformer = getTransformer(); if (transformer == null) { logger.warn("No transformer available for xslPath {}", xslPath); return xslOutputs; } multipleOutputs.clearResults(); xslOutputs.put(mainOutputKey, ""); // TODO can do better, but as fast? if (xmlBytes.length <= 10) { logger.warn("No enough bytes to apply XSL on..."); return xslOutputs; } try { setTimestamp(df.format(new Date())); logger.debug("Going to execute [{}], on a document of {} bytes", this.xslPath, xmlBytes.length); // Pass parameters to XSL for (String paramName : parameters.keySet()) { logger.debug("Setting up parameter {} to {}", paramName, parameters.get(paramName)); transformer.setParameter(paramName, parameters.get(paramName)); } String xmlSourceFilename = ""; // save XML source if requested if (saveXmlSource) { File xmlSourceFile = saveXmlSourceFile(xmlBytes); if (xmlSourceFile != null) { xmlSourceFilename = xmlSourceFile.getAbsolutePath(); } } InputSource inputSource = new InputSource(new ByteArrayInputStream(xmlBytes)); // To prevent error such FORG0002 (Base URI {} is not an absolute // URI), etc. inputSource.setSystemId(systemId); SAXSource saxSource = new SAXSource(reader, inputSource); StringWriter xslMainStringOutput = new StringWriter(); logger.debug("Start execution of [{}]", this.xslPath); Date startDate = new Date(); long startTime = System.nanoTime(); transformer.transform(saxSource, new StreamResult(xslMainStringOutput)); long executionTime = System.nanoTime() - startTime; logger.debug("Stop execution of [{}] ({}ms)", this.xslPath, (double) executionTime / 1000000); executionCount++; // Add main result output logger.debug("Storing main output (key={})", mainOutputKey); xslOutputs.put(mainOutputKey, xslMainStringOutput.toString()); // Add potential other result outputs Map<String, StringWriter> outputs = multipleOutputs.getOutputs(); for (String outputName : outputs.keySet()) { logger.debug("Storing additional output (key={})", outputName); xslOutputs.put(outputName, outputs.get(outputName).toString()); } setLastRunReport(createRunReport(xmlSourceFilename, xslOutputs, startDate, executionTime)); } catch (TransformerConfigurationException e) { logger.error("Error during XSL transformation.", e); } catch (TransformerException e) { logger.error("Error during XSL transformation.", e); } catch (IllegalArgumentException e) { logger.error("Error during XSL transformation.", e); } return xslOutputs; }
From source file:com.seajas.search.contender.service.modifier.ArchiveModifierService.java
/** * Handle the given archive's content by sending each entry to the given handler. * /*from w w w. j av a2s . c o m*/ * @param archive * @param handler * @throws Exception */ public void handleArchive(final Archive archive, final ArchiveResultHandler handler) throws Exception { // Create a validating SAX parser final SAXParserFactory parserFactory = SAXParserFactory.newInstance(); parserFactory.setValidating(true); parserFactory.setNamespaceAware(true); // Create a common transformer per thread Transformer transformer = null; try { transformer = transformerCache.getTransformer(archive.getId(), "archive", archive.getModificationDate()); if (transformer == null) transformer = transformerCache.putContent(archive.getId(), "archive", archive.getModificationDate(), archive.getTransformerContent()); } catch (TransformerConfigurationException e) { logger.error("Unable to generate a (cached) transformer from the given content", e); return; } catch (TransformerFactoryConfigurationError e) { logger.error("Unable to generate a (cached) transformer from the given content", e); return; } // Store and process the files try { Map<File, String> storedResults = storeAndDecompressFiles(archive); List<String> deletedLinks = new ArrayList<String>(); // Only process deletes when a deletion expression has been provided if (StringUtils.hasText(archive.getDeletionExpression())) { // Process all entries beforehand, so to exclude deletes from the final result for (Map.Entry<File, String> storedResult : storedResults.entrySet()) { File storedResultsFolder = storedResult.getKey(); for (File entryLocation : storedResultsFolder.listFiles()) if (entryLocation.getName().matches(archive.getDeletionExpression())) { String deleteLink = entryLocation.getName().replaceAll(archive.getDeletionExpression(), archive.getInternalLink()); deletedLinks.add(deleteLink); // Delete the actual link cacheService .addDeleted(new CacheService.DeletedEntry(archive.getCollection(), deleteLink)); } } } // Now process the stored results themselves for (Map.Entry<File, String> storedResult : storedResults.entrySet()) { File storedResultsFolder = storedResult.getKey(); // Create the descriptions folder if it doesn't already exist File descriptionsFolderLocation = new File(storedResultsFolder, "descriptions"); if (!descriptionsFolderLocation.exists()) descriptionsFolderLocation.mkdirs(); for (File entryLocation : storedResultsFolder.listFiles()) { if (entryLocation.isDirectory()) { if (!entryLocation.getName().equals("descriptions")) logger.warn("Unknown folder '" + entryLocation.getName() + "'found in decompressed archive folder '" + storedResultsFolder.getAbsolutePath() + "'"); continue; } else if (StringUtils.hasText(archive.getDeletionExpression()) && entryLocation.getName().matches(archive.getDeletionExpression())) continue; InputStream transformationInputStream = null; try { transformationInputStream = new BufferedInputStream(new FileInputStream(entryLocation)); // Now determine the content type and create a reader in case of structured content MediaType entryMediaType = autoDetectParser.getDetector().detect(transformationInputStream, new Metadata()); if (!(entryMediaType.getSubtype().equals("xml") || entryMediaType.getSubtype().endsWith("+xml"))) { logger.warn("Archive entry " + entryLocation.getAbsolutePath() + " contains " + entryMediaType + " data which is unstructured, ignoring"); continue; } } catch (IOException e) { logger.error("Could not close input stream during archive processing", e); if (transformationInputStream != null) transformationInputStream.close(); continue; } // Process it as (semi-)structured content XmlReader transformationReader = new XmlReader(transformationInputStream, true); StringWriter transformationWriter = new StringWriter(); try { transformer.setOutputProperty(OutputKeys.ENCODING, "UTF8"); // Use a SAX reader for entity resolution XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader(); InputSource inputSource = new InputSource(transformationReader); xmlReader.setEntityResolver(transformerCache.getEntityResolver()); inputSource.setSystemId("file://" + transformerCache.getDtdImportPath() + "/template.xsl"); // Perform the actual transformation transformer.setParameter("substituteUrl", archive.getInternalLink()); transformer.transform(new SAXSource(xmlReader, inputSource), new StreamResult(transformationWriter)); } catch (TransformerException e) { logger.error("Unable to perform content transformation for entry " + entryLocation.getAbsolutePath()); continue; } catch (SAXException e) { logger.error("Unable to perform content transformation for entry " + entryLocation.getAbsolutePath()); continue; } catch (ParserConfigurationException e) { logger.error("Unable to perform content transformation for entry " + entryLocation.getAbsolutePath()); continue; } finally { transformationInputStream.close(); transformationReader.close(); } // Create a syndication feed from the given result String resultContent = transformationWriter.toString(); SyndFeed resultFeed = null; try { SyndFeedInput feedInput = new SyndFeedInput(); resultFeed = feedInput.build(new StringReader(resultContent)); } catch (FeedException e) { logger.error("Could not parse the feed resulting from the archive entry transformation"); continue; } finally { transformationWriter.close(); } // Write the <description> content to a separate file and add it as an <enclosure> if (resultFeed.getEntries().size() > 0) { Integer entryNumber = 0; for (SyndEntry feedEntry : (Collection<SyndEntry>) resultFeed.getEntries()) { if (!deletedLinks.contains(feedEntry.getLink())) { String description = feedEntry.getDescription().getValue().trim(); File descriptionLocation = new File(descriptionsFolderLocation, stripExtension(entryLocation.getName()) + "-" + entryNumber++ + (feedEntry.getDescription().getType().equals("text/html") ? ".html" : ".xml")); Writer descriptionWriter = new OutputStreamWriter( new FileOutputStream(descriptionLocation), "UTF-8"); if (!description.endsWith("</html>")) descriptionWriter.write("<html>\n<head>\n\t<title>" + feedEntry.getTitle() + "</title>\n</head>\n<body>\n"); descriptionWriter.write(description); if (!description.endsWith("</html>")) descriptionWriter.write("\n</body>\n</html>"); descriptionWriter.flush(); descriptionWriter.close(); // Remove the link from the processed cache should it already be in there, taking care of updates cacheService.deleteElement(feedEntry.getLink()); // Then offer it up to the handler if (logger.isDebugEnabled()) logger.debug("Adding result content (" + entryNumber + ") for archive entry with path " + entryLocation.getAbsolutePath()); try { // NOTE: The encoding of 'UTF-8' is implied for archive-related files handler.process(new URI(feedEntry.getLink()), archive.getHostname(), feedEntry, resultFeed); } catch (FeedException e) { logger.error(String.format( "Could not offer feed entry with link '%s' - invalid entry", feedEntry.getLink()), e); } catch (URISyntaxException e) { logger.error(String.format( "Could not offer feed entry with link '%s' - invalid link", feedEntry.getLink()), e); } } else logger.info("Skipping over feed entry with link '" + feedEntry.getLink() + "' - marked for deletion"); } } else if (logger.isDebugEnabled()) logger.debug("No entries were found in archive entry with path " + entryLocation.getAbsolutePath()); } logger.info("Finished processing archive with name " + storedResult.getValue()); // Now archive the entry in the cache cacheService.addArchived(storedResult.getValue()); } logger.info("Finishing archive populator thread"); } catch (IOException e) { logger.error("Could not close input stream during archive processing", e); } }
From source file:cl.utfsm.cdbChecker.CDBChecker.java
/** * This method validates the XML files./*from w w w .jav a2 s. co m*/ * * @param filenames name with absolute path of the XML file to validate. */ protected void XMLValidate(Vector<String> filenames) { System.out.println("*** Will verify XML files in directory: " + this.XMLPath); for (int i = 0; i < filenames.size(); i++) { File file = new File(filenames.get(i)); if (file.length() != 0) { if (verbose) { System.out.print(" " + filenames.get(i)); /*for(int j=0;j<(90-(int)((String)filename.get(i)).length())/8;j++) System.out.print("\t"); */} String targetNamespace = ((xsd_targetns.toString()).replace(',', ' ')).replace('=', ' ') .replace('{', ' ').replace('}', ' '); errorFlag = false; try { validateFileEncoding(filenames.get(i)); SP.reset(); SP.setFeature("http://xml.org/sax/features/validation", true); SP.setFeature("http://apache.org/xml/features/validation/schema", true); SP.setFeature("http://xml.org/sax/features/namespace-prefixes", false); SP.setFeature("http://xml.org/sax/features/namespaces", true); SP.setErrorHandler(new CDBErrorHandler(this)); SP.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", targetNamespace); FileInputStream fis = new FileInputStream(file); InputSource inputSource = new InputSource(fis); inputSource.setSystemId("file:///" + file.getAbsolutePath()); SP.parse(inputSource); fis.close(); if (verbose && !errorFlag) System.out.println("[OK]"); } catch (SAXException e) { System.out.println("[SAXException] " + e.getMessage()); } catch (IOException e) { System.out.println("[IOException] Probably " + filenames.get(i) + " doesn't exist."); } } else { System.out.print(filenames.get(i) + ": [Warning] file is empty.\n"); } } }
From source file:net.sf.joost.trax.TrAXHelper.java
/** * Helpermethod for getting an InputSource from a StreamSource. * @param source <code>Source</code> * @return An <code>InputSource</code> object or null * @throws TransformerConfigurationException *//*from w w w . j a va2s . co m*/ protected static InputSource getInputSourceForStreamSources(Source source, ErrorListener errorListener) throws TransformerConfigurationException { if (DEBUG) log.debug("getting an InputSource from a StreamSource"); InputSource input = null; String systemId = source.getSystemId(); if (systemId == null) { systemId = ""; } try { if (source instanceof StreamSource) { if (DEBUG) log.debug("Source is a StreamSource"); StreamSource stream = (StreamSource) source; InputStream istream = stream.getInputStream(); Reader reader = stream.getReader(); // Create InputSource from Reader or InputStream in Source if (istream != null) { input = new InputSource(istream); } else { if (reader != null) { input = new InputSource(reader); } else { input = new InputSource(systemId); } } } else { //Source type is not supported if (errorListener != null) { try { errorListener .fatalError(new TransformerConfigurationException("Source is not a StreamSource")); return null; } catch (TransformerException e2) { if (DEBUG) log.debug("Source is not a StreamSource"); throw new TransformerConfigurationException("Source is not a StreamSource"); } } if (DEBUG) log.debug("Source is not a StreamSource"); throw new TransformerConfigurationException("Source is not a StreamSource"); } //setting systemId input.setSystemId(systemId); // } catch (NullPointerException nE) { // //catching NullPointerException // if(errorListener != null) { // try { // errorListener.fatalError( // new TransformerConfigurationException(nE)); // return null; // } catch( TransformerException e2) { // log.debug(nE); // throw new TransformerConfigurationException(nE.getMessage()); // } // } // log.debug(nE); // throw new TransformerConfigurationException(nE.getMessage()); } catch (SecurityException sE) { //catching SecurityException if (errorListener != null) { try { errorListener.fatalError(new TransformerConfigurationException(sE)); return null; } catch (TransformerException e2) { if (DEBUG) log.debug(sE); throw new TransformerConfigurationException(sE.getMessage()); } } if (DEBUG) log.debug(sE); throw new TransformerConfigurationException(sE.getMessage()); } return (input); }
From source file:gate.corpora.FastInfosetDocumentFormat.java
/** * Unpack markup from any XML format. The XML elements are translated to * annotations on the Original markups annotation set. * //from www .ja v a2 s . co m * @param doc * the document to process * @throws DocumentFormatException */ private void unpackGeneralXmlMarkup(Document doc, RepositioningInfo repInfo, RepositioningInfo ampCodingInfo, StatusListener statusListener) throws DocumentFormatException { boolean docHasContentButNoValidURL = hasContentButNoValidUrl(doc); XmlDocumentHandler xmlDocHandler = null; try { // Create a new Xml document handler xmlDocHandler = new XmlDocumentHandler(doc, this.markupElementsMap, this.element2StringMap); // Register a status listener with it xmlDocHandler.addStatusListener(statusListener); // set repositioning object xmlDocHandler.setRepositioningInfo(repInfo); // set the object with ampersand coding positions xmlDocHandler.setAmpCodingInfo(ampCodingInfo); // create the parser SAXDocumentParser newxmlParser = new SAXDocumentParser(); // Set up the factory to create the appropriate type of parser // Fast Infoset doesn't support validating which is good as we would want // it off any way, but we do want it to be namesapace aware newxmlParser.setFeature("http://xml.org/sax/features/namespaces", true); newxmlParser.setFeature("http://xml.org/sax/features/namespace-prefixes", true); newxmlParser.setContentHandler(xmlDocHandler); newxmlParser.setErrorHandler(xmlDocHandler); newxmlParser.setDTDHandler(xmlDocHandler); newxmlParser.setEntityResolver(xmlDocHandler); // Parse the XML Document with the appropriate encoding Reader docReader = null; try { InputSource is; if (docHasContentButNoValidURL) { // no URL, so parse from string is = new InputSource(new StringReader(doc.getContent().toString())); } else if (doc instanceof TextualDocument) { // textual document - load with user specified encoding String docEncoding = ((TextualDocument) doc).getEncoding(); // don't strip BOM on XML. docReader = new InputStreamReader(doc.getSourceUrl().openStream(), docEncoding); is = new InputSource(docReader); // must set system ID to allow relative URLs (e.g. to a DTD) to // work is.setSystemId(doc.getSourceUrl().toString()); } else { // let the parser decide the encoding is = new InputSource(doc.getSourceUrl().toString()); } newxmlParser.parse(is); } finally { // make sure the open streams are closed if (docReader != null) docReader.close(); } ((DocumentImpl) doc).setNextAnnotationId(xmlDocHandler.getCustomObjectsId()); } catch (SAXException e) { doc.getFeatures().put("parsingError", Boolean.TRUE); Boolean bThrow = (Boolean) doc.getFeatures().get(GateConstants.THROWEX_FORMAT_PROPERTY_NAME); if (bThrow != null && bThrow.booleanValue()) { throw new DocumentFormatException(e); } else { Out.println("Warning: Document remains unparsed. \n" + "\n Stack Dump: "); e.printStackTrace(Out.getPrintWriter()); } } catch (IOException e) { throw new DocumentFormatException("I/O exception for " + doc.getSourceUrl(), e); } finally { if (xmlDocHandler != null) xmlDocHandler.removeStatusListener(statusListener); } }
From source file:cl.utfsm.cdbChecker.CDBChecker.java
/** * This method checks for the targetNamespace defined by the schema files and fills the CDBChecker.xsd_targetns with pairs {targetNamespace, XSD filename} * //from w ww .ja va 2 s . c o m * @param xsdFilenames Vector with all the XSD filenames with absolute path. */ protected void getTargetNamespace(Vector<String> xsdFilenames) { String filename; for (int i = 0; i < xsdFilenames.size(); i++) { filename = xsdFilenames.get(i); File file = new File(xsdFilenames.get(i)); if (file.length() != 0) { SP.setContentHandler(new CDBContentHandler(this)); SP.reset(); try { SP.setFeature("http://xml.org/sax/features/validation", false); SP.setFeature("http://apache.org/xml/features/validation/schema", false); SP.setFeature("http://xml.org/sax/features/namespace-prefixes", false); SP.setFeature("http://xml.org/sax/features/namespaces", true); SP.setErrorHandler(new CDBErrorHandler(this)); FileInputStream fis = new FileInputStream(file); InputSource inputSource = new InputSource(fis); inputSource.setSystemId("file:///" + file.getAbsolutePath()); SP.parse(inputSource); fis.close(); } catch (SAXException e) { e.getMessage(); } catch (IOException e) { System.out.println("[IOException] Probably " + filename + " doesn't exists."); } if (targetNamespace != null) { /* GCH * If the targetNamespace has been already registered, * I skip registering it again. * In this way I give priority to definitions that come first. * Since the search order depends on the order int the ACS.cdbPath * property, standard definitions can be overwritten byte newer ones in * the standard search path algorithm. */ if (xsd_targetns.containsKey(targetNamespace)) { /* * If the same targetNamespace appears int files with * the same name, then we are simply overriding the * default version with a new one and we need to warning. * Otherwise, a warning can be useful to discover * inconsistencies. */ String[] newArr = filename.split("/"); String[] oldArr = ((String) xsd_targetns.get(targetNamespace)).split("/"); if (newArr[newArr.length - 1].compareTo(oldArr[oldArr.length - 1]) != 0) { System.out.println("[Warning] The XSD files \"" + xsdFilenames.get(i) + "\" and \"" + xsd_targetns.get(targetNamespace) + "\" have same targetNamespace: \"" + targetNamespace + "\". Skipping this one."); } } else { xsd_targetns.put(targetNamespace, "file:///" + xsdFilenames.get(i)); } } } else { System.out.print(xsdFilenames.get(i) + ": [Warning] file is empty.\n"); } } }