KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > xml > xlink > ExtendedXLinkPipe


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.xml.xlink;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import org.xml.sax.Attributes JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25 import org.xml.sax.helpers.AttributesImpl JavaDoc;
26
27 /**
28  * This class extends the XLink semantic capabilities to understand those
29  * elements that are have default linking semantics associated.
30  *
31  * <p>This class reacts on 'href' and 'src' attributes and is able to understand
32  * the semantics of XHTML/WML/SMIL/SVG and all the rest of the languages that
33  * use either XLink of the above attributes.</p>
34  *
35  * <p>NOTE: this class is clearly a hack and is not future compatible, but
36  * since many XML formats to date are not compatible with the XLink semantics
37  * this is what we have to do to live in the bleeding edge. Once there will
38  * be a way to remove this, that will be a happy day for XML and for Cocoon too.</p>
39  *
40  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
41  * @author <a HREF="mailto:tk-cocoon@datas-world.de">Torsten Knodt</a>
42  * @version CVS $Id: ExtendedXLinkPipe.java 30932 2004-07-29 17:35:38Z vgritsenko $
43  */

44 public abstract class ExtendedXLinkPipe extends XLinkPipe {
45
46     protected static Set JavaDoc arrayToSet(Object JavaDoc[] array) {
47         final Set JavaDoc set = new HashSet JavaDoc(array.length);
48
49         for (int i = 0; i < array.length; i++)
50             set.add(array[i]);
51         return set;
52     }
53     
54     private final Map JavaDoc MAP = new HashMap JavaDoc() {
55         {
56             put(
57                 "",
58                 arrayToSet(
59                     new String JavaDoc[] {
60                         "about",
61                         "action",
62                         "background",
63                         "data",
64                         "discuri",
65                         "href",
66                         "longdesc",
67                         "onenterforward",
68                         "onenterbackward",
69                         "ontimer",
70                         "onpick",
71                         "src" }));
72             put(
73                 "http://www.w3.org/1999/xhtml",
74                 arrayToSet(
75                     new String JavaDoc[] {
76                         "action",
77                         "background",
78                         "data",
79                         "href",
80                         "longdesc",
81                         "src" }));
82             put(
83                 "http://www.w3.org/2001/XInclude",
84                 arrayToSet(new String JavaDoc[] { "href" }));
85             put(
86                 "http://www.wapforum.org/2001/wml",
87                 arrayToSet(
88                     new String JavaDoc[] {
89                         "onenterforward",
90                         "onenterbackward",
91                         "ontimer",
92                         "href",
93                         "onpick",
94                         "src" }));
95             put(
96                 "http://www.w3.org/2002/01/P3Pv1",
97                 arrayToSet(
98                     new String JavaDoc[] { "about", "discuri", "src", "service" }));
99         }
100     };
101
102     private int attrIndex = -1;
103
104     public void startElement(
105         String JavaDoc uri,
106         final String JavaDoc name,
107         final String JavaDoc raw,
108         final Attributes JavaDoc attr)
109         throws SAXException JavaDoc {
110         final Set JavaDoc attrList = (Set JavaDoc) MAP.get((uri == null) ? "" : uri);
111
112         if (attrList != null) {
113             for (int i = attrIndex + 1; i < attr.getLength(); i++)
114                 if (attr.getURI(i).equals("")
115                     && attrList.contains(attr.getLocalName(i))) {
116
117                     final String JavaDoc att = attr.getValue(i);
118
119                     if (att != null) {
120                         final String JavaDoc str =
121                             ": URI="
122                                 + uri
123                                 + " NAME="
124                                 + name
125                                 + " RAW="
126                                 + raw
127                                 + " ATT="
128                                 + attr.getLocalName(i)
129                                 + " NS="
130                                 + uri
131                                 + " VALUE="
132                                 + att;
133
134                         if (getLogger().isDebugEnabled())
135                            getLogger().debug("Transforming to XLink" + str);
136                            
137                         attrIndex = i;
138                         
139                         simpleLink(
140                             att,
141                             null,
142                             null,
143                             null,
144                             null,
145                             null,
146                             uri,
147                             name,
148                             raw,
149                             attr);
150                         
151                         return;
152                     }
153                 }
154             attrIndex = -1;
155         }
156
157         super.startElement(uri, name, raw, attr);
158     }
159
160     public void simpleLink(
161         final String JavaDoc href,
162         final String JavaDoc role,
163         final String JavaDoc arcrole,
164         final String JavaDoc title,
165         final String JavaDoc show,
166         final String JavaDoc actuate,
167         final String JavaDoc uri,
168         final String JavaDoc name,
169         final String JavaDoc raw,
170         final Attributes JavaDoc attr)
171         throws SAXException JavaDoc {
172         if (attrIndex != -1) {
173             final AttributesImpl JavaDoc newattr = new AttributesImpl JavaDoc(attr);
174             newattr.setValue(attrIndex, href);
175             startElement(uri, name, raw, newattr);
176         } else {
177             super.simpleLink(
178                 href,
179                 role,
180                 arcrole,
181                 title,
182                 show,
183                 actuate,
184                 uri,
185                 name,
186                 raw,
187                 attr);
188         }
189     }
190 }
191
Popular Tags