use of javax.xml.transform.sax.SAXSource in project asciidoctor-fopub by asciidoctor.
the class InputHandler method createMainSource.
/**
* Creates a Source for the main input file. Processes XInclude if
* available in the XML parser.
*
* @return the Source for the main input file
*/
protected Source createMainSource() {
Source source;
InputStream in;
String uri;
if (this.sourcefile != null) {
try {
in = new java.io.FileInputStream(this.sourcefile);
uri = this.sourcefile.toURI().toASCIIString();
} catch (FileNotFoundException e) {
//handled elsewhere
return new StreamSource(this.sourcefile);
}
} else {
in = System.in;
uri = null;
}
try {
InputSource is = new InputSource(in);
is.setSystemId(uri);
XMLReader xr = getXMLReader();
if (entityResolver != null) {
xr.setEntityResolver(entityResolver);
}
source = new SAXSource(xr, is);
} catch (SAXException e) {
if (this.sourcefile != null) {
source = new StreamSource(this.sourcefile);
} else {
source = new StreamSource(in, uri);
}
} catch (ParserConfigurationException e) {
if (this.sourcefile != null) {
source = new StreamSource(this.sourcefile);
} else {
source = new StreamSource(in, uri);
}
}
return source;
}
use of javax.xml.transform.sax.SAXSource in project asciidoctor-fopub by asciidoctor.
the class InputHandler method createXSLTSource.
/**
* Creates a Source for the selected stylesheet.
*
* @return the Source for the selected stylesheet or null if there's no stylesheet
*/
protected Source createXSLTSource() {
Source xslt = null;
if (this.stylesheet != null) {
if (entityResolver != null) {
try {
InputSource is = new InputSource(this.stylesheet.getPath());
XMLReader xr = getXMLReader();
xr.setEntityResolver(entityResolver);
xslt = new SAXSource(xr, is);
} catch (SAXException e) {
// return StreamSource
} catch (ParserConfigurationException e) {
// return StreamSource
}
}
if (xslt == null) {
xslt = new StreamSource(this.stylesheet);
}
}
return xslt;
}
use of javax.xml.transform.sax.SAXSource in project j2objc by google.
the class StylesheetPIHandler method processingInstruction.
/**
* Handle the xml-stylesheet processing instruction.
*
* @param target The processing instruction target.
* @param data The processing instruction data, or null if
* none is supplied.
* @throws org.xml.sax.SAXException Any SAX exception, possibly
* wrapping another exception.
* @see org.xml.sax.ContentHandler#processingInstruction
* @see <a href="http://www.w3.org/TR/xml-stylesheet/">Associating Style Sheets with XML documents, Version 1.0</a>
*/
public void processingInstruction(String target, String data) throws org.xml.sax.SAXException {
if (target.equals("xml-stylesheet")) {
// CDATA #REQUIRED
String href = null;
// CDATA #REQUIRED
String type = null;
// CDATA #IMPLIED
String title = null;
// CDATA #IMPLIED
String media = null;
// CDATA #IMPLIED
String charset = null;
// (yes|no) "no"
boolean alternate = false;
StringTokenizer tokenizer = new StringTokenizer(data, " \t=\n", true);
boolean lookedAhead = false;
Source source = null;
String token = "";
while (tokenizer.hasMoreTokens()) {
if (!lookedAhead)
token = tokenizer.nextToken();
else
lookedAhead = false;
if (tokenizer.hasMoreTokens() && (token.equals(" ") || token.equals("\t") || token.equals("=")))
continue;
String name = token;
if (name.equals("type")) {
token = tokenizer.nextToken();
while (tokenizer.hasMoreTokens() && (token.equals(" ") || token.equals("\t") || token.equals("="))) token = tokenizer.nextToken();
type = token.substring(1, token.length() - 1);
} else if (name.equals("href")) {
token = tokenizer.nextToken();
while (tokenizer.hasMoreTokens() && (token.equals(" ") || token.equals("\t") || token.equals("="))) token = tokenizer.nextToken();
href = token;
if (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
// already have the next token.
while (token.equals("=") && tokenizer.hasMoreTokens()) {
href = href + token + tokenizer.nextToken();
if (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
lookedAhead = true;
} else {
break;
}
}
}
href = href.substring(1, href.length() - 1);
try {
// Add code to use a URIResolver. Patch from Dmitri Ilyin.
if (m_uriResolver != null) {
source = m_uriResolver.resolve(href, m_baseID);
} else {
href = SystemIDResolver.getAbsoluteURI(href, m_baseID);
source = new SAXSource(new InputSource(href));
}
} catch (TransformerException te) {
throw new org.xml.sax.SAXException(te);
}
} else if (name.equals("title")) {
token = tokenizer.nextToken();
while (tokenizer.hasMoreTokens() && (token.equals(" ") || token.equals("\t") || token.equals("="))) token = tokenizer.nextToken();
title = token.substring(1, token.length() - 1);
} else if (name.equals("media")) {
token = tokenizer.nextToken();
while (tokenizer.hasMoreTokens() && (token.equals(" ") || token.equals("\t") || token.equals("="))) token = tokenizer.nextToken();
media = token.substring(1, token.length() - 1);
} else if (name.equals("charset")) {
token = tokenizer.nextToken();
while (tokenizer.hasMoreTokens() && (token.equals(" ") || token.equals("\t") || token.equals("="))) token = tokenizer.nextToken();
charset = token.substring(1, token.length() - 1);
} else if (name.equals("alternate")) {
token = tokenizer.nextToken();
while (tokenizer.hasMoreTokens() && (token.equals(" ") || token.equals("\t") || token.equals("="))) token = tokenizer.nextToken();
alternate = token.substring(1, token.length() - 1).equals("yes");
}
}
if ((null != type) && (type.equals("text/xsl") || type.equals("text/xml") || type.equals("application/xml+xslt")) && (null != href)) {
if (null != m_media) {
if (null != media) {
if (!media.equals(m_media))
return;
} else
return;
}
if (null != m_charset) {
if (null != charset) {
if (!charset.equals(m_charset))
return;
} else
return;
}
if (null != m_title) {
if (null != title) {
if (!title.equals(m_title))
return;
} else
return;
}
m_stylesheets.addElement(source);
}
}
}
use of javax.xml.transform.sax.SAXSource in project j2objc by google.
the class SourceTreeManager method getXMLReader.
/**
* This method returns the SAX2 parser to use with the InputSource
* obtained from this URI.
* It may return null if any SAX2-conformant XML parser can be used,
* or if getInputSource() will also return null. The parser must
* be free for use (i.e.
* not currently in use for another parse().
*
* @param inputSource The value returned from the URIResolver.
* @return a SAX2 XMLReader to use to resolve the inputSource argument.
* @param locator The location of the original caller, for diagnostic purposes.
*
* @throws TransformerException if the reader can not be created.
*/
public static XMLReader getXMLReader(Source inputSource, SourceLocator locator) throws TransformerException {
try {
XMLReader reader = (inputSource instanceof SAXSource) ? ((SAXSource) inputSource).getXMLReader() : null;
if (null == reader) {
try {
javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
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();
}
try {
reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
} catch (org.xml.sax.SAXException se) {
// What can we do?
// TODO: User diagnostics.
}
return reader;
} catch (org.xml.sax.SAXException se) {
throw new TransformerException(se.getMessage(), locator, se);
}
}
use of javax.xml.transform.sax.SAXSource in project j2objc by google.
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();
}
Aggregations