Source Home >> Java Source 1.6.0 >> com.sun.org.apache.xml.internal.utils.StylesheetPIHandler V 0.09
  • 001/*
  • 002 * Copyright 1999-2004 The Apache Software Foundation.
  • 003 *
  • 004 * Licensed under the Apache License, Version 2.0 (the "License");
  • 005 * you may not use this file except in compliance with the License.
  • 006 * You may obtain a copy of the License at
  • 007 *
  • 008 * http://www.apache.org/licenses/LICENSE-2.0
  • 009 *
  • 010 * Unless required by applicable law or agreed to in writing, software
  • 011 * distributed under the License is distributed on an "AS IS" BASIS,
  • 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • 013 * See the License for the specific language governing permissions and
  • 014 * limitations under the License.
  • 015 */
  • 016/*
  • 017 * $Id: StylesheetPIHandler.java,v 1.2.4.1 2005/09/15 08:15:57 suresh_emailid Exp $
  • 018 */
  • 019package com.sun.org.apache.xml.internal.utils;
  • 020
  • 021import java.util.StringTokenizer;
  • 022import java.util.Vector;
  • 023
  • 024import javax.xml.transform.Source;
  • 025import javax.xml.transform.TransformerException;
  • 026import javax.xml.transform.URIResolver;
  • 027import javax.xml.transform.sax.SAXSource;
  • 028
  • 029import com.sun.org.apache.xml.internal.utils.SystemIDResolver;
  • 030
  • 031import org.xml.sax.Attributes;
  • 032import org.xml.sax.InputSource;
  • 033import org.xml.sax.helpers.DefaultHandler;
  • 034
  • 035/**
  • 036 * Search for the xml-stylesheet processing instructions in an XML document.
  • 037 * @see <a href="http://www.w3.org/TR/xml-stylesheet/">Associating Style Sheets with XML documents, Version 1.0</a>
  • 038 */
  • 039public class StylesheetPIHandler extends DefaultHandler
  • 040{
  • 041 /** The baseID of the document being processed. */
  • 042 String m_baseID;
  • 043
  • 044 /** The desired media criteria. */
  • 045 String m_media;
  • 046
  • 047 /** The desired title criteria. */
  • 048 String m_title;
  • 049
  • 050 /** The desired character set criteria. */
  • 051 String m_charset;
  • 052
  • 053 /** A list of SAXSource objects that match the criteria. */
  • 054 Vector m_stylesheets = new Vector();
  • 055
  • 056 // Add code to use a URIResolver. Patch from Dmitri Ilyin.
  • 057
  • 058 /**
  • 059 * The object that implements the URIResolver interface,
  • 060 * or null.
  • 061 */
  • 062 URIResolver m_uriResolver;
  • 063
  • 064 /**
  • 065 * Get the object that will be used to resolve URIs in href
  • 066 * in xml-stylesheet processing instruction.
  • 067 *
  • 068 * @param resolver An object that implements the URIResolver interface,
  • 069 * or null.
  • 070 */
  • 071 public void setURIResolver(URIResolver resolver)
  • 072 {
  • 073 m_uriResolver = resolver;
  • 074 }
  • 075
  • 076 /**
  • 077 * Get the object that will be used to resolve URIs in href
  • 078 * in xml-stylesheet processing instruction.
  • 079 *
  • 080 * @return The URIResolver that was set with setURIResolver.
  • 081 */
  • 082 public URIResolver getURIResolver()
  • 083 {
  • 084 return m_uriResolver;
  • 085 }
  • 086
  • 087 /**
  • 088 * Construct a StylesheetPIHandler instance that will search
  • 089 * for xml-stylesheet PIs based on the given criteria.
  • 090 *
  • 091 * @param baseID The base ID of the XML document, needed to resolve
  • 092 * relative IDs.
  • 093 * @param media The desired media criteria.
  • 094 * @param title The desired title criteria.
  • 095 * @param charset The desired character set criteria.
  • 096 */
  • 097 public StylesheetPIHandler(String baseID, String media, String title,
  • 098 String charset)
  • 099 {
  • 100
  • 101 m_baseID = baseID;
  • 102 m_media = media;
  • 103 m_title = title;
  • 104 m_charset = charset;
  • 105 }
  • 106
  • 107 /**
  • 108 * Return the last stylesheet found that match the constraints.
  • 109 *
  • 110 * @return Source object that references the last stylesheet reference
  • 111 * that matches the constraints.
  • 112 */
  • 113 public Source getAssociatedStylesheet()
  • 114 {
  • 115
  • 116 int sz = m_stylesheets.size();
  • 117
  • 118 if (sz > 0)
  • 119 {
  • 120 Source source = (Source) m_stylesheets.elementAt(sz-1);
  • 121 return source;
  • 122 }
  • 123 else
  • 124 return null;
  • 125 }
  • 126
  • 127 /**
  • 128 * Handle the xml-stylesheet processing instruction.
  • 129 *
  • 130 * @param target The processing instruction target.
  • 131 * @param data The processing instruction data, or null if
  • 132 * none is supplied.
  • 133 * @throws org.xml.sax.SAXException Any SAX exception, possibly
  • 134 * wrapping another exception.
  • 135 * @see org.xml.sax.ContentHandler#processingInstruction
  • 136 * @see <a href="http://www.w3.org/TR/xml-stylesheet/">Associating Style Sheets with XML documents, Version 1.0</a>
  • 137 */
  • 138 public void processingInstruction(String target, String data)
  • 139 throws org.xml.sax.SAXException
  • 140 {
  • 141
  • 142 if (target.equals("xml-stylesheet"))
  • 143 {
  • 144 String href = null; // CDATA #REQUIRED
  • 145 String type = null; // CDATA #REQUIRED
  • 146 String title = null; // CDATA #IMPLIED
  • 147 String media = null; // CDATA #IMPLIED
  • 148 String charset = null; // CDATA #IMPLIED
  • 149 boolean alternate = false; // (yes|no) "no"
  • 150 StringTokenizer tokenizer = new StringTokenizer(data, " \t=\n", true);
  • 151 boolean lookedAhead = false;
  • 152 Source source = null;
  • 153
  • 154 String token = "";
  • 155 while (tokenizer.hasMoreTokens())
  • 156 {
  • 157 if (!lookedAhead)
  • 158 token = tokenizer.nextToken();
  • 159 else
  • 160 lookedAhead = false;
  • 161 if (tokenizer.hasMoreTokens() &&
  • 162 (token.equals(" ") || token.equals("\t") || token.equals("=")))
  • 163 continue;
  • 164
  • 165 String name = token;
  • 166 if (name.equals("type"))
  • 167 {
  • 168 token = tokenizer.nextToken();
  • 169 while (tokenizer.hasMoreTokens() &&
  • 170 (token.equals(" " ) || token.equals("\t") || token.equals("=")))
  • 171 token = tokenizer.nextToken();
  • 172 type = token.substring(1, token.length() - 1);
  • 173
  • 174 }
  • 175 else if (name.equals("href"))
  • 176 {
  • 177 token = tokenizer.nextToken();
  • 178 while (tokenizer.hasMoreTokens() &&
  • 179 (token.equals(" " ) || token.equals("\t") || token.equals("=")))
  • 180 token = tokenizer.nextToken();
  • 181 href = token;
  • 182 if (tokenizer.hasMoreTokens())
  • 183 {
  • 184 token = tokenizer.nextToken();
  • 185 // If the href value has parameters to be passed to a
  • 186 // servlet(something like "foobar?id=12..."),
  • 187 // we want to make sure we get them added to
  • 188 // the href value. Without this check, we would move on
  • 189 // to try to process another attribute and that would be
  • 190 // wrong.
  • 191 // We need to set lookedAhead here to flag that we
  • 192 // already have the next token.
  • 193 while ( token.equals("=") && tokenizer.hasMoreTokens())
  • 194 {
  • 195 href = href + token + tokenizer.nextToken();
  • 196 if (tokenizer.hasMoreTokens())
  • 197 {
  • 198 token = tokenizer.nextToken();
  • 199 lookedAhead = true;
  • 200 }
  • 201 else
  • 202 {
  • 203 break;
  • 204 }
  • 205 }
  • 206 }
  • 207 href = href.substring(1, href.length() - 1);
  • 208 try
  • 209 {
  • 210 // Add code to use a URIResolver. Patch from Dmitri Ilyin.
  • 211 if (m_uriResolver != null)
  • 212 {
  • 213 source = m_uriResolver.resolve(href, m_baseID);
  • 214 }
  • 215 else
  • 216 {
  • 217 href = SystemIDResolver.getAbsoluteURI(href, m_baseID);
  • 218 source = new SAXSource(new InputSource(href));
  • 219 }
  • 220 }
  • 221 catch(TransformerException te)
  • 222 {
  • 223 throw new org.xml.sax.SAXException(te);
  • 224 }
  • 225 }
  • 226 else if (name.equals("title"))
  • 227 {
  • 228 token = tokenizer.nextToken();
  • 229 while (tokenizer.hasMoreTokens() &&
  • 230 (token.equals(" " ) || token.equals("\t") || token.equals("=")))
  • 231 token = tokenizer.nextToken();
  • 232 title = token.substring(1, token.length() - 1);
  • 233 }
  • 234 else if (name.equals("media"))
  • 235 {
  • 236 token = tokenizer.nextToken();
  • 237 while (tokenizer.hasMoreTokens() &&
  • 238 (token.equals(" " ) || token.equals("\t") || token.equals("=")))
  • 239 token = tokenizer.nextToken();
  • 240 media = token.substring(1, token.length() - 1);
  • 241 }
  • 242 else if (name.equals("charset"))
  • 243 {
  • 244 token = tokenizer.nextToken();
  • 245 while (tokenizer.hasMoreTokens() &&
  • 246 (token.equals(" " ) || token.equals("\t") || token.equals("=")))
  • 247 token = tokenizer.nextToken();
  • 248 charset = token.substring(1, token.length() - 1);
  • 249 }
  • 250 else if (name.equals("alternate"))
  • 251 {
  • 252 token = tokenizer.nextToken();
  • 253 while (tokenizer.hasMoreTokens() &&
  • 254 (token.equals(" " ) || token.equals("\t") || token.equals("=")))
  • 255 token = tokenizer.nextToken();
  • 256 alternate = token.substring(1, token.length()
  • 257 - 1).equals("yes");
  • 258 }
  • 259
  • 260 }
  • 261
  • 262 if ((null != type)
  • 263 && (type.equals("text/xsl") || type.equals("text/xml") || type.equals("application/xml+xslt"))
  • 264 && (null != href))
  • 265 {
  • 266 if (null != m_media)
  • 267 {
  • 268 if (null != media)
  • 269 {
  • 270 if (!media.equals(m_media))
  • 271 return;
  • 272 }
  • 273 else
  • 274 return;
  • 275 }
  • 276
  • 277 if (null != m_charset)
  • 278 {
  • 279 if (null != charset)
  • 280 {
  • 281 if (!charset.equals(m_charset))
  • 282 return;
  • 283 }
  • 284 else
  • 285 return;
  • 286 }
  • 287
  • 288 if (null != m_title)
  • 289 {
  • 290 if (null != title)
  • 291 {
  • 292 if (!title.equals(m_title))
  • 293 return;
  • 294 }
  • 295 else
  • 296 return;
  • 297 }
  • 298
  • 299 m_stylesheets.addElement(source);
  • 300 }
  • 301 }
  • 302 }
  • 303
  • 304
  • 305 /**
  • 306 * The spec notes that "The xml-stylesheet processing instruction is allowed only in the prolog of an XML document.",
  • 307 * so, at least for right now, I'm going to go ahead an throw a TransformerException
  • 308 * in order to stop the parse.
  • 309 *
  • 310 * @param namespaceURI The Namespace URI, or an empty string.
  • 311 * @param localName The local name (without prefix), or empty string if not namespace processing.
  • 312 * @param qName The qualified name (with prefix).
  • 313 * @param atts The specified or defaulted attributes.
  • 314 *
  • 315 * @throws StopParseException since there can be no valid xml-stylesheet processing
  • 316 * instructions past the first element.
  • 317 */
  • 318 public void startElement(
  • 319 String namespaceURI, String localName, String qName, Attributes atts)
  • 320 throws org.xml.sax.SAXException
  • 321 {
  • 322 throw new StopParseException();
  • 323 }
  • 324
  • 325 /**
  • 326 * Added additional getter and setter methods for the Base Id
  • 327 * to fix bugzilla bug 24187
  • 328 *
  • 329 */
  • 330 public void setBaseId(String baseId) {
  • 331 m_baseID = baseId;
  • 332
  • 333 }
  • 334 public String getBaseId() {
  • 335 return m_baseID ;
  • 336 }
  • 337
  • 338}

文件:StylesheetPIHandler.java
包名:com.sun.org.apache.xml.internal.utils
类名:StylesheetPIHandler
继承:DefaultHandler
接口: