本文整理匯總了Java中javax.xml.transform.stream.StreamSource類的典型用法代碼示例。如果您正苦於以下問題:Java StreamSource類的具體用法?Java StreamSource怎麽用?Java StreamSource使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
StreamSource類屬於javax.xml.transform.stream包,在下文中一共展示了StreamSource類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testRedirect
import javax.xml.transform.stream.StreamSource; //導入依賴的package包/類
/**
* @bug 8165116
* Verifies that redirect works properly when extension function is enabled
*
* @param xml the XML source
* @param xsl the stylesheet that redirect output to a file
* @param output the output file
* @param redirect the redirect file
* @throws Exception if the test fails
**/
@Test(dataProvider = "redirect")
public void testRedirect(String xml, String xsl, String output, String redirect) throws Exception {
TransformerFactory tf = TransformerFactory.newInstance();
tf.setFeature(ORACLE_ENABLE_EXTENSION_FUNCTION, true);
Transformer t = tf.newTransformer(new StreamSource(new StringReader(xsl)));
//Transform the xml
tryRunWithTmpPermission(
() -> t.transform(new StreamSource(new StringReader(xml)), new StreamResult(new StringWriter())),
new FilePermission(output, "write"), new FilePermission(redirect, "write"));
// Verifies that the output is redirected successfully
String userDir = getSystemProperty("user.dir");
Path pathOutput = Paths.get(userDir, output);
Path pathRedirect = Paths.get(userDir, redirect);
Assert.assertTrue(Files.exists(pathOutput));
Assert.assertTrue(Files.exists(pathRedirect));
System.out.println("Output to " + pathOutput + " successful.");
System.out.println("Redirect to " + pathRedirect + " successful.");
Files.deleteIfExists(pathOutput);
Files.deleteIfExists(pathRedirect);
}
示例2: fromXML
import javax.xml.transform.stream.StreamSource; //導入依賴的package包/類
@Override
public T fromXML(String xml) {
try {
JAXBContext context = JAXBContext.newInstance(type);
Unmarshaller u = context.createUnmarshaller();
if(schemaLocation != null) {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
StreamSource source = new StreamSource(getClass().getResourceAsStream(schemaLocation));
Schema schema = schemaFactory.newSchema(source);
u.setSchema(schema);
}
StringReader reader = new StringReader(xml);
T obj = (T) u.unmarshal(reader);
return obj;
} catch (Exception e) {
System.out.println("ERROR: "+e.toString());
return null;
}
}
示例3: loadXML
import javax.xml.transform.stream.StreamSource; //導入依賴的package包/類
public static Document loadXML(ByteArrayInputStream byteArrayInputStream)
{
try
{
StreamSource streamSource = new StreamSource(byteArrayInputStream);
DocumentBuilderFactory dbf = DocumentBuilderFactoryImpl.newInstance() ;
DocumentBuilder db = dbf.newDocumentBuilder() ;
Document doc = db.newDocument() ;
Result res = new DOMResult(doc) ;
TransformerFactory tr = TransformerFactory.newInstance();
Transformer xformer = tr.newTransformer();
xformer.transform(streamSource, res);
return doc ;
}
catch (Exception e)
{
String csError = e.toString();
Log.logImportant(csError);
Log.logImportant("ERROR while loading XML from byteArrayInputStream "+byteArrayInputStream.toString());
}
return null;
}
示例4: testProperty
import javax.xml.transform.stream.StreamSource; //導入依賴的package包/類
@Test
public void testProperty() throws TransformerConfigurationException {
/* Create a transform factory instance */
TransformerFactory tfactory = TransformerFactory.newInstance();
/* Create a StreamSource instance */
StreamSource streamSource = new StreamSource(new StringReader(xslData));
transformer = tfactory.newTransformer(streamSource);
transformer.setOutputProperty(INDENT, "no");
transformer.setOutputProperty(ENCODING, "UTF-16");
assertEquals(printPropertyValue(INDENT), "indent=no");
assertEquals(printPropertyValue(ENCODING), "encoding=UTF-16");
transformer.setOutputProperties(null);
assertEquals(printPropertyValue(INDENT), "indent=yes");
assertEquals(printPropertyValue(ENCODING), "encoding=UTF-8");
}
示例5: getDataValidator
import javax.xml.transform.stream.StreamSource; //導入依賴的package包/類
@DataProvider(name = "data_ValidatorA")
public Object[][] getDataValidator() {
DOMSource ds = getDOMSource(xml_val_test, xml_val_test_id, true, true, xml_catalog);
SAXSource ss = new SAXSource(new InputSource(xml_val_test));
ss.setSystemId(xml_val_test_id);
StAXSource stax = getStaxSource(xml_val_test, xml_val_test_id, true, true, xml_catalog);
StAXSource stax1 = getStaxSource(xml_val_test, xml_val_test_id, true, true, xml_catalog);
StreamSource source = new StreamSource(new File(xml_val_test));
return new Object[][]{
// use catalog
{true, false, true, ds, null, null, xml_catalog, null},
{false, true, true, ds, null, null, null, xml_catalog},
{true, false, true, ss, null, null, xml_catalog, null},
{false, true, true, ss, null, null, null, xml_catalog},
{true, false, true, stax, null, null, xml_catalog, xml_catalog},
{false, true, true, stax1, null, null, xml_catalog, xml_catalog},
{true, false, true, source, null, null, xml_catalog, null},
{false, true, true, source, null, null, null, xml_catalog},
};
}
示例6: convert
import javax.xml.transform.stream.StreamSource; //導入依賴的package包/類
/**
* Tries to convert the given XML file into an HTML file using the given XSLT stylesheet. If
* this fails (probably because the given XML file doesn't exist),
* {@link #createOfflineFallbackDocumentation(Operator)} will be used to generate a String from
* the old local operator description resources.
*
* @param xmlStream
* @param operator
* @return
* @throws MalformedURLException
* @throws IOException
*/
public static String convert(InputStream xmlStream, Operator operator) throws MalformedURLException, IOException {
if (operator == null) {
throw new IllegalArgumentException("operator must not be null!");
}
if (xmlStream == null) {
LogService.getRoot().finer("Failed to load documentation, using online fallback. Reason: xmlStream is null.");
return createFallbackDocumentation(operator);
}
Source xmlSource = new StreamSource(xmlStream);
try {
return applyXSLTTransformation(xmlSource);
} catch (TransformerException e) {
LogService.getRoot().log(Level.WARNING, "Failed to load documentation, using online fallback.", e);
return createFallbackDocumentation(operator);
}
}
示例7: getSourceImpl
import javax.xml.transform.stream.StreamSource; //導入依賴的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);
}
示例8: validation
import javax.xml.transform.stream.StreamSource; //導入依賴的package包/類
/**
* 利用xsd驗證xml
* @param xsdFile xsdFile
* @param xmlInput xmlInput
* @throws SAXException SAXException
* @throws IOException IOException
*/
public static void validation(String xsdFile, InputStream xmlInput) throws SAXException, IOException
{
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL xsdURL = Validation.class.getClassLoader().getResource(xsdFile);
if(xsdURL != null)
{
Schema schema = factory.newSchema(xsdURL);
Validator validator = schema.newValidator();
// validator.setErrorHandler(new AutoErrorHandler());
Source source = new StreamSource(xmlInput);
try(OutputStream resultOut = new FileOutputStream(new File(PathUtil.getRootDir(), xsdFile + ".xml")))
{
Result result = new StreamResult(resultOut);
validator.validate(source, result);
}
}
else
{
throw new FileNotFoundException(String.format("can not found xsd file [%s] from classpath.", xsdFile));
}
}
示例9: testTransform
import javax.xml.transform.stream.StreamSource; //導入依賴的package包/類
@Test
public final void testTransform() {
try {
StreamSource input = new StreamSource(getClass().getResourceAsStream("PredicateInKeyTest.xml"));
StreamSource stylesheet = new StreamSource(getClass().getResourceAsStream("PredicateInKeyTest.xsl"));
CharArrayWriter buffer = new CharArrayWriter();
StreamResult output = new StreamResult(buffer);
TransformerFactory.newInstance().newTransformer(stylesheet).transform(input, output);
Assert.assertEquals(buffer.toString(), "0|1|2|3", "XSLT xsl:key implementation is broken!");
// expected success
} catch (Exception e) {
// unexpected failure
e.printStackTrace();
Assert.fail(e.toString());
}
}
示例10: createReader
import javax.xml.transform.stream.StreamSource; //導入依賴的package包/類
private XMLReader createReader() throws SAXException {
try {
SAXParserFactory pfactory = SAXParserFactory.newInstance();
pfactory.setValidating(true);
pfactory.setNamespaceAware(true);
// Enable schema validation
SchemaFactory sfactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
InputStream stream = Parser.class.getResourceAsStream("graphdocument.xsd");
pfactory.setSchema(sfactory.newSchema(new Source[]{new StreamSource(stream)}));
return pfactory.newSAXParser().getXMLReader();
} catch (ParserConfigurationException ex) {
throw new SAXException(ex);
}
}
示例11: test
import javax.xml.transform.stream.StreamSource; //導入依賴的package包/類
@Test
public void test() {
try {
SAXParserFactory fac = SAXParserFactory.newInstance();
fac.setNamespaceAware(true);
SAXParser saxParser = fac.newSAXParser();
StreamSource src = new StreamSource(new StringReader(SIMPLE_TESTXML));
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMResult result = new DOMResult();
transformer.transform(src, result);
} catch (Throwable ex) {
// unexpected failure
ex.printStackTrace();
Assert.fail(ex.toString());
}
}
示例12: getSchemaSources
import javax.xml.transform.stream.StreamSource; //導入依賴的package包/類
private Source[] getSchemaSources() throws Exception {
List<Source> list = new ArrayList<Source>();
String file = getClass().getResource("hello_literal.wsdl").getFile();
Source source = new StreamSource(new FileInputStream(file), file);
Transformer trans = TransformerFactory.newInstance().newTransformer();
DOMResult result = new DOMResult();
trans.transform(source, result);
// Look for <xsd:schema> element in wsdl
Element e = ((Document) result.getNode()).getDocumentElement();
NodeList typesList = e.getElementsByTagNameNS("http://schemas.xmlsoap.org/wsdl/", "types");
NodeList schemaList = ((Element) typesList.item(0)).getElementsByTagNameNS("http://www.w3.org/2001/XMLSchema", "schema");
Element elem = (Element) schemaList.item(0);
list.add(new DOMSource(elem, file + "#schema0"));
// trans.transform(new DOMSource(elem), new StreamResult(System.out));
return list.toArray(new Source[list.size()]);
}
示例13: formatXml
import javax.xml.transform.stream.StreamSource; //導入依賴的package包/類
public static String formatXml(String xml) {
String formatted = null;
if (xml == null || xml.trim().length() == 0) {
return formatted;
}
try {
Source xmlInput = new StreamSource(new StringReader(xml));
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
String.valueOf(XML_INDENT));
transformer.transform(xmlInput, xmlOutput);
formatted = xmlOutput.getWriter().toString().replaceFirst(">", ">"
+ XPrinter.lineSeparator);
} catch (Exception e) {
e.printStackTrace();
}
return formatted;
}
示例14: processSource
import javax.xml.transform.stream.StreamSource; //導入依賴的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;
}
}
示例15: render
import javax.xml.transform.stream.StreamSource; //導入依賴的package包/類
/**
* Oveloaded CustomComponent's render
*/
public final void render(final RenderContext ctx) {
StreamSource xslSource = null;
StreamSource xmlSource = null;
try {
xslSource = xslSource();
xmlSource = xmlSource();
final Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource);
final PrintWriter pw = ctx.getWriter();
transformer.transform(xmlSource, new StreamResult(pw));
} catch (Exception e) {
showUnexpectedErrorMsg(ctx, e);
} finally {
closeHard(xslSource);
closeHard(xmlSource);
}
}