1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: StylesheetPIHandler.java,v 1.2 2004/02/17 04:21:14 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. import java.util.StringTokenizer;
  21. import java.util.Vector;
  22. import javax.xml.transform.Source;
  23. import javax.xml.transform.TransformerException;
  24. import javax.xml.transform.URIResolver;
  25. import javax.xml.transform.sax.SAXSource;
  26. import com.sun.org.apache.xml.internal.utils.SystemIDResolver;
  27. import org.xml.sax.Attributes;
  28. import org.xml.sax.InputSource;
  29. import org.xml.sax.helpers.DefaultHandler;
  30. /**
  31. * Search for the xml-stylesheet processing instructions in an XML document.
  32. * @see <a href="http://www.w3.org/TR/xml-stylesheet/">Associating Style Sheets with XML documents, Version 1.0</a>
  33. */
  34. public class StylesheetPIHandler extends DefaultHandler
  35. {
  36. /** The baseID of the document being processed. */
  37. String m_baseID;
  38. /** The desired media criteria. */
  39. String m_media;
  40. /** The desired title criteria. */
  41. String m_title;
  42. /** The desired character set criteria. */
  43. String m_charset;
  44. /** A list of SAXSource objects that match the criteria. */
  45. Vector m_stylesheets = new Vector();
  46. // Add code to use a URIResolver. Patch from Dmitri Ilyin.
  47. /**
  48. * The object that implements the URIResolver interface,
  49. * or null.
  50. */
  51. URIResolver m_uriResolver;
  52. /**
  53. * Get the object that will be used to resolve URIs in href
  54. * in xml-stylesheet processing instruction.
  55. *
  56. * @param resolver An object that implements the URIResolver interface,
  57. * or null.
  58. */
  59. public void setURIResolver(URIResolver resolver)
  60. {
  61. m_uriResolver = resolver;
  62. }
  63. /**
  64. * Get the object that will be used to resolve URIs in href
  65. * in xml-stylesheet processing instruction.
  66. *
  67. * @return The URIResolver that was set with setURIResolver.
  68. */
  69. public URIResolver getURIResolver()
  70. {
  71. return m_uriResolver;
  72. }
  73. /**
  74. * Construct a StylesheetPIHandler instance that will search
  75. * for xml-stylesheet PIs based on the given criteria.
  76. *
  77. * @param baseID The base ID of the XML document, needed to resolve
  78. * relative IDs.
  79. * @param media The desired media criteria.
  80. * @param title The desired title criteria.
  81. * @param charset The desired character set criteria.
  82. */
  83. public StylesheetPIHandler(String baseID, String media, String title,
  84. String charset)
  85. {
  86. m_baseID = baseID;
  87. m_media = media;
  88. m_title = title;
  89. m_charset = charset;
  90. }
  91. /**
  92. * Return the last stylesheet found that match the constraints.
  93. *
  94. * @return Source object that references the last stylesheet reference
  95. * that matches the constraints.
  96. */
  97. public Source getAssociatedStylesheet()
  98. {
  99. int sz = m_stylesheets.size();
  100. if (sz > 0)
  101. {
  102. Source source = (Source) m_stylesheets.elementAt(sz-1);
  103. return source;
  104. }
  105. else
  106. return null;
  107. }
  108. /**
  109. * Handle the xml-stylesheet processing instruction.
  110. *
  111. * @param target The processing instruction target.
  112. * @param data The processing instruction data, or null if
  113. * none is supplied.
  114. * @throws org.xml.sax.SAXException Any SAX exception, possibly
  115. * wrapping another exception.
  116. * @see org.xml.sax.ContentHandler#processingInstruction
  117. * @see <a href="http://www.w3.org/TR/xml-stylesheet/">Associating Style Sheets with XML documents, Version 1.0</a>
  118. */
  119. public void processingInstruction(String target, String data)
  120. throws org.xml.sax.SAXException
  121. {
  122. if (target.equals("xml-stylesheet"))
  123. {
  124. String href = null; // CDATA #REQUIRED
  125. String type = null; // CDATA #REQUIRED
  126. String title = null; // CDATA #IMPLIED
  127. String media = null; // CDATA #IMPLIED
  128. String charset = null; // CDATA #IMPLIED
  129. boolean alternate = false; // (yes|no) "no"
  130. StringTokenizer tokenizer = new StringTokenizer(data, " \t=\n", true);
  131. boolean lookedAhead = false;
  132. Source source = null;
  133. String token = "";
  134. while (tokenizer.hasMoreTokens())
  135. {
  136. if (!lookedAhead)
  137. token = tokenizer.nextToken();
  138. else
  139. lookedAhead = false;
  140. if (tokenizer.hasMoreTokens() &&
  141. (token.equals(" ") || token.equals("\t") || token.equals("=")))
  142. continue;
  143. String name = token;
  144. if (name.equals("type"))
  145. {
  146. token = tokenizer.nextToken();
  147. while (tokenizer.hasMoreTokens() &&
  148. (token.equals(" " ) || token.equals("\t") || token.equals("=")))
  149. token = tokenizer.nextToken();
  150. type = token.substring(1, token.length() - 1);
  151. }
  152. else if (name.equals("href"))
  153. {
  154. token = tokenizer.nextToken();
  155. while (tokenizer.hasMoreTokens() &&
  156. (token.equals(" " ) || token.equals("\t") || token.equals("=")))
  157. token = tokenizer.nextToken();
  158. href = token;
  159. if (tokenizer.hasMoreTokens())
  160. {
  161. token = tokenizer.nextToken();
  162. // If the href value has parameters to be passed to a
  163. // servlet(something like "foobar?id=12..."),
  164. // we want to make sure we get them added to
  165. // the href value. Without this check, we would move on
  166. // to try to process another attribute and that would be
  167. // wrong.
  168. // We need to set lookedAhead here to flag that we
  169. // already have the next token.
  170. while ( token.equals("=") && tokenizer.hasMoreTokens())
  171. {
  172. href = href + token + tokenizer.nextToken();
  173. if (tokenizer.hasMoreTokens())
  174. {
  175. token = tokenizer.nextToken();
  176. lookedAhead = true;
  177. }
  178. else
  179. {
  180. break;
  181. }
  182. }
  183. }
  184. href = href.substring(1, href.length() - 1);
  185. try
  186. {
  187. // Add code to use a URIResolver. Patch from Dmitri Ilyin.
  188. if (m_uriResolver != null)
  189. {
  190. source = m_uriResolver.resolve(href, m_baseID);
  191. }
  192. else
  193. {
  194. href = SystemIDResolver.getAbsoluteURI(href, m_baseID);
  195. source = new SAXSource(new InputSource(href));
  196. }
  197. }
  198. catch(TransformerException te)
  199. {
  200. throw new org.xml.sax.SAXException(te);
  201. }
  202. }
  203. else if (name.equals("title"))
  204. {
  205. token = tokenizer.nextToken();
  206. while (tokenizer.hasMoreTokens() &&
  207. (token.equals(" " ) || token.equals("\t") || token.equals("=")))
  208. token = tokenizer.nextToken();
  209. title = token.substring(1, token.length() - 1);
  210. }
  211. else if (name.equals("media"))
  212. {
  213. token = tokenizer.nextToken();
  214. while (tokenizer.hasMoreTokens() &&
  215. (token.equals(" " ) || token.equals("\t") || token.equals("=")))
  216. token = tokenizer.nextToken();
  217. media = token.substring(1, token.length() - 1);
  218. }
  219. else if (name.equals("charset"))
  220. {
  221. token = tokenizer.nextToken();
  222. while (tokenizer.hasMoreTokens() &&
  223. (token.equals(" " ) || token.equals("\t") || token.equals("=")))
  224. token = tokenizer.nextToken();
  225. charset = token.substring(1, token.length() - 1);
  226. }
  227. else if (name.equals("alternate"))
  228. {
  229. token = tokenizer.nextToken();
  230. while (tokenizer.hasMoreTokens() &&
  231. (token.equals(" " ) || token.equals("\t") || token.equals("=")))
  232. token = tokenizer.nextToken();
  233. alternate = token.substring(1, token.length()
  234. - 1).equals("yes");
  235. }
  236. }
  237. if ((null != type)
  238. && (type.equals("text/xsl") || type.equals("text/xml") || type.equals("application/xml+xslt"))
  239. && (null != href))
  240. {
  241. if (null != m_media)
  242. {
  243. if (null != media)
  244. {
  245. if (!media.equals(m_media))
  246. return;
  247. }
  248. else
  249. return;
  250. }
  251. if (null != m_charset)
  252. {
  253. if (null != charset)
  254. {
  255. if (!charset.equals(m_charset))
  256. return;
  257. }
  258. else
  259. return;
  260. }
  261. if (null != m_title)
  262. {
  263. if (null != title)
  264. {
  265. if (!title.equals(m_title))
  266. return;
  267. }
  268. else
  269. return;
  270. }
  271. m_stylesheets.addElement(source);
  272. }
  273. }
  274. }
  275. /**
  276. * The spec notes that "The xml-stylesheet processing instruction is allowed only in the prolog of an XML document.",
  277. * so, at least for right now, I'm going to go ahead an throw a TransformerException
  278. * in order to stop the parse.
  279. *
  280. * @param uri The Namespace URI, or an empty string.
  281. * @param localName The local name (without prefix), or empty string if not namespace processing.
  282. * @param rawName The qualified name (with prefix).
  283. * @param attributes The specified or defaulted attributes.
  284. *
  285. * @throws StopParseException since there can be no valid xml-stylesheet processing
  286. * instructions past the first element.
  287. */
  288. public void startElement(
  289. String namespaceURI, String localName, String qName, Attributes atts)
  290. throws org.xml.sax.SAXException
  291. {
  292. throw new StopParseException();
  293. }
  294. /**
  295. * Added additional getter and setter methods for the Base Id
  296. * to fix bugzilla bug 24187
  297. *
  298. */
  299. public void setBaseId(String baseId) {
  300. m_baseID = baseId;
  301. }
  302. public String getBaseId() {
  303. return m_baseID ;
  304. }
  305. }