001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xsl;
030:
031: import com.caucho.xml.XmlUtil;
032:
033: import org.xml.sax.helpers.DefaultHandler;
034:
035: import java.util.ArrayList;
036: import java.util.HashMap;
037:
038: /**
039: * Handler for xml-stylesheet the resin.conf file.
040: */
041: public class XmlStylesheetReader extends DefaultHandler {
042: private ArrayList _stylesheets = new ArrayList();
043:
044: public XmlStylesheetReader() {
045: }
046:
047: public void processingInstruction(String name, String value) {
048: if (!name.equals("xml-stylesheet"))
049: return;
050:
051: try {
052: HashMap values = XmlUtil.splitNameList(value);
053:
054: String href = (String) values.get("href");
055: String media = (String) values.get("media");
056: String title = (String) values.get("title");
057: String charset = (String) values.get("charset");
058:
059: if (href == null)
060: return;
061:
062: _stylesheets.add(new XmlStylesheet(href, media, title,
063: charset));
064: } catch (Exception e) {
065: }
066: }
067:
068: public String getAssociatedStylesheet(String media, String title,
069: String charset) {
070: String best = null;
071: int bestCost = -1;
072:
073: for (int i = 0; i < _stylesheets.size(); i++) {
074: XmlStylesheet ss = (XmlStylesheet) _stylesheets.get(i);
075:
076: int cost = ss.match(media, title, charset);
077:
078: if (cost > bestCost) {
079: bestCost = cost;
080: best = ss.getSystemId();
081: }
082: }
083:
084: return best;
085: }
086:
087: static class XmlStylesheet {
088: private String _systemId;
089: private String _media;
090: private String _title;
091: private String _charset;
092:
093: XmlStylesheet(String systemId, String media, String title,
094: String charset) {
095: _systemId = systemId;
096: _media = media;
097: _title = title;
098: _charset = charset;
099: }
100:
101: public String getSystemId() {
102: return _systemId;
103: }
104:
105: public int match(String media, String title, String charset) {
106: int cost = 1;
107:
108: if (_media != null && _media.equals(media))
109: cost++;
110: else if (_media != null && !_media.equals(media))
111: return -1;
112: else if (_media == null && media == null)
113: cost++;
114:
115: if (_title != null && _title.equals(title))
116: cost++;
117: else if (_title != null && !_title.equals(title))
118: return -1;
119: else if (_title == null && title == null)
120: cost++;
121:
122: if (_charset != null && _charset.equals(charset))
123: cost++;
124: else if (_charset != null && !_charset.equals(charset))
125: return -1;
126: else if (_charset == null && charset == null)
127: cost++;
128:
129: return cost;
130: }
131: }
132: }
|