Gurpreet Singh
Gurpreet Singh

Reputation: 63

Do we have any saxon specific Transformer class?

My Transformation code snippet is below:

EnterpriseConfiguration config = new EnterpriseConfiguration();
StreamResult xmlOutput = new StreamResult(new StringWriter());
Source xmlInput = new StreamSource(new StringReader(sourceMsg));
EnterpriseTransformerFactory factory = new EnterpriseTransformerFactory();
Transformer transformer = factory.newTransformer(new StreamSource(new File(xsltPath)));
System.out.println("config.isLicenseFound() " + config.isLicenseFound());
transformer.transform(xmlInput,xmlOutput);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
System.out.println("OUTPUT :::-> " + xmlOutput.getWriter().toString());

Where I am using javax.xml.transform.Transformer class, I would like to know is there any tranformer which is specific to Saxon? If yes could suggest me any, Thanks in advance.

Here is the code which i am trying to achieve the functionality:

static HashMap<String, String> mapValues = new HashMap<String, String>();
public static HashMap<String, String> getMapValues() {
        return mapValues;
    }

    public static void setMapValues(HashMap<String, String> mapValues) {
        DecomposeXsl.mapValues = mapValues;
    }

public String transformXsl() throws IOException, SaxonApiException
    {
        mapValues.put("John", "Jbl");
        processor = new Processor(false);  
        xsltCompiler = processor.newXsltCompiler();
        XPathCompiler xpathCompiler = new Processor(false).newXPathCompiler();
        String pageFragment = null;

        StreamSource xslStreamSource = new StreamSource(new File(xsltPath));

          XsltExecutable xsltExecutable = xsltCompiler.compile(xslStreamSource); 
          xsltExecutable = xsltCompiler.compile(xslStreamSource); 
          pageXmlTransformer = xsltExecutable.load();
          XsltTransformer transformer = null;

          transformer = pageXmlTransformer;
          String inputStr = null;
          //StringReader inputStrReader = new StringReader(inputStr); 
          StreamSource xmlDoc = new StreamSource(new File(sourcePath)); 
          Serializer serializer = new Serializer(); 
          serializer.setOutputWriter(new StringWriter()); 
          serializer.setOutputProperty(Serializer.Property.SAXON_STYLESHEET_VERSION, "2.0"); 
          serializer.setOutputProperty(Serializer.Property.METHOD, "xml"); 
          serializer.setOutputProperty(Serializer.Property.MEDIA_TYPE, "text/html"); 
          serializer.setOutputProperty(Serializer.Property.INDENT, "no"); 
          serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes"); 
          serializer.setOutputProperty(Serializer.Property.ENCODING, "utf-8"); 
          transformer.setSource(xmlDoc);   
          transformer.setDestination(serializer); 
          transformer.transform();  
          pageFragment = serializer.getOutputDestination().toString();
          System.out.println("pageFragment "+pageFragment);
        return pageFragment;

    }

Exception i am getting is:

XTDE1425: Cannot find a matching 2-argument function named {java:java.util.Map}get().
  Reflexive calls to Java methods are not available under Saxon-HE
  in built-in template rule
net.sf.saxon.s9api.SaxonApiException: Cannot find a matching 2-argument function named {java:java.util.Map}get(). Reflexive calls to Java methods are not available under Saxon-HE

Here is my xsl code:

<xsl:param name="valMap" as="java:java.util.Map" required="no" select="controller:getMapValues()"/>

<newTag><xsl:value-of select="map:get($valMap, 'John')"/></newTag>

Upvotes: 0

Views: 267

Answers (1)

Michael Kay
Michael Kay

Reputation: 163595

It would be easier to answer if you told us why you need to know.

The literal answer to your question is yes, there is a Saxon-specific class that implements the JAXP Transformer interface. You can find out what it is by displaying the value of transformer.getClass().getName(). But you don't normally need to know what the class name is unless you want to achieve some special effect.

In the above code, creating an EnterpriseConfiguration is redundant, because you get one anyway when you create an EnterpriseTransformerFactory. It would be more sensible to call getConfiguration() on the factory. If you have two different Configuration objects around, you're probably going to get confused when you set properties on one of them and it has no effect.

And one other comment on your code: there's no point setting output properties AFTER doing the transformation: it's too late by then.

Upvotes: 1

Related Questions