本文整理汇总了Java中javax.xml.transform.sax.SAXSource类的典型用法代码示例。如果您正苦于以下问题:Java SAXSource类的具体用法?Java SAXSource怎么用?Java SAXSource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SAXSource类属于javax.xml.transform.sax包,在下文中一共展示了SAXSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: retrieveJAXB
import javax.xml.transform.sax.SAXSource; //导入依赖的package包/类
/**
* Non-validating
*
* @param path path to input file
* @param _class class of xml root object
* @return root object
*/
public static Object retrieveJAXB(String path, Class _class) {
Object obj = null;
/*
Block parser from reaching out externally see:
https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet#SAXTransformerFactory
*/
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
Source xmlSource = new SAXSource(spf.newSAXParser().getXMLReader(),
new InputSource(new FileReader(new File(path))));
JAXBContext jc = JAXBContext.newInstance(_class);
Unmarshaller um = jc.createUnmarshaller();
obj = um.unmarshal(xmlSource);
} catch (JAXBException | FileNotFoundException | SAXException | ParserConfigurationException e) {
e.printStackTrace();
}
return obj;
}
示例2: testTransformer
import javax.xml.transform.sax.SAXSource; //导入依赖的package包/类
@Test
public void testTransformer() throws TransformerException {
String xml = "<?xml version='1.0'?><root/>";
ReaderStub.used = false;
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
InputSource in = new InputSource(new StringReader(xml));
SAXSource source = new SAXSource(in);
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(source, result);
assertTrue(ReaderStub.used);
}
示例3: xsltprocess
import javax.xml.transform.sax.SAXSource; //导入依赖的package包/类
public void xsltprocess(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException {
// 1. Instantiate a TransformerFactory.
SAXTransformerFactory tFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
// 2. Use the TransformerFactory to process the stylesheet Source and
// generate a Transformer.
InputStream is = getClass().getResourceAsStream("xmg2pol.xsl");
Transformer transformer = tFactory.newTransformer (new StreamSource(is));
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "polarities.dtd,xml");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
// 3. Use the Transformer to transform an XML Source and send the
// output to a Result object.
try {
String input = args[0];
String output= args[1];
SAXSource saxs = new SAXSource(new InputSource(input));
XMLReader saxReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
saxReader.setEntityResolver(new MyEntityResolver());
saxs.setXMLReader(saxReader);
transformer.transform(saxs, new StreamResult(new OutputStreamWriter(new FileOutputStream(output), "utf-8")));
} catch (Exception e) {
e.printStackTrace();
}
}
示例4: saveWithCustomIndetation
import javax.xml.transform.sax.SAXSource; //导入依赖的package包/类
/**
* Saves the xml, contained by the specified input with the custom indentation.
* If the input is the result of jaxb marshalling, make sure to set
* Marshaller.JAXB_FORMATTED_OUTPUT to false in order for this method to work
* properly.
*
* @param input
* @param fos
* @param indentation
*/
public static void saveWithCustomIndetation(ByteArrayInputStream input, FileOutputStream fos, int indentation) {
try {
Transformer transformer = SAXTransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indentation));
Source xmlSource = new SAXSource(new org.xml.sax.InputSource(input));
StreamResult res = new StreamResult(fos);
transformer.transform(xmlSource, res);
fos.flush();
fos.close();
} catch (TransformerFactoryConfigurationError | TransformerException | IOException e) {
log.log(Level.SEVERE, e.getMessage(), e);
}
}
示例5: getSourceImpl
import javax.xml.transform.sax.SAXSource; //导入依赖的package包/类
/**
* Returns a Source for reading the XML value designated by this SQLXML
* instance. <p>
*
* @param sourceClass The class of the source, or null. If null, then a
* DOMSource is returned.
* @return a Source for reading the XML value.
* @throws SQLException if there is an error processing the XML value
* or if the given <tt>sourceClass</tt> is not supported.
*/
protected <T extends Source>T getSourceImpl(
Class<T> sourceClass) throws SQLException {
if (JAXBSource.class.isAssignableFrom(sourceClass)) {
// Must go first presently, since JAXBSource extends SAXSource
// (purely as an implementation detail) and it's not possible
// to instantiate a valid JAXBSource with a Zero-Args
// constructor(or any subclass thereof, due to the finality of
// its private marshaller and context object attributes)
// FALL THROUGH... will throw an exception
} else if (StreamSource.class.isAssignableFrom(sourceClass)) {
return createStreamSource(sourceClass);
} else if ((sourceClass == null)
|| DOMSource.class.isAssignableFrom(sourceClass)) {
return createDOMSource(sourceClass);
} else if (SAXSource.class.isAssignableFrom(sourceClass)) {
return createSAXSource(sourceClass);
} else if (StAXSource.class.isAssignableFrom(sourceClass)) {
return createStAXSource(sourceClass);
}
throw JDBCUtil.invalidArgument("sourceClass: " + sourceClass);
}
示例6: getSourceImpl
import javax.xml.transform.sax.SAXSource; //导入依赖的package包/类
/**
* Returns a Source for reading the XML value designated by this SQLXML
* instance. <p>
*
* @param sourceClass The class of the source, or null. If null, then a
* DOMSource is returned.
* @return a Source for reading the XML value.
* @throws SQLException if there is an error processing the XML value
* or if the given <tt>sourceClass</tt> is not supported.
*/
protected <T extends Source>T getSourceImpl(
Class<T> sourceClass) throws SQLException {
if (JAXBSource.class.isAssignableFrom(sourceClass)) {
// Must go first presently, since JAXBSource extends SAXSource
// (purely as an implmentation detail) and it's not possible
// to instantiate a valid JAXBSource with a Zero-Args
// constructor(or any subclass thereof, due to the finality of
// its private marshaller and context object attrbutes)
// FALL THROUGH... will throw an exception
} else if (StreamSource.class.isAssignableFrom(sourceClass)) {
return createStreamSource(sourceClass);
} else if ((sourceClass == null)
|| DOMSource.class.isAssignableFrom(sourceClass)) {
return createDOMSource(sourceClass);
} else if (SAXSource.class.isAssignableFrom(sourceClass)) {
return createSAXSource(sourceClass);
} else if (StAXSource.class.isAssignableFrom(sourceClass)) {
return createStAXSource(sourceClass);
}
throw Util.invalidArgument("sourceClass: " + sourceClass);
}
示例7: processSource
import javax.xml.transform.sax.SAXSource; //导入依赖的package包/类
protected Source processSource(Source source) {
if (source instanceof StreamSource) {
StreamSource streamSource = (StreamSource) source;
InputSource inputSource = new InputSource(streamSource.getInputStream());
try {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
String featureName = "http://xml.org/sax/features/external-general-entities";
xmlReader.setFeature(featureName, isProcessExternalEntities());
if (!isProcessExternalEntities()) {
xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
}
return new SAXSource(xmlReader, inputSource);
}
catch (SAXException ex) {
logger.warn("Processing of external entities could not be disabled", ex);
return source;
}
}
else {
return source;
}
}
示例8: readInternal
import javax.xml.transform.sax.SAXSource; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
InputStream body = inputMessage.getBody();
if (DOMSource.class.equals(clazz)) {
return (T) readDOMSource(body);
}
else if (SAXSource.class.equals(clazz)) {
return (T) readSAXSource(body);
}
else if (StAXSource.class.equals(clazz)) {
return (T) readStAXSource(body);
}
else if (StreamSource.class.equals(clazz) || Source.class.equals(clazz)) {
return (T) readStreamSource(body);
}
else {
throw new HttpMessageConversionException("Could not read class [" + clazz +
"]. Only DOMSource, SAXSource, StAXSource, and StreamSource are supported.");
}
}
示例9: getSourceImpl
import javax.xml.transform.sax.SAXSource; //导入依赖的package包/类
/**
* Returns a Source for reading the XML value designated by this SQLXML
* instance. <p>
*
* @param sourceClass The class of the source, or null. If null, then a
* DOMSource is returned.
* @return a Source for reading the XML value.
* @throws SQLException if there is an error processing the XML value
* or if the given <tt>sourceClass</tt> is not supported.
*/
protected <T extends Source>T getSourceImpl(
Class<T> sourceClass) throws SQLException {
if (JAXBSource.class.isAssignableFrom(sourceClass)) {
// Must go first presently, since JAXBSource extends SAXSource
// (purely as an implmentation detail) and it's not possible
// to instantiate a valid JAXBSource with a Zero-Args
// constructor(or any subclass thereof, due to the finality of
// its private marshaller and context object attrbutes)
// FALL THROUGH... will throw an exception
} else if (StreamSource.class.isAssignableFrom(sourceClass)) {
return createStreamSource(sourceClass);
} else if ((sourceClass == null)
|| DOMSource.class.isAssignableFrom(sourceClass)) {
return createDOMSource(sourceClass);
} else if (SAXSource.class.isAssignableFrom(sourceClass)) {
return createSAXSource(sourceClass);
} else if (StAXSource.class.isAssignableFrom(sourceClass)) {
return createStAXSource(sourceClass);
}
throw JDBCUtil.invalidArgument("sourceClass: " + sourceClass);
}
示例10: format
import javax.xml.transform.sax.SAXSource; //导入依赖的package包/类
@Override
public String format(String response) {
try {
Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(response.getBytes())));
StreamResult res = new StreamResult(new ByteArrayOutputStream());
serializer.transform(xmlSource, res);
return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray()).trim();
} catch (Exception e) {
e.printStackTrace();
Logger.e(TAG, e.getMessage(), e);
return response;
}
}
示例11: unmarshal
import javax.xml.transform.sax.SAXSource; //导入依赖的package包/类
@Override
public <T> JAXBElement<T> unmarshal( Source source, Class<T> expectedType ) throws JAXBException {
if (source instanceof SAXSource) {
SAXSource ss = (SAXSource) source;
XMLReader locReader = ss.getXMLReader();
if (locReader == null) {
locReader = getXMLReader();
}
return unmarshal(locReader, ss.getInputSource(), expectedType);
}
if (source instanceof StreamSource) {
return unmarshal(getXMLReader(), streamSourceToInputSource((StreamSource) source), expectedType);
}
if (source instanceof DOMSource) {
return unmarshal(((DOMSource) source).getNode(), expectedType);
}
// we don't handle other types of Source
throw new IllegalArgumentException();
}
示例12: unmarshal0
import javax.xml.transform.sax.SAXSource; //导入依赖的package包/类
public Object unmarshal0( Source source, JaxBeanInfo expectedType ) throws JAXBException {
if (source instanceof SAXSource) {
SAXSource ss = (SAXSource) source;
XMLReader locReader = ss.getXMLReader();
if (locReader == null) {
locReader = getXMLReader();
}
return unmarshal0(locReader, ss.getInputSource(), expectedType);
}
if (source instanceof StreamSource) {
return unmarshal0(getXMLReader(), streamSourceToInputSource((StreamSource) source), expectedType);
}
if (source instanceof DOMSource) {
return unmarshal0(((DOMSource) source).getNode(), expectedType);
}
// we don't handle other types of Source
throw new IllegalArgumentException();
}
示例13: unmarshal
import javax.xml.transform.sax.SAXSource; //导入依赖的package包/类
public Object unmarshal( Source source ) throws JAXBException {
if( source == null ) {
throw new IllegalArgumentException(
Messages.format( Messages.MUST_NOT_BE_NULL, "source" ) );
}
if(source instanceof SAXSource)
return unmarshal( (SAXSource)source );
if(source instanceof StreamSource)
return unmarshal( streamSourceToInputSource((StreamSource)source));
if(source instanceof DOMSource)
return unmarshal( ((DOMSource)source).getNode() );
// we don't handle other types of Source
throw new IllegalArgumentException();
}
示例14: testSAXTransformerFactory
import javax.xml.transform.sax.SAXSource; //导入依赖的package包/类
@Test
public void testSAXTransformerFactory() throws TransformerConfigurationException {
final String xsl = "<?xml version='1.0'?>\n" + "<xsl:stylesheet" + " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" + " version='1.0'>\n"
+ " <xsl:template match='/'>Hello World!</xsl:template>\n" + "</xsl:stylesheet>\n";
ReaderStub.used = false;
TransformerFactory transFactory = TransformerFactory.newInstance();
assertTrue(transFactory.getFeature(SAXTransformerFactory.FEATURE));
InputSource in = new InputSource(new StringReader(xsl));
SAXSource source = new SAXSource(in);
transFactory.newTransformer(source);
assertTrue(ReaderStub.used);
}
示例15: loadDocument
import javax.xml.transform.sax.SAXSource; //导入依赖的package包/类
/**
* Loads the document and updates build-time (latency) statistics
*/
public void loadDocument(String uri) {
try {
final long stamp = System.currentTimeMillis();
_dom = (DOMEnhancedForDTM)_dtmManager.getDTM(
new SAXSource(_reader, new InputSource(uri)),
false, null, true, false);
_dom.setDocumentURI(uri);
// The build time can be used for statistics for a better
// priority algorithm (currently round robin).
final long thisTime = System.currentTimeMillis() - stamp;
if (_buildTime > 0)
_buildTime = (_buildTime + thisTime) >>> 1;
else
_buildTime = thisTime;
}
catch (Exception e) {
_dom = null;
}
}