Then let's take a look at Parser (org.xml.sax.parser),
Seeing the Parser's code, you will be disappointed. It turns out that Parser is just an empty shell.
In the figure, you can see that the Parser that looks at the layer is just an interface.
Recall that when you see the resolver code in front
Plant mode
SAXPARSERFAACTORY FACTORY = SAXPARSERFACTORY.NEWINSTANCE ();
Try {
// set up Output Stream
OUT = New OutputStreamWriter (System.out, "UTF-8");
// Parse the Input
SAXPARSER SAXPARSER = factory.newsaxparser ();
SAXPARSER.PARSE (New file (Argv [0]), HANDLER);
The type of Parser generated is completely controlled by SaxParserFactory class. We just take it with it. Secret must be hidden inside.
Seeing the code, discovering that the factory is also a virtual class, and the instance of the factory returned is a realization of the virtual factory.
Go to see the true realization org.apache.crimson.jaxp.saxparserfactoryImpl
It is found that it is Wrap SAXPARSERIMPL. It is understood that SaxParserImpl is a subclass of SAXPARSER.
Continue to track, because SaxParserImpl inherits SAXPARSER, so it also inherits the SAXPARSER method. In SAXPARSERIMPL, there is no place to overwrite the Parser method, so SAXPARSERIMPL's Parser is the Parser of Saxparser, huh, isn't it a little winding? So, how we wrapped around and went back. Take a closer look at SAXPARSER's Parser Method
It can be seen that the Parser Parser inside is called this.getParser () method. Look at the getParser method in SAXPARSER
Is it a little feeling? By the way, this method is to leave the SAXPARSerImpl inherited SaxParser, so that SAXPARSER's subclasses can be free to change the Parser. Just rewrite the getParser method.
Hello to see the method of SaxParserImpl's getParser
You will find that you are still being fooled, here is a very embarrassing code, not what we guess is a real implementation, look carefully. There is such a sentence in the comment: Adapt A Sax2 XmlReader Into A Sax1 Parser.
XmlReader is not very familiar? Think about where I saw it? Yes, just in the SAXPARSER in the SAXPARSER body Wrap, one is the Parser we track, the other is XmlReader. It turns out that this XMLReader is now in the SAX2 parser, and in order to keep the previous system. The SAX1 parser PARSER is retained, but is actually obtained by Wrap of XMLReader. Just an adapte.
Ok, now you can concentrate on the firepower to find XmlReader. With a just experience, it is easy, we found, like Parser, XmlReader is also an interface:
It is easy, we find XMLReaderImpl
And the PARSE method inside:
It can be seen that the part of the selection of PARSE is based on whether or not Validation is required to select different Parser's implementation. The true face of Parser has been found.
Parser2:
ValidatingParser:
In fact, ValidatingParser also inherited a class of Parser2, plus the function of verifying legitimacy.
I found here today. SAX's implementation contains many pattern, here is just the horn of the iceberg, slowly afraid. . .