下面列出了javax.xml.transform.dom.DOMSource#setSystemId ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* This test case checks for the getAssociatedStylesheet method
* of TransformerFactory.
* The style sheet returned is then copied to an tfactory01.out
* It will then be verified to see if it matches the golden files.
*
* @throws Exception If any errors occur.
*/
@Test
public void tfactory01() throws Exception {
String outputFile = USER_DIR + "tfactory01.out";
String goldFile = GOLDEN_DIR + "tfactory01GF.out";
String xmlFile = XML_DIR + "TransformerFactoryTest.xml";
String xmlURI = "file:///" + XML_DIR;
try (FileInputStream fis = new FileInputStream(xmlFile);
FileOutputStream fos = new FileOutputStream(outputFile);) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(fis, xmlURI);
DOMSource domSource = new DOMSource(doc);
domSource.setSystemId(xmlURI);
StreamResult streamResult = new StreamResult(fos);
TransformerFactory tFactory = TransformerFactory.newInstance();
Source s = tFactory.getAssociatedStylesheet(domSource, "screen",
"Modern", null);
Transformer t = tFactory.newTransformer();
t.transform(s, streamResult);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
@Override
public Source resolve(String href, String base) throws TransformerException {
Document xslDoc;
try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(href)) {
InputSource xslInputSource = new InputSource(in);
if (dBuilder != null && href.startsWith("xslt")) {
xslDoc = dBuilder.parse(xslInputSource);
DOMSource xslDomSource = new DOMSource(xslDoc);
xslDomSource.setSystemId(href);
return xslDomSource;
}
} catch (SAXException | IOException e) {
logger.error("Failed to load XSLT!", e);
}
return null;
}
@Override
protected void setUp() throws Exception {
super.setUp();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
URL wsdlUri = getClass().getResource("test.wsdl");
Document wsdl = documentBuilder.parse(wsdlUri.openStream());
String wsdlSystemId = wsdlUri.toExternalForm();
DOMSource source = new DOMSource(wsdl);
source.setSystemId(wsdlSystemId);
LocalController controller = new LocalController();
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
wsdlgrammar = WSDLSchemaReader.read(source, factory, controller);
schema = new W3CSchema(wsdlgrammar);
}
/**
* Unit test for XMLReader parsing when relative URI is used in xsl file and
* SystemId was set.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase05() throws Exception {
String outputFile = USER_DIR + "saxtf005.out";
String goldFile = GOLDEN_DIR + "saxtf005GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document = docBuilder.parse(new File(XSLT_INCL_FILE));
Node node = (Node)document;
DOMSource domSource= new DOMSource(node);
domSource.setSystemId("file:///" + XML_DIR);
TransformerHandler handler =
saxTFactory.newTransformerHandler(domSource);
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
/**
* create transformer
* @param stylesheet
* @return
*/
private static Transformer createTransformer(Document stylesheet)
{
if (log.isDebugEnabled())
{
log.debug("createTransformer(Document " + stylesheet + ")");
}
Transformer transformer = null;
TransformerFactory transformerFactory = TransformerFactory.newInstance();
URIResolver resolver = new URIResolver();
transformerFactory.setURIResolver(resolver);
try
{
DOMSource source = new DOMSource(stylesheet);
String systemId = "/xsl";
source.setSystemId(systemId);
transformer = transformerFactory.newTransformer(source);
}
catch (TransformerConfigurationException e)
{
log.error(e.getMessage(), e);
}
return transformer;
}
/**
* Create a transformer from a stylesheet
*
* @param stylesheet Document
*
* @return the Transformer
*/
public static Transformer createTransformer(Document stylesheet)
{
if(log.isDebugEnabled())
{
log.debug("createTransformer(Document " + stylesheet + ")");
}
Transformer transformer = null;
TransformerFactory transformerFactory = TransformerFactory.newInstance();
URIResolver resolver = new URIResolver();
transformerFactory.setURIResolver(resolver);
try
{
DOMSource source = new DOMSource(stylesheet);
String systemId = "/xml/xsl/report";
source.setSystemId(systemId);
transformer = transformerFactory.newTransformer(source);
}
catch(TransformerConfigurationException e)
{
log.error(e.getMessage(), e);
}
return transformer;
}
/**
* create transformer
* @param stylesheet
* @return
*/
private static Transformer createTransformer(Document stylesheet)
{
if (log.isDebugEnabled())
{
log.debug("createTransformer(Document " + stylesheet + ")");
}
Transformer transformer = null;
TransformerFactory transformerFactory = TransformerFactory.newInstance();
URIResolver resolver = new URIResolver();
transformerFactory.setURIResolver(resolver);
try
{
DOMSource source = new DOMSource(stylesheet);
String systemId = "/xsl";
source.setSystemId(systemId);
transformer = transformerFactory.newTransformer(source);
}
catch (TransformerConfigurationException e)
{
log.error(e.getMessage(), e);
}
return transformer;
}
/**
* Create a transformer from a stylesheet
*
* @param stylesheet Document
*
* @return the Transformer
*/
public static Transformer createTransformer(Document stylesheet)
{
if(log.isDebugEnabled())
{
log.debug("createTransformer(Document " + stylesheet + ")");
}
Transformer transformer = null;
TransformerFactory transformerFactory = TransformerFactory.newInstance();
URIResolver resolver = new URIResolver();
transformerFactory.setURIResolver(resolver);
try
{
DOMSource source = new DOMSource(stylesheet);
String systemId = "/xml/xsl/report";
source.setSystemId(systemId);
transformer = transformerFactory.newTransformer(source);
}
catch(TransformerConfigurationException e)
{
log.error(e.getMessage(), e);
}
return transformer;
}
@Override
public Source getSource() {
DOMSource source = new DOMSource(dom);
source.setSystemId(systemId);
return source;
}
@Nullable
public final Document applySchematronValidation (@Nonnull final Node aXMLNode,
@Nullable final String sBaseURI) throws TransformerException
{
ValueEnforcer.notNull (aXMLNode, "XMLNode");
final ISchematronXSLTBasedProvider aXSLTProvider = getXSLTProvider ();
if (aXSLTProvider == null || !aXSLTProvider.isValidSchematron ())
{
// We cannot progress because of invalid Schematron
return null;
}
// Debug print the created XSLT document
if (SchematronDebug.isShowCreatedXSLT ())
LOGGER.info ("Created XSLT document: " + XMLWriter.getNodeAsString (aXSLTProvider.getXSLTDocument ()));
// Create result document
final Document ret = XMLFactory.newDocument ();
// Create the transformer object from the templates specified in the
// constructor
final Transformer aTransformer = aXSLTProvider.getXSLTTransformer ();
// Apply customizations
// Ensure an error listener is present
if (m_aCustomErrorListener != null)
aTransformer.setErrorListener (m_aCustomErrorListener);
else
aTransformer.setErrorListener (new LoggingTransformErrorListener (Locale.US));
// Set the optional URI Resolver
if (m_aCustomURIResolver != null)
aTransformer.setURIResolver (m_aCustomURIResolver);
// Set all custom parameters
if (m_aCustomParameters != null)
for (final Map.Entry <String, ?> aEntry : m_aCustomParameters.entrySet ())
aTransformer.setParameter (aEntry.getKey (), aEntry.getValue ());
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Applying Schematron XSLT on XML [start]");
// Enable this for hardcore Saxon debugging only
if (false)
if (aTransformer.getClass ().getName ().equals ("net.sf.saxon.jaxp.TransformerImpl"))
{
final XsltTransformer aXT = ((TransformerImpl) aTransformer).getUnderlyingXsltTransformer ();
aXT.setMessageListener ( (a,
b,
c,
d) -> LOGGER.info ("MessageListener2: " + a + ", " + b + ", " + c + ", " + d));
aXT.setTraceFunctionDestination (new StandardLogger (System.err));
if (false)
aXT.getUnderlyingController ().setTraceListener (new XSLTTraceListener ());
if (false)
{
final XSLTTraceListener aTL = new XSLTTraceListener ();
aTL.setOutputDestination (new StandardLogger (System.err));
aXT.getUnderlyingController ().setTraceListener (TraceEventMulticaster.add (aTL, null));
}
if (false)
System.out.println ("mode=" + aXT.getInitialMode ());
if (false)
System.out.println ("temp=" + aXT.getInitialTemplate ());
if (false)
System.out.println (aTransformer.getOutputProperties ());
}
// Do the main transformation
{
final DOMSource aSource = new DOMSource (aXMLNode);
aSource.setSystemId (sBaseURI);
aTransformer.transform (aSource, new DOMResult (ret));
}
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Applying Schematron XSLT on XML [end]");
// Debug print the created SVRL document
if (SchematronDebug.isShowCreatedSVRL ())
LOGGER.info ("Created SVRL:\n" + XMLWriter.getNodeAsString (ret));
return ret;
}