use of org.xml.sax.XMLReader in project translationstudio8 by heartsome.
the class Xlsx2TmxHelper method parse.
public void parse(InputStream sheetInputStream, ReadOnlySharedStringsTable sharedStringsTable, AbstractWriter tmxWriter) throws ParserConfigurationException, SAXException, IOException {
InputSource sheetSource = new InputSource(sheetInputStream);
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxFactory.newSAXParser();
XMLReader sheetParser = saxParser.getXMLReader();
ContentHandler handler = new XSSFHander(sharedStringsTable);
sheetParser.setContentHandler(handler);
sheetParser.parse(sheetSource);
if (langCodes.isEmpty()) {
throw new SAXException("EMPTY-LANG-CODE");
}
writeEnd();
}
use of org.xml.sax.XMLReader in project camel by apache.
the class JingValidator method process.
public void process(Exchange exchange) throws Exception {
Jaxp11XMLReaderCreator xmlCreator = new Jaxp11XMLReaderCreator();
DefaultValidationErrorHandler errorHandler = new DefaultValidationErrorHandler();
PropertyMapBuilder mapBuilder = new PropertyMapBuilder();
mapBuilder.put(ValidateProperty.XML_READER_CREATOR, xmlCreator);
mapBuilder.put(ValidateProperty.ERROR_HANDLER, errorHandler);
PropertyMap propertyMap = mapBuilder.toPropertyMap();
Validator validator = getSchema().createValidator(propertyMap);
Message in = exchange.getIn();
SAXSource saxSource = in.getBody(SAXSource.class);
if (saxSource == null) {
Source source = exchange.getIn().getMandatoryBody(Source.class);
saxSource = ExchangeHelper.convertToMandatoryType(exchange, SAXSource.class, source);
}
InputSource bodyInput = saxSource.getInputSource();
// now lets parse the body using the validator
XMLReader reader = xmlCreator.createXMLReader();
reader.setContentHandler(validator.getContentHandler());
reader.setDTDHandler(validator.getDTDHandler());
reader.setErrorHandler(errorHandler);
reader.parse(bodyInput);
errorHandler.handleErrors(exchange, schema);
}
use of org.xml.sax.XMLReader in project camel by apache.
the class XmlConverter method toSAXSourceFromStream.
@Converter
public SAXSource toSAXSourceFromStream(StreamSource source, Exchange exchange) throws SAXException {
InputSource inputSource;
if (source.getReader() != null) {
inputSource = new InputSource(source.getReader());
} else {
inputSource = new InputSource(source.getInputStream());
}
inputSource.setSystemId(source.getSystemId());
inputSource.setPublicId(source.getPublicId());
XMLReader xmlReader = null;
try {
// use the SAXPaserFactory which is set from exchange
if (exchange != null) {
SAXParserFactory sfactory = exchange.getProperty(Exchange.SAXPARSER_FACTORY, SAXParserFactory.class);
if (sfactory != null) {
if (!sfactory.isNamespaceAware()) {
sfactory.setNamespaceAware(true);
}
xmlReader = sfactory.newSAXParser().getXMLReader();
}
}
if (xmlReader == null) {
if (xmlReaderPool == null) {
xmlReaderPool = new XMLReaderPool(createSAXParserFactory());
}
xmlReader = xmlReaderPool.createXMLReader();
}
} catch (Exception ex) {
LOG.warn("Cannot create the SAXParser XMLReader, due to {}", ex);
}
return new SAXSource(xmlReader, inputSource);
}
use of org.xml.sax.XMLReader in project robovm by robovm.
the class TransformerFactoryImpl method newTemplates.
/**
* Process the source into a Templates object, which is likely
* a compiled representation of the source. This Templates object
* may then be used concurrently across multiple threads. Creating
* a Templates object allows the TransformerFactory to do detailed
* performance optimization of transformation instructions, without
* penalizing runtime transformation.
*
* @param source An object that holds a URL, input stream, etc.
* @return A Templates object capable of being used for transformation purposes.
*
* @throws TransformerConfigurationException May throw this during the parse when it
* is constructing the Templates object and fails.
*/
public Templates newTemplates(Source source) throws TransformerConfigurationException {
String baseID = source.getSystemId();
if (null != baseID) {
baseID = SystemIDResolver.getAbsoluteURI(baseID);
}
if (source instanceof DOMSource) {
DOMSource dsource = (DOMSource) source;
Node node = dsource.getNode();
if (null != node)
return processFromNode(node, baseID);
else {
String messageStr = XSLMessages.createMessage(XSLTErrorResources.ER_ILLEGAL_DOMSOURCE_INPUT, null);
throw new IllegalArgumentException(messageStr);
}
}
TemplatesHandler builder = newTemplatesHandler();
builder.setSystemId(baseID);
try {
InputSource isource = SAXSource.sourceToInputSource(source);
isource.setSystemId(baseID);
XMLReader reader = null;
if (source instanceof SAXSource)
reader = ((SAXSource) source).getXMLReader();
if (null == reader) {
// Use JAXP1.1 ( if possible )
try {
javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
if (m_isSecureProcessing) {
try {
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
} catch (org.xml.sax.SAXException se) {
}
}
javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser();
reader = jaxpParser.getXMLReader();
} catch (javax.xml.parsers.ParserConfigurationException ex) {
throw new org.xml.sax.SAXException(ex);
} catch (javax.xml.parsers.FactoryConfigurationError ex1) {
throw new org.xml.sax.SAXException(ex1.toString());
} catch (NoSuchMethodError ex2) {
} catch (AbstractMethodError ame) {
}
}
if (null == reader)
reader = XMLReaderFactory.createXMLReader();
// If you set the namespaces to true, we'll end up getting double
// xmlns attributes. Needs to be fixed. -sb
// reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
reader.setContentHandler(builder);
reader.parse(isource);
} catch (org.xml.sax.SAXException se) {
if (m_errorListener != null) {
try {
m_errorListener.fatalError(new TransformerException(se));
} catch (TransformerConfigurationException ex1) {
throw ex1;
} catch (TransformerException ex1) {
throw new TransformerConfigurationException(ex1);
}
} else {
throw new TransformerConfigurationException(se.getMessage(), se);
}
} catch (Exception e) {
if (m_errorListener != null) {
try {
m_errorListener.fatalError(new TransformerException(e));
return null;
} catch (TransformerConfigurationException ex1) {
throw ex1;
} catch (TransformerException ex1) {
throw new TransformerConfigurationException(ex1);
}
} else {
throw new TransformerConfigurationException(e.getMessage(), e);
}
}
return builder.getTemplates();
}
use of org.xml.sax.XMLReader in project robovm by robovm.
the class ProcessorInclude method parse.
/**
* Set off a new parse for an included or imported stylesheet. This will
* set the {@link StylesheetHandler} to a new state, and recurse in with
* a new set of parse events. Once this function returns, the state of
* the StylesheetHandler should be restored.
*
* @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
* @param uri The Namespace URI, which should be the XSLT namespace.
* @param localName The local name (without prefix), which should be "include" or "import".
* @param rawName The qualified name (with prefix).
* @param attributes The list of attributes on the xsl:include or xsl:import element.
*
* @throws org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
*/
protected void parse(StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes) throws org.xml.sax.SAXException {
TransformerFactoryImpl processor = handler.getStylesheetProcessor();
URIResolver uriresolver = processor.getURIResolver();
try {
Source source = null;
if (null != uriresolver) {
// There is a user provided URI resolver.
// At the startElement() call we would
// have tried to obtain a Source from it
// which we now retrieve
source = handler.peekSourceFromURIResolver();
if (null != source && source instanceof DOMSource) {
Node node = ((DOMSource) source).getNode();
// There is a user provided URI resolver.
// At the startElement() call we would
// have already pushed the system ID, obtained
// from either the source.getSystemId(), if non-null
// or from SystemIDResolver.getAbsoluteURI() as a backup
// which we now retrieve.
String systemId = handler.peekImportURL();
// stylesheet module onto the stack.
if (systemId != null)
handler.pushBaseIndentifier(systemId);
TreeWalker walker = new TreeWalker(handler, new org.apache.xml.utils.DOM2Helper(), systemId);
try {
walker.traverse(node);
} catch (org.xml.sax.SAXException se) {
throw new TransformerException(se);
}
if (systemId != null)
handler.popBaseIndentifier();
return;
}
}
if (null == source) {
String absURL = SystemIDResolver.getAbsoluteURI(getHref(), handler.getBaseIdentifier());
source = new StreamSource(absURL);
}
// possible callback to a class that over-rides this method.
source = processSource(handler, source);
XMLReader reader = null;
if (source instanceof SAXSource) {
SAXSource saxSource = (SAXSource) source;
// may be null
reader = saxSource.getXMLReader();
}
InputSource inputSource = SAXSource.sourceToInputSource(source);
if (null == reader) {
// Use JAXP1.1 ( if possible )
try {
javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
if (handler.getStylesheetProcessor().isSecureProcessing()) {
try {
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
} catch (org.xml.sax.SAXException se) {
}
}
javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser();
reader = jaxpParser.getXMLReader();
} catch (javax.xml.parsers.ParserConfigurationException ex) {
throw new org.xml.sax.SAXException(ex);
} catch (javax.xml.parsers.FactoryConfigurationError ex1) {
throw new org.xml.sax.SAXException(ex1.toString());
} catch (NoSuchMethodError ex2) {
} catch (AbstractMethodError ame) {
}
}
if (null == reader)
reader = XMLReaderFactory.createXMLReader();
if (null != reader) {
reader.setContentHandler(handler);
// Push the absolute URI of the included/imported
// stylesheet module onto the stack.
handler.pushBaseIndentifier(inputSource.getSystemId());
try {
reader.parse(inputSource);
} finally {
handler.popBaseIndentifier();
}
}
} catch (IOException ioe) {
handler.error(XSLTErrorResources.ER_IOEXCEPTION, new Object[] { getHref() }, ioe);
} catch (TransformerException te) {
handler.error(te.getMessage(), te);
}
}
Aggregations