KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xsl > XmlStylesheetReader


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xsl;
30
31 import com.caucho.xml.XmlUtil;
32
33 import org.xml.sax.helpers.DefaultHandler JavaDoc;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.HashMap JavaDoc;
37
38 /**
39    * Handler for xml-stylesheet the resin.conf file.
40    */

41 public class XmlStylesheetReader extends DefaultHandler JavaDoc {
42   private ArrayList JavaDoc _stylesheets = new ArrayList JavaDoc();
43
44   public XmlStylesheetReader()
45   {
46   }
47
48   public void processingInstruction(String JavaDoc name, String JavaDoc value)
49   {
50     if (! name.equals("xml-stylesheet"))
51       return;
52
53     try {
54       HashMap JavaDoc values = XmlUtil.splitNameList(value);
55
56       String JavaDoc href = (String JavaDoc) values.get("href");
57       String JavaDoc media = (String JavaDoc) values.get("media");
58       String JavaDoc title = (String JavaDoc) values.get("title");
59       String JavaDoc charset = (String JavaDoc) values.get("charset");
60
61       if (href == null)
62         return;
63
64       _stylesheets.add(new XmlStylesheet(href, media, title, charset));
65     } catch (Exception JavaDoc e) {
66     }
67   }
68
69   public String JavaDoc getAssociatedStylesheet(String JavaDoc media,
70                                         String JavaDoc title,
71                                         String JavaDoc charset)
72   {
73     String JavaDoc 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 JavaDoc _systemId;
92     private String JavaDoc _media;
93     private String JavaDoc _title;
94     private String JavaDoc _charset;
95
96     XmlStylesheet(String JavaDoc systemId, String JavaDoc media, String JavaDoc title, String JavaDoc charset)
97     {
98       _systemId = systemId;
99       _media = media;
100       _title = title;
101       _charset = charset;
102     }
103
104     public String JavaDoc getSystemId()
105     {
106       return _systemId;
107     }
108
109     public int match(String JavaDoc media, String JavaDoc title, String JavaDoc 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