1 28 29 package com.caucho.xsl; 30 31 import com.caucho.xml.XmlUtil; 32 33 import org.xml.sax.helpers.DefaultHandler ; 34 35 import java.util.ArrayList ; 36 import java.util.HashMap ; 37 38 41 public class XmlStylesheetReader extends DefaultHandler { 42 private ArrayList _stylesheets = new ArrayList (); 43 44 public XmlStylesheetReader() 45 { 46 } 47 48 public void processingInstruction(String name, String value) 49 { 50 if (! name.equals("xml-stylesheet")) 51 return; 52 53 try { 54 HashMap values = XmlUtil.splitNameList(value); 55 56 String href = (String ) values.get("href"); 57 String media = (String ) values.get("media"); 58 String title = (String ) values.get("title"); 59 String charset = (String ) values.get("charset"); 60 61 if (href == null) 62 return; 63 64 _stylesheets.add(new XmlStylesheet(href, media, title, charset)); 65 } catch (Exception e) { 66 } 67 } 68 69 public String getAssociatedStylesheet(String media, 70 String title, 71 String charset) 72 { 73 String best = null; 74 int bestCost = -1; 75 76 for (int i = 0; i < _stylesheets.size(); i++) { 77 XmlStylesheet ss = (XmlStylesheet) _stylesheets.get(i); 78 79 int cost = ss.match(media, title, charset); 80 81 if (cost > bestCost) { 82 bestCost = cost; 83 best = ss.getSystemId(); 84 } 85 } 86 87 return best; 88 } 89 90 static class XmlStylesheet { 91 private String _systemId; 92 private String _media; 93 private String _title; 94 private String _charset; 95 96 XmlStylesheet(String systemId, String media, String title, String charset) 97 { 98 _systemId = systemId; 99 _media = media; 100 _title = title; 101 _charset = charset; 102 } 103 104 public String getSystemId() 105 { 106 return _systemId; 107 } 108 109 public int match(String media, String title, String charset) 110 { 111 int cost = 1; 112 113 if (_media != null && _media.equals(media)) 114 cost++; 115 else if (_media != null && ! _media.equals(media)) 116 return -1; 117 else if (_media == null && media == null) 118 cost++; 119 120 if (_title != null && _title.equals(title)) 121 cost++; 122 else if (_title != null && ! _title.equals(title)) 123 return -1; 124 else if (_title == null && title == null) 125 cost++; 126 127 if (_charset != null && _charset.equals(charset)) 128 cost++; 129 else if (_charset != null && ! _charset.equals(charset)) 130 return -1; 131 else if (_charset == null && charset == null) 132 cost++; 133 134 return cost; 135 } 136 } 137 } 138 | Popular Tags |