Java Code Examples for javax.xml.transform.dom.DOMSource#setSystemId()

The following examples show how to use javax.xml.transform.dom.DOMSource#setSystemId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransformerFactoryTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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));
}
 
Example 2
Source File: DocExporter.java    From TranskribusCore with GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example 3
Source File: TestWsdlValidation.java    From woodstox with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 4
Source File: SAXTFactoryTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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));
}
 
Example 5
Source File: RestContentProvider.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: XmlUtil.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 7
Source File: RestContentProvider.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source File: XmlUtil.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 9
Source File: MCRDOMContent.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Source getSource() {
    DOMSource source = new DOMSource(dom);
    source.setSystemId(systemId);
    return source;
}
 
Example 10
Source File: AbstractSchematronXSLTBasedResource.java    From ph-schematron with Apache License 2.0 4 votes vote down vote up
@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;
}