List of usage examples for org.xml.sax InputSource getSystemId
public String getSystemId()
From source file:org.mycore.common.xml.MCREntityResolver.java
private InputSource resolvedEntity(InputSource entity) { String msg = "Resolved to: " + entity.getSystemId() + "."; LOGGER.info(msg);/*from ww w . j a v a 2s .co m*/ return entity; }
From source file:org.openrdf.rio.rdfxml.RDFXMLParser.java
private void parse(InputSource inputSource) throws IOException, RDFParseException, RDFHandlerException { try {/*from w w w . ja v a2s . c o m*/ documentURI = inputSource.getSystemId(); saxFilter.setParseStandAloneDocuments( getParserConfig().get(XMLParserSettings.PARSE_STANDALONE_DOCUMENTS)); // saxFilter.clear(); saxFilter.setDocumentURI(documentURI); XMLReader xmlReader; if (getParserConfig().isSet(XMLParserSettings.CUSTOM_XML_READER)) { xmlReader = getParserConfig().get(XMLParserSettings.CUSTOM_XML_READER); } else { xmlReader = XMLReaderFactory.createXMLReader(); } xmlReader.setContentHandler(saxFilter); xmlReader.setErrorHandler(this); // Set all compulsory feature settings, using the defaults if they are // not explicitly set for (RioSetting<Boolean> aSetting : getCompulsoryXmlFeatureSettings()) { try { xmlReader.setFeature(aSetting.getKey(), getParserConfig().get(aSetting)); } catch (SAXNotRecognizedException e) { reportWarning(String.format("%s is not a recognized SAX feature.", aSetting.getKey())); } catch (SAXNotSupportedException e) { reportWarning(String.format("%s is not a supported SAX feature.", aSetting.getKey())); } } // Set all compulsory property settings, using the defaults if they are // not explicitly set for (RioSetting<?> aSetting : getCompulsoryXmlPropertySettings()) { try { xmlReader.setProperty(aSetting.getKey(), getParserConfig().get(aSetting)); } catch (SAXNotRecognizedException e) { reportWarning(String.format("%s is not a recognized SAX property.", aSetting.getKey())); } catch (SAXNotSupportedException e) { reportWarning(String.format("%s is not a supported SAX property.", aSetting.getKey())); } } // Check for any optional feature settings that are explicitly set in // the parser config for (RioSetting<Boolean> aSetting : getOptionalXmlFeatureSettings()) { try { if (getParserConfig().isSet(aSetting)) { xmlReader.setFeature(aSetting.getKey(), getParserConfig().get(aSetting)); } } catch (SAXNotRecognizedException e) { reportWarning(String.format("%s is not a recognized SAX feature.", aSetting.getKey())); } catch (SAXNotSupportedException e) { reportWarning(String.format("%s is not a supported SAX feature.", aSetting.getKey())); } } // Check for any optional property settings that are explicitly set in // the parser config for (RioSetting<?> aSetting : getOptionalXmlPropertySettings()) { try { if (getParserConfig().isSet(aSetting)) { xmlReader.setProperty(aSetting.getKey(), getParserConfig().get(aSetting)); } } catch (SAXNotRecognizedException e) { reportWarning(String.format("%s is not a recognized SAX property.", aSetting.getKey())); } catch (SAXNotSupportedException e) { reportWarning(String.format("%s is not a supported SAX property.", aSetting.getKey())); } } xmlReader.parse(inputSource); } catch (SAXParseException e) { Exception wrappedExc = e.getException(); if (wrappedExc == null) { reportFatalError(e, e.getLineNumber(), e.getColumnNumber()); } else { reportFatalError(wrappedExc, e.getLineNumber(), e.getColumnNumber()); } } catch (SAXException e) { Exception wrappedExc = e.getException(); if (wrappedExc == null) { reportFatalError(e); } else if (wrappedExc instanceof RDFParseException) { throw (RDFParseException) wrappedExc; } else if (wrappedExc instanceof RDFHandlerException) { throw (RDFHandlerException) wrappedExc; } else { reportFatalError(wrappedExc); } } finally { // Clean up saxFilter.clear(); xmlLang = null; elementStack.clear(); usedIDs.clear(); clear(); } }
From source file:org.regenstrief.util.XMLUtil.java
public final static Reader toReader(final InputSource source) { try {//from w w w. ja v a 2s . c om final InputStream in = source.getByteStream(); if (in != null) { return Util.getReader(in); } else { final Reader r = source.getCharacterStream(); if (r != null) { return r; } else { final String systemId = source.getSystemId(); if (systemId != null) { return Util.getReader(systemId); } } } } catch (final Exception e) { throw Util.toRuntimeException(e); } throw new RuntimeException("Unsupported InputSource: " + source); }
From source file:org.xchain.framework.strategy.CatalogConsumerStrategy.java
public Catalog consume(String systemId, SourceStrategy<InputSource> sourceStrategy, DependencyTracker tracker) throws Exception { // Get the input source InputSource inputSource = sourceStrategy.getSource(systemId); // create the XMLReader. XMLReader reader = XmlFactoryLifecycle.newXmlReader(); reader.setErrorHandler(new FailingErrorHandler()); XChainDeclFilter sourceFilter = new XChainDeclFilter(); sourceFilter.setParent(reader);//w ww . j a v a 2 s . c o m sourceFilter.setErrorHandler(new FailingErrorHandler()); // create the jsl filter. SaxTemplateHandler xmlFilter = new SaxTemplateHandler(); xmlFilter.setParent(sourceFilter); xmlFilter.setErrorHandler(new FailingErrorHandler()); // create the digester, passing the jsl filter. Digester digester = new Digester(xmlFilter); // set the digester onto the xml filter. xmlFilter.setDigester(digester); // add the annotation rule set to the digester. digester.addRuleSet(new AnnotationRuleSet(systemId)); digester.setErrorHandler(new FailingErrorHandler()); // get the catalog object. Catalog catalog = null; try { catalog = (Catalog) digester.parse(inputSource); } catch (Exception e) { if (log.isErrorEnabled()) { log.error("Could not create catalog for system id '" + inputSource.getSystemId() + "'.", e); } throw e; } return catalog; }
From source file:org.zanata.adapter.glossary.GlossaryPoReader.java
static MessageStreamParser createParser(InputSource inputSource) { MessageStreamParser messageParser;// w w w.j a va 2s . c o m if (inputSource.getCharacterStream() != null) messageParser = new MessageStreamParser(inputSource.getCharacterStream()); else if (inputSource.getByteStream() != null) { if (inputSource.getEncoding() != null) messageParser = new MessageStreamParser(inputSource.getByteStream(), Charset.forName(inputSource.getEncoding())); else messageParser = new MessageStreamParser(inputSource.getByteStream(), Charset.forName("UTF-8")); } else if (inputSource.getSystemId() != null) { try { URL url = new URL(inputSource.getSystemId()); if (inputSource.getEncoding() != null) messageParser = new MessageStreamParser(url.openStream(), Charset.forName(inputSource.getEncoding())); else messageParser = new MessageStreamParser(url.openStream(), Charset.forName("UTF-8")); } catch (IOException e) { throw new RuntimeException("failed to get input from url in inputSource", e); } } else throw new RuntimeException("not a valid inputSource"); return messageParser; }
From source file:org.zanata.adapter.po.PoReader2.java
static MessageStreamParser createParser(InputSource inputSource) { MessageStreamParser messageParser;// w ww . ja v a 2s . co m if (inputSource.getCharacterStream() != null) messageParser = new MessageStreamParser(inputSource.getCharacterStream()); else if (inputSource.getByteStream() != null) { if (inputSource.getEncoding() != null) messageParser = new MessageStreamParser(inputSource.getByteStream(), Charset.forName(inputSource.getEncoding())); else messageParser = new MessageStreamParser(inputSource.getByteStream(), Charset.forName("UTF-8")); } else if (inputSource.getSystemId() != null) { try { URL url = new URL(inputSource.getSystemId()); if (inputSource.getEncoding() != null) messageParser = new MessageStreamParser(url.openStream(), Charset.forName(inputSource.getEncoding())); else messageParser = new MessageStreamParser(url.openStream(), Charset.forName("UTF-8")); } catch (IOException e) { // TODO throw stronger typed exception throw new RuntimeException("failed to get input from url in inputSource", e); } } else // TODO throw stronger typed exception throw new RuntimeException("not a valid inputSource"); return messageParser; }
From source file:uk.ac.ebi.arrayexpress.utils.saxon.FlatFileXMLReader.java
public void parse(InputSource input) throws IOException, SAXException { int headerRows = getIntOptionValue(OPTION_HEADER_ROWS, 0); int page = getIntOptionValue(OPTION_PAGE, 0); int pageSize = getIntOptionValue(OPTION_PAGE_SIZE, -1); Integer sortBy = getIntOptionValue(OPTION_SORT_BY, null); String sortOrder = getStringOptionValue(OPTION_SORT_ORDER, "a"); ContentHandler ch = getContentHandler(); if (null == ch) { return;// w ww .ja v a 2 s . c o m } Reader inStream; if (input.getCharacterStream() != null) { inStream = input.getCharacterStream(); } else if (input.getByteStream() != null) { inStream = new InputStreamReader(input.getByteStream()); } else if (input.getSystemId() != null) { URL url = new URL(input.getSystemId()); inStream = new InputStreamReader(url.openStream()); } else { throw new SAXException("Invalid InputSource object"); } CSVReader ffReader = new CSVReader(new BufferedReader(inStream), this.columnDelimiter, this.columnQuoteChar); List<String[]> ff = ffReader.readAll(); int cols = ff.size() > 0 ? ff.get(0).length : 0; // verify that sort by column is with in range of columns // if not then sort will not be performed // else - switch from 1-based to 0-based index if (null != sortBy) { if (sortBy < 1 || sortBy > cols) { sortBy = null; } else { sortBy = sortBy - 1; } } // 1. removes all dodgy rows (that have less columns than the first one) // 2. determines if column to be sorted is numeric ColDataType sortColDataType = ColDataType.INTEGER; int colTypeSkipRows = headerRows; for (Iterator<String[]> iterator = ff.iterator(); iterator.hasNext();) { String[] row = iterator.next(); if (row.length != cols || isRowBlank(row)) { iterator.remove(); } else { if (null != sortBy && 0 == colTypeSkipRows && ColDataType.STRING != sortColDataType) { ColDataType dataType = getColDataType(row[sortBy]); // downgrade from int to decimal or string if (ColDataType.INTEGER == sortColDataType && ColDataType.INTEGER != dataType) { sortColDataType = dataType; } // downgrade from decimal to string only if (ColDataType.DECIMAL == sortColDataType && ColDataType.STRING == dataType) { sortColDataType = dataType; } } if (colTypeSkipRows > 0) { colTypeSkipRows--; } } } int rows = ff.size() > 0 ? ff.size() - headerRows : 0; if (-1 == pageSize) { page = 1; pageSize = rows; } ch.startDocument(); AttributesImpl tableAttrs = new AttributesImpl(); tableAttrs.addAttribute(EMPTY_NAMESPACE, "rows", "rows", CDATA_TYPE, String.valueOf(rows)); ch.startElement(EMPTY_NAMESPACE, "table", "table", tableAttrs); for (Iterator<String[]> iterator = ff.iterator(); iterator.hasNext() && headerRows > 0; headerRows--) { String[] row = iterator.next(); outputRow(ch, true, null, row); iterator.remove(); } if (null != sortBy) { Collections.sort(ff, new SortColumnComparator(sortBy, sortOrder, sortColDataType)); } int rowSeq = 1; for (String[] row : ff) { if (rowSeq > (pageSize * (page - 1)) && rowSeq <= (pageSize * page)) { outputRow(ch, false, String.valueOf(rowSeq), row); } ++rowSeq; } ch.endElement(EMPTY_NAMESPACE, "table", "table"); ch.endDocument(); }