1 16 19 package org.apache.xml.utils; 20 21 import java.util.StringTokenizer ; 22 import java.util.Vector ; 23 24 import javax.xml.transform.Source ; 25 import javax.xml.transform.TransformerException ; 26 import javax.xml.transform.URIResolver ; 27 import javax.xml.transform.sax.SAXSource ; 28 29 import org.apache.xml.utils.SystemIDResolver; 30 31 import org.xml.sax.Attributes ; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.helpers.DefaultHandler ; 34 35 39 public class StylesheetPIHandler extends DefaultHandler 40 { 41 42 String m_baseID; 43 44 45 String m_media; 46 47 48 String m_title; 49 50 51 String m_charset; 52 53 54 Vector m_stylesheets = new Vector (); 55 56 58 62 URIResolver m_uriResolver; 63 64 71 public void setURIResolver(URIResolver resolver) 72 { 73 m_uriResolver = resolver; 74 } 75 76 82 public URIResolver getURIResolver() 83 { 84 return m_uriResolver; 85 } 86 87 97 public StylesheetPIHandler(String baseID, String media, String title, 98 String charset) 99 { 100 101 m_baseID = baseID; 102 m_media = media; 103 m_title = title; 104 m_charset = charset; 105 } 106 107 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 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; String type = null; String title = null; String media = null; String charset = null; boolean alternate = false; 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 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 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 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 330 public void setBaseId(String baseId) { 331 m_baseID = baseId; 332 333 } 334 public String getBaseId() { 335 return m_baseID ; 336 } 337 338 } 339 | Popular Tags |