KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > event > XHTMLURIEscaper


1 package net.sf.saxon.event;
2
3 import net.sf.saxon.om.NamePool;
4 import net.sf.saxon.om.NamespaceConstant;
5 import net.sf.saxon.trans.XPathException;
6
7 import java.util.HashSet JavaDoc;
8
9
10 /**
11  * This class performs URI escaping for the XHTML output method. The logic for performing escaping
12  * is the same as the HTML output method, but the way in which attributes are identified for escaping
13  * is different, because XHTML is case-sensitive.
14  */

15
16 public class XHTMLURIEscaper extends HTMLURIEscaper {
17
18     /**
19     * Table of attributes whose value is a URL
20     */

21
22     private HashSet JavaDoc urlTable;
23
24     private synchronized void buildURIAttributeTable() {
25         // Reuse the attribute table for all XHTMLEmitters sharing the same namepool
26
NamePool pool = getPipelineConfiguration().getConfiguration().getNamePool();
27         urlTable = (HashSet JavaDoc)pool.getClientData(this.getClass());
28         if (urlTable == null) {
29             urlTable = new HashSet JavaDoc(40);
30             pool.setClientData(this.getClass(), urlTable);
31         }
32         setUrlAttribute(pool, "form", "action");
33         setUrlAttribute(pool, "body", "background");
34         setUrlAttribute(pool, "q", "cite");
35         setUrlAttribute(pool, "blockquote", "cite");
36         setUrlAttribute(pool, "del", "cite");
37         setUrlAttribute(pool, "ins", "cite");
38         setUrlAttribute(pool, "object", "classid");
39         setUrlAttribute(pool, "object", "codebase");
40         setUrlAttribute(pool, "applet", "codebase");
41         setUrlAttribute(pool, "object", "data");
42         setUrlAttribute(pool, "a", "href");
43         setUrlAttribute(pool, "a", "name"); // see second note in section B.2.1 of HTML 4 specification
44
setUrlAttribute(pool, "area", "href");
45         setUrlAttribute(pool, "link", "href");
46         setUrlAttribute(pool, "base", "href");
47         setUrlAttribute(pool, "img", "longdesc");
48         setUrlAttribute(pool, "frame", "longdesc");
49         setUrlAttribute(pool, "iframe", "longdesc");
50         setUrlAttribute(pool, "head", "profile");
51         setUrlAttribute(pool, "script", "src");
52         setUrlAttribute(pool, "input", "src");
53         setUrlAttribute(pool, "frame", "src");
54         setUrlAttribute(pool, "iframe", "src");
55         setUrlAttribute(pool, "img", "src");
56         setUrlAttribute(pool, "img", "usemap");
57         setUrlAttribute(pool, "input", "usemap");
58         setUrlAttribute(pool, "object", "usemap");
59     }
60
61     private void setUrlAttribute(NamePool pool, String JavaDoc element, String JavaDoc attribute) {
62         int elcode = pool.allocate("", NamespaceConstant.XHTML, element) & NamePool.FP_MASK;
63         int atcode = pool.allocate("", "", attribute) & NamePool.FP_MASK;
64         Long JavaDoc key = new Long JavaDoc(((long)elcode)<<32 | (long)atcode);
65         urlTable.add(key);
66     }
67
68     /**
69      * Determine whether a given attribute is a URL attribute
70      */

71
72     private boolean isURLAttribute(int elcode, int atcode) {
73         elcode = elcode & NamePool.FP_MASK;
74         atcode = atcode & NamePool.FP_MASK;
75         Long JavaDoc key = new Long JavaDoc(((long)elcode)<<32 | (long)atcode);
76         return urlTable.contains(key);
77     }
78
79     /**
80      * Do the real work of starting the document. This happens when the first
81      * content is written.
82      *
83      * @throws net.sf.saxon.trans.XPathException
84      *
85      */

86
87     public void open() throws XPathException {
88         super.open();
89         if (escapeURIAttributes) {
90             buildURIAttributeTable();
91         }
92     }
93
94     /**
95      * Notify an attribute. Attributes are notified after the startElement event, and before any
96      * children. Namespaces and attributes may be intermingled.
97      *
98      * @param nameCode The name of the attribute, as held in the name pool
99      * @param typeCode The type of the attribute, as held in the name pool
100      * @param properties Bit significant value. The following bits are defined:
101      * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
102      * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
103      * @throws IllegalStateException: attempt to output an attribute when there is no open element
104      * start tag
105      */

106
107     public void attribute(int nameCode, int typeCode, CharSequence JavaDoc value, int locationId, int properties) throws XPathException {
108         if (escapeURIAttributes &&
109                 isURLAttribute(currentElement, nameCode) &&
110                 (properties & ReceiverOptions.DISABLE_ESCAPING) == 0) {
111             getUnderlyingReceiver().attribute(
112                     nameCode, typeCode, HTMLEmitter.escapeURL(value), locationId,
113                     properties | ReceiverOptions.DISABLE_CHARACTER_MAPS);
114         } else {
115             getUnderlyingReceiver().attribute(
116                     nameCode, typeCode, value, locationId, properties);
117         }
118     }
119
120 }
121
122 //
123
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
124
// you may not use this file except in compliance with the License. You may obtain a copy of the
125
// License at http://www.mozilla.org/MPL/
126
//
127
// Software distributed under the License is distributed on an "AS IS" basis,
128
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
129
// See the License for the specific language governing rights and limitations under the License.
130
//
131
// The Original Code is: all this file.
132
//
133
// The Initial Developer of the Original Code is Michael H. Kay.
134
//
135
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
136
//
137
// Contributor(s): none.
138
//
139
Popular Tags