本文整理汇总了Java中javax.xml.transform.Source类的典型用法代码示例。如果您正苦于以下问题:Java Source类的具体用法?Java Source怎么用?Java Source使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Source类属于javax.xml.transform包,在下文中一共展示了Source类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createReader
import javax.xml.transform.Source; //导入依赖的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);
}
}
示例2: xmlFormat
import javax.xml.transform.Source; //导入依赖的package包/类
/**
* xml 格式化
*
* @param xml 要格式化的 xml 字符串
* @return 格式化后的新字符串
*/
public static String xmlFormat(String xml) {
if (TextUtils.isEmpty(xml)) {
return "Empty/Null xml content";
}
String message;
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", "2");
transformer.transform(xmlInput, xmlOutput);
message = xmlOutput.getWriter().toString().replaceFirst(">", ">\n");
} catch (TransformerException e) {
message = xml;
}
return message;
}
示例3: format
import javax.xml.transform.Source; //导入依赖的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;
}
}
示例4: toReturnValue
import javax.xml.transform.Source; //导入依赖的package包/类
Object toReturnValue(Packet response) {
try {
Unmarshaller unmarshaller = jaxbcontext.createUnmarshaller();
Message msg = response.getMessage();
switch (mode) {
case PAYLOAD:
return msg.<Object>readPayloadAsJAXB(unmarshaller);
case MESSAGE:
Source result = msg.readEnvelopeAsSource();
return unmarshaller.unmarshal(result);
default:
throw new WebServiceException("Unrecognized dispatch mode");
}
} catch (JAXBException e) {
throw new WebServiceException(e);
}
}
示例5: resolve
import javax.xml.transform.Source; //导入依赖的package包/类
/**
* Reads document description with a given doi from the Scopus API.
*
*
*
*
*/
public Source resolve(String href, String base) throws TransformerException {
String doi = href.substring(href.indexOf(":") + 1);
LOGGER.debug("Reading MCRContent with DOI " + doi);
ScopusConnector connection = new ScopusConnector();
try {
MCRContent content = connection.getPublicationByDOI(doi);
if (content == null) {
return null;
}
MCRXSL2XMLTransformer transformer = new MCRXSL2XMLTransformer("xsl/scopus2mods.xsl");
MCRContent mods = transformer.transform(content);
LOGGER.debug("end resolving " + href);
return mods.getSource();
} catch (IOException e) {
throw new TransformerException(e);
}
}
示例6: xmlToHtml
import javax.xml.transform.Source; //导入依赖的package包/类
/**
* Transforms the XML content to XHTML/HTML format string with the XSL.
*
* @param payload the XML payload to convert
* @param xsltFile the XML stylesheet file
* @return the transformed XHTML/HTML format string
* @throws AlipayApiException problem converting XML to HTML
*/
public static String xmlToHtml(String payload, File xsltFile) throws AlipayApiException {
String result = null;
try {
Source template = new StreamSource(xsltFile);
Transformer transformer = TransformerFactory.newInstance().newTransformer(template);
Properties props = transformer.getOutputProperties();
props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);
transformer.setOutputProperties(props);
StreamSource source = new StreamSource(new StringReader(payload));
StreamResult sr = new StreamResult(new StringWriter());
transformer.transform(source, sr);
result = sr.getWriter().toString();
} catch (TransformerException e) {
throw new AlipayApiException("XML_TRANSFORM_ERROR", e);
}
return result;
}
示例7: getSourceImpl
import javax.xml.transform.Source; //导入依赖的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: formatXml
import javax.xml.transform.Source; //导入依赖的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) {
}
return formatted;
}
示例9: writeDocument
import javax.xml.transform.Source; //导入依赖的package包/类
/**
* Write an XML document to a Writer
*/
public static void writeDocument(Document doc, Writer writer)
throws IOException {
final Source source = new DOMSource(doc);
// Prepare the output file
final Result result = new StreamResult(writer);
// Write the DOM document to the file
try {
final Transformer xformer =
TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$
xformer.transform(source, result);
}
catch (TransformerException e) {
// FIXME: switch to IOException(Throwable) ctor in Java 1.6
throw (IOException) new IOException().initCause(e);
}
}
示例10: formatXml
import javax.xml.transform.Source; //导入依赖的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;
}
示例11: xmlToHtml
import javax.xml.transform.Source; //导入依赖的package包/类
/**
* Transforms the XML content to XHTML/HTML format string with the XSL.
*
* @param payload the XML payload to convert
* @param xsltFile the XML stylesheet file
* @return the transformed XHTML/HTML format string
* @throws AlipayApiException problem converting XML to HTML
*/
public static String xmlToHtml(String payload, File xsltFile)
throws AlipayApiException {
String result = null;
try {
Source template = new StreamSource(xsltFile);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(template);
Properties props = transformer.getOutputProperties();
props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);
transformer.setOutputProperties(props);
StreamSource source = new StreamSource(new StringReader(payload));
StreamResult sr = new StreamResult(new StringWriter());
transformer.transform(source, sr);
result = sr.getWriter().toString();
} catch (TransformerException e) {
throw new AlipayApiException("XML_TRANSFORM_ERROR", e);
}
return result;
}
示例12: putDocumentInCache
import javax.xml.transform.Source; //导入依赖的package包/类
/**
* Put the source tree root node in the document cache.
* TODO: This function needs to be a LOT more sophisticated.
*
* @param n The node to cache.
* @param source The Source object to cache.
*/
public void putDocumentInCache(int n, Source source)
{
int cachedNode = getNode(source);
if (DTM.NULL != cachedNode)
{
if (!(cachedNode == n))
throw new RuntimeException(
"Programmer's Error! "
+ "putDocumentInCache found reparse of doc: "
+ source.getSystemId());
return;
}
if (null != source.getSystemId())
{
m_sourceTree.addElement(new SourceTree(n, source.getSystemId()));
}
}
示例13: soapMessage2String
import javax.xml.transform.Source; //导入依赖的package包/类
/**
* Gets the SOAP message pretty printed as escaped xml for displaying in
* browser
*
* @param msg
* @return
*/
private String soapMessage2String(SOAPMessage msg) {
if (msg == null)
return "";
try (ByteArrayOutputStream streamOut = new ByteArrayOutputStream();) {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "2");
Source sc = msg.getSOAPPart().getContent();
StreamResult result = new StreamResult(streamOut);
transformer.transform(sc, result);
String strMessage = streamOut.toString();
return escapeXmlString(strMessage);
} catch (Exception e) {
System.out.println("Exception in printing SOAP message: "
+ e.getMessage());
return "";
}
}
示例14: test
import javax.xml.transform.Source; //导入依赖的package包/类
@Test
public void test() {
try {
Source source = new StreamSource(util.BOMInputStream.createStream("UTF-16BE", this.getClass().getResourceAsStream("Hello.wsdl.data")),
this.getClass().getResource("Hello.wsdl.data").toExternalForm());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(source, new StreamResult(baos));
System.out.println(new String(baos.toByteArray()));
ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
InputSource inSource = new InputSource(bis);
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(inSource.getSystemId(), inSource.getByteStream());
while (reader.hasNext()) {
reader.next();
}
} catch (Exception ex) {
ex.printStackTrace(System.err);
Assert.fail("Exception occured: " + ex.getMessage());
}
}
示例15: resolve
import javax.xml.transform.Source; //导入依赖的package包/类
/**
* Reads document description from the CrossRef API.
*
*
* @author Eike Spielberg
*
*/
public Source resolve(String href, String base) throws TransformerException {
String id = href.substring(href.indexOf(":") + 1);
MCRContent content = null;
CrossRefConnector connection = new CrossRefConnector();
try {
MCRContent crossRefExport = connection.getPublicationByDOI(id);
content = new MCRStringContent(XML.toString(new JSONObject(crossRefExport.asString()), "content"));
LOGGER.debug("Reading MCRContent with DOI " + id);
MCRXSL2XMLTransformer transformer = new MCRXSL2XMLTransformer("xsl/CrossRef2mods.xsl");
MCRContent mods = transformer.transform(content);
connection.close();
return mods.getSource();
} catch (Exception e) {
throw new TransformerException(e);
}
}