StylesheetPIHandlerpublic class StylesheetPIHandler extends DefaultHandler Search for the xml-stylesheet processing instructions in an XML document. |
Fields Summary |
---|
String | m_baseIDThe baseID of the document being processed. | String | m_mediaThe desired media criteria. | String | m_titleThe desired title criteria. | String | m_charsetThe desired character set criteria. | Vector | m_stylesheetsA list of SAXSource objects that match the criteria. | URIResolver | m_uriResolverThe object that implements the URIResolver interface,
or null. |
Constructors Summary |
---|
public StylesheetPIHandler(String baseID, String media, String title, String charset)Construct a StylesheetPIHandler instance that will search
for xml-stylesheet PIs based on the given criteria.
m_baseID = baseID;
m_media = media;
m_title = title;
m_charset = charset;
|
Methods Summary |
---|
public javax.xml.transform.Source | getAssociatedStylesheet()Return the last stylesheet found that match the constraints.
int sz = m_stylesheets.size();
if (sz > 0)
{
Source source = (Source) m_stylesheets.elementAt(sz-1);
return source;
}
else
return null;
| public java.lang.String | getBaseId()
return m_baseID ;
| public javax.xml.transform.URIResolver | getURIResolver()Get the object that will be used to resolve URIs in href
in xml-stylesheet processing instruction.
return m_uriResolver;
| public void | processingInstruction(java.lang.String target, java.lang.String data)Handle the xml-stylesheet processing instruction.
if (target.equals("xml-stylesheet"))
{
String href = null; // CDATA #REQUIRED
String type = null; // CDATA #REQUIRED
String title = null; // CDATA #IMPLIED
String media = null; // CDATA #IMPLIED
String charset = null; // CDATA #IMPLIED
boolean alternate = false; // (yes|no) "no"
StringTokenizer tokenizer = new StringTokenizer(data, " \t=\n", true);
boolean lookedAhead = false;
Source source = null;
String token = "";
while (tokenizer.hasMoreTokens())
{
if (!lookedAhead)
token = tokenizer.nextToken();
else
lookedAhead = false;
if (tokenizer.hasMoreTokens() &&
(token.equals(" ") || token.equals("\t") || token.equals("=")))
continue;
String name = token;
if (name.equals("type"))
{
token = tokenizer.nextToken();
while (tokenizer.hasMoreTokens() &&
(token.equals(" " ) || token.equals("\t") || token.equals("=")))
token = tokenizer.nextToken();
type = token.substring(1, token.length() - 1);
}
else if (name.equals("href"))
{
token = tokenizer.nextToken();
while (tokenizer.hasMoreTokens() &&
(token.equals(" " ) || token.equals("\t") || token.equals("=")))
token = tokenizer.nextToken();
href = token;
if (tokenizer.hasMoreTokens())
{
token = tokenizer.nextToken();
// If the href value has parameters to be passed to a
// servlet(something like "foobar?id=12..."),
// we want to make sure we get them added to
// the href value. Without this check, we would move on
// to try to process another attribute and that would be
// wrong.
// We need to set lookedAhead here to flag that we
// already have the next token.
while ( token.equals("=") && tokenizer.hasMoreTokens())
{
href = href + token + tokenizer.nextToken();
if (tokenizer.hasMoreTokens())
{
token = tokenizer.nextToken();
lookedAhead = true;
}
else
{
break;
}
}
}
href = href.substring(1, href.length() - 1);
try
{
// Add code to use a URIResolver. Patch from Dmitri Ilyin.
if (m_uriResolver != null)
{
source = m_uriResolver.resolve(href, m_baseID);
}
else
{
href = SystemIDResolver.getAbsoluteURI(href, m_baseID);
source = new SAXSource(new InputSource(href));
}
}
catch(TransformerException te)
{
throw new org.xml.sax.SAXException(te);
}
}
else if (name.equals("title"))
{
token = tokenizer.nextToken();
while (tokenizer.hasMoreTokens() &&
(token.equals(" " ) || token.equals("\t") || token.equals("=")))
token = tokenizer.nextToken();
title = token.substring(1, token.length() - 1);
}
else if (name.equals("media"))
{
token = tokenizer.nextToken();
while (tokenizer.hasMoreTokens() &&
(token.equals(" " ) || token.equals("\t") || token.equals("=")))
token = tokenizer.nextToken();
media = token.substring(1, token.length() - 1);
}
else if (name.equals("charset"))
{
token = tokenizer.nextToken();
while (tokenizer.hasMoreTokens() &&
(token.equals(" " ) || token.equals("\t") || token.equals("=")))
token = tokenizer.nextToken();
charset = token.substring(1, token.length() - 1);
}
else if (name.equals("alternate"))
{
token = tokenizer.nextToken();
while (tokenizer.hasMoreTokens() &&
(token.equals(" " ) || token.equals("\t") || token.equals("=")))
token = tokenizer.nextToken();
alternate = token.substring(1, token.length()
- 1).equals("yes");
}
}
if ((null != type)
&& (type.equals("text/xsl") || type.equals("text/xml") || type.equals("application/xml+xslt"))
&& (null != href))
{
if (null != m_media)
{
if (null != media)
{
if (!media.equals(m_media))
return;
}
else
return;
}
if (null != m_charset)
{
if (null != charset)
{
if (!charset.equals(m_charset))
return;
}
else
return;
}
if (null != m_title)
{
if (null != title)
{
if (!title.equals(m_title))
return;
}
else
return;
}
m_stylesheets.addElement(source);
}
}
| public void | setBaseId(java.lang.String baseId)Added additional getter and setter methods for the Base Id
to fix bugzilla bug 24187
m_baseID = baseId;
| public void | setURIResolver(javax.xml.transform.URIResolver resolver)Get the object that will be used to resolve URIs in href
in xml-stylesheet processing instruction.
m_uriResolver = resolver;
| public void | startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts)The spec notes that "The xml-stylesheet processing instruction is allowed only in the prolog of an XML document.",
so, at least for right now, I'm going to go ahead an throw a TransformerException
in order to stop the parse.
throw new StopParseException();
|
|