0

So lets asume that we want to make XSLT transformation and we want to use.

 TransformerFactory transFact = TransformerFactory.newInstance(); (1)

So newInstance() create object from abstract class which is impossible in Java. SO what happends behind? How Java changes the implementation. I know that you can use directlly

TransformerFactory transFact = new org.apache.xalan.processor.TransformerFactoryImpl();

but here we have contcrete instance of non-abstract class. Or we can use

System.setProperty("javax.xml.transform.TransformerFactory",    
        "net.sf.saxon.TransformerFactoryImpl"); 

or even the default implementation (1) from com.sun.org.apache.xalan.internal.xsltc.trax;

Designed to be flexible, JAXP allows you to use any XML-compliant parser from within your application. It does this with what is called a pluggability layer, which lets you plug in an implementation of the SAX or DOM API. The pluggability layer also allows you to plug in an XSL processor, letting you control how your XML data is displayed.

How this layer works? Can I write such in my application? To create instace of abstract class?

Xelian
  • 16,680
  • 25
  • 99
  • 152
  • They never create an instance of an abstract class, only an instance of a class that *extends* an abstract class. All this configuration layer around it is to specify what class needs to be instantiated when an instance of an abstract class is requested. – Sergey Kalinichenko Feb 14 '16 at 15:05

2 Answers2

0

The Javadoc of TransformerFactory.newInstance() describes in detail how the concrete implementation of TransformerFactory is determined:

This method uses the following ordered lookup procedure to determine the TransformerFactory implementation class to load:

  • Use the javax.xml.transform.TransformerFactory system property.
  • Use the properties file "lib/jaxp.properties" in the JRE directory. This configuration file is in standard java.util.Properties format and contains the fully qualified name of the implementation class with the key being the system property defined above. The jaxp.properties file is read only once by the JAXP implementation and it's values are then cached for future use. If the file does not exist when the first attempt is made to read from it, no further attempts are made to check for its existence. It is not possible to change the value of any property in jaxp.properties after it has been read for the first time.
  • Use the Services API (as detailed in the JAR specification), if available, to determine the classname. The Services API will look for a classname in the file META-INF/services/javax.xml.transform.TransformerFactory in jars available to the runtime.
  • Platform default TransformerFactory instance.

If you want to integrate a similar factory mechanism in your application I would recommend to take a look at the services API.

wero
  • 32,544
  • 3
  • 59
  • 84
  • OK I saw the flow before but how JDK do it? It uses reflection and then run-time based on these criteria to plug the right implementation? – Xelian Feb 14 '16 at 15:20
  • Xelian: the lookup results in a class name, and the rest is `Class.forName().newInstance()` – wero Feb 14 '16 at 15:22
0

newInstance() is a factory method that creates instance of concrete class that is a subclass of TransformerFactory.

I have not investigated the source code but can assume that at the end of the day it uses Class.forName() to get class and then Class.newInstance() to get instance. As you mentioned yourself there is the factory method uses system property javax.xml.transform.TransformerFactory to get the fully qualified class name of concrete implementation of the TransformerFactory.

AlexR
  • 114,158
  • 16
  • 130
  • 208