KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.cocoon.xml.AbstractXMLPipe;
19 import org.xml.sax.Attributes JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
21 import org.xml.sax.helpers.AttributesImpl JavaDoc;
22
23 /**
24  * This class implements a SAX consumer wrapper that transforms the
25  * general SAX semantic into XLink semantics for easier consumption.
26  *
27  * Classes should extend this class and overwrite the abstract method
28  * to consume the XLink events that come in as SAX events.
29  *
30  * NOTE: this is based on XLink W3C Candidate Recommendation 3 July 2000
31  *
32  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
33  * @version CVS $Id: XLinkPipe.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35
36 public abstract class XLinkPipe extends AbstractXMLPipe implements XLinkHandler
37 {
38
39     public static final String JavaDoc XLINK_NAMESPACE_URI = "http://www.w3.org/1999/xlink";
40     public static final String JavaDoc XLINK_TYPE = "type";
41     public static final String JavaDoc XLINK_HREF = "href";
42     public static final String JavaDoc XLINK_ROLE = "role";
43     public static final String JavaDoc XLINK_ARCROLE = "arcrole";
44     public static final String JavaDoc XLINK_TITLE = "title";
45     public static final String JavaDoc XLINK_SHOW = "show";
46     public static final String JavaDoc XLINK_ACTUATE = "actuate";
47     public static final String JavaDoc XLINK_LABEL = "label";
48     public static final String JavaDoc XLINK_FROM = "from";
49     public static final String JavaDoc XLINK_TO = "to";
50     public static final String JavaDoc XLINK_TYPE_SIMPLE = "simple";
51     public static final String JavaDoc XLINK_TYPE_EXTENDED = "extended";
52     public static final String JavaDoc XLINK_TYPE_LOCATOR = "locator";
53     public static final String JavaDoc XLINK_TYPE_ARC = "arc";
54     public static final String JavaDoc XLINK_TYPE_RESOURCE = "resource";
55     public static final String JavaDoc XLINK_TYPE_TITLE = "title";
56
57     private String JavaDoc extendedLinkElementName = null;
58     private String JavaDoc extendedLinkElementURI = null;
59     private String JavaDoc linkLocatorElementName = null;
60     private String JavaDoc linkLocatorElementURI = null;
61     private String JavaDoc linkArcElementName = null;
62     private String JavaDoc linkArcElementURI = null;
63
64     public void startElement( String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attr ) throws SAXException JavaDoc
65     {
66         String JavaDoc type = attr.getValue( XLINK_NAMESPACE_URI, XLINK_TYPE );
67         if ( type != null )
68         {
69             if ( type.equals( XLINK_TYPE_SIMPLE ) )
70             {
71                 if ( this.extendedLinkElementName != null )
72                 {
73                     throw new SAXException JavaDoc( "An XLink simple link cannot be included into an 'extended' element" );
74                 }
75                 else if ( this.linkLocatorElementName != null )
76                 {
77                     throw new SAXException JavaDoc( "An XLink simple link cannot be included into a 'locator' element" );
78                 }
79                 else if ( this.linkArcElementName != null )
80                 {
81                     throw new SAXException JavaDoc( "An XLink simple link cannot be included into an 'arc' element" );
82                 }
83                 String JavaDoc href = attr.getValue( XLINK_NAMESPACE_URI, XLINK_HREF );
84                 String JavaDoc role = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ROLE );
85                 String JavaDoc arcrole = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ARCROLE );
86                 String JavaDoc title = attr.getValue( XLINK_NAMESPACE_URI, XLINK_TITLE );
87                 String JavaDoc show = attr.getValue( XLINK_NAMESPACE_URI, XLINK_SHOW );
88                 String JavaDoc actuate = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ACTUATE );
89                 simpleLink( href, role, arcrole, title, show, actuate, uri, name, raw, attr );
90             }
91             else if ( type.equals( XLINK_TYPE_EXTENDED ) )
92             {
93                 if ( this.extendedLinkElementName != null )
94                 {
95                     throw new SAXException JavaDoc( "An XLink extended link cannot include another 'extended' element" );
96                 }
97                 else if ( this.linkLocatorElementName != null )
98                 {
99                     throw new SAXException JavaDoc( "An XLink extended link cannot be included into a 'locator' element" );
100                 }
101                 else if ( this.linkArcElementName != null )
102                 {
103                     throw new SAXException JavaDoc( "An XLink extended link cannot be included into an 'arc' element" );
104                 }
105                 String JavaDoc role = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ROLE );
106                 String JavaDoc title = attr.getValue( XLINK_NAMESPACE_URI, XLINK_TITLE );
107                 this.extendedLinkElementName = name;
108                 this.extendedLinkElementURI = uri;
109                 startExtendedLink( role, title, uri, name, raw, attr );
110             }
111             else if ( type.equals( XLINK_TYPE_LOCATOR ) )
112             {
113                 if ( this.extendedLinkElementName == null )
114                 {
115                     throw new SAXException JavaDoc( "An XLink locator must be included into an 'extended' element" );
116                 }
117                 else if ( this.linkLocatorElementName != null )
118                 {
119                     throw new SAXException JavaDoc( "An XLink locator cannot be included into another 'locator' element" );
120                 }
121                 else if ( this.linkArcElementName != null )
122                 {
123                     throw new SAXException JavaDoc( "An XLink locator cannot be included into an 'arc' element" );
124                 }
125                 String JavaDoc href = attr.getValue( XLINK_NAMESPACE_URI, XLINK_HREF );
126                 String JavaDoc role = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ROLE );
127                 String JavaDoc title = attr.getValue( XLINK_NAMESPACE_URI, XLINK_TITLE );
128                 String JavaDoc label = attr.getValue( XLINK_NAMESPACE_URI, XLINK_LABEL );
129                 this.linkLocatorElementName = name;
130                 this.linkLocatorElementURI = uri;
131                 startLocator( href, role, title, label, uri, name, raw, attr );
132             }
133             else if ( type.equals( XLINK_TYPE_ARC ) )
134             {
135                 if ( this.extendedLinkElementName == null )
136                 {
137                     throw new SAXException JavaDoc( "An XLink arc must be included into an 'extended' element" );
138                 }
139                 else if ( this.linkLocatorElementName != null )
140                 {
141                     throw new SAXException JavaDoc( "An XLink arc cannot be included into a 'locator' element" );
142                 }
143                 else if ( this.linkArcElementName != null )
144                 {
145                     throw new SAXException JavaDoc( "An XLink arc cannot be included into another 'arc' element" );
146                 }
147                 String JavaDoc arcrole = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ARCROLE );
148                 String JavaDoc title = attr.getValue( XLINK_NAMESPACE_URI, XLINK_TITLE );
149                 String JavaDoc show = attr.getValue( XLINK_NAMESPACE_URI, XLINK_SHOW );
150                 String JavaDoc actuate = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ACTUATE );
151                 String JavaDoc from = attr.getValue( XLINK_NAMESPACE_URI, XLINK_FROM );
152                 String JavaDoc to = attr.getValue( XLINK_NAMESPACE_URI, XLINK_TO );
153                 this.linkArcElementName = name;
154                 this.linkArcElementURI = uri;
155                 startArc( arcrole, title, show, actuate, from, to, uri, name, raw, attr );
156             }
157             else if ( type.equals( XLINK_TYPE_RESOURCE ) )
158             {
159                 if ( this.extendedLinkElementName == null )
160                 {
161                     throw new SAXException JavaDoc( "An XLink resource must be included into an 'extended' element" );
162                 }
163                 String JavaDoc role = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ROLE );
164                 String JavaDoc title = attr.getValue( XLINK_NAMESPACE_URI, XLINK_TITLE );
165                 String JavaDoc label = attr.getValue( XLINK_NAMESPACE_URI, XLINK_LABEL );
166                 linkResource( role, title, label, uri, name, raw, attr );
167             }
168             else if ( type.equals( XLINK_TYPE_TITLE ) )
169             {
170                 if ( ( this.extendedLinkElementName == null )
171                         && ( this.linkLocatorElementName == null )
172                         && ( this.linkArcElementName == null ) )
173                 {
174                     throw new SAXException JavaDoc( "An XLink title must be included into an 'extended', 'locator' or 'arc' element" );
175                 }
176                 linkTitle( uri, name, raw, attr );
177             }
178             else
179             {
180                 super.startElement( uri, name, raw, attr );
181             }
182         }
183         else
184         {
185             super.startElement( uri, name, raw, attr );
186         }
187     }
188
189     public void endElement( String JavaDoc uri, String JavaDoc name, String JavaDoc raw ) throws SAXException JavaDoc
190     {
191         if ( ( name.equals( this.extendedLinkElementName ) ) && ( uri.equals( this.extendedLinkElementURI ) ) )
192         {
193             this.extendedLinkElementName = null;
194             this.extendedLinkElementURI = null;
195             this.endExtendedLink( uri, name, raw );
196         }
197         else if ( ( name.equals( this.linkLocatorElementName ) ) && ( uri.equals( this.linkLocatorElementURI ) ) )
198         {
199             this.linkLocatorElementName = null;
200             this.linkLocatorElementURI = null;
201             this.endLocator( uri, name, raw );
202         }
203         else if ( ( name.equals( this.linkArcElementName ) ) && ( uri.equals( this.linkArcElementURI ) ) )
204         {
205             this.linkArcElementName = null;
206             this.linkArcElementURI = null;
207             this.endArc( uri, name, raw );
208         }
209         else
210         {
211             super.endElement( uri, name, raw );
212         }
213     }
214
215     // Default XLinkHandler implementation (defaults to copy over)
216

217     public void simpleLink( String JavaDoc href, String JavaDoc role, String JavaDoc arcrole, String JavaDoc title, String JavaDoc show, String JavaDoc actuate, String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attr ) throws SAXException JavaDoc
218     {
219         AttributesImpl JavaDoc newattr = new AttributesImpl JavaDoc( attr );
220         int hrefIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_HREF );
221         int roleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ROLE );
222         int arcroleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ARCROLE );
223         int titleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_TITLE );
224         int showIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_SHOW );
225         int actuateIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ACTUATE );
226         if ( hrefIndex > -1 )
227             newattr.setValue( hrefIndex, href );
228         if ( roleIndex > -1 )
229             newattr.setValue( roleIndex, role );
230         if ( arcroleIndex > -1 )
231             newattr.setValue( arcroleIndex, arcrole );
232         if ( titleIndex > -1 )
233             newattr.setValue( titleIndex, title );
234         if ( showIndex > -1 )
235             newattr.setValue( showIndex, show );
236         if ( actuateIndex > -1 )
237             newattr.setValue( actuateIndex, actuate );
238         super.startElement( uri, name, raw, newattr );
239     }
240
241     public void startExtendedLink( String JavaDoc role, String JavaDoc title, String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attr ) throws SAXException JavaDoc
242     {
243         AttributesImpl JavaDoc newattr = new AttributesImpl JavaDoc( attr );
244         int roleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ROLE );
245         int titleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_TITLE );
246         if ( roleIndex > -1 )
247             newattr.setValue( roleIndex, role );
248         if ( titleIndex > -1 )
249             newattr.setValue( titleIndex, title );
250         super.startElement( uri, name, raw, newattr );
251     }
252
253     public void startLocator( String JavaDoc href, String JavaDoc role, String JavaDoc title, String JavaDoc label, String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attr ) throws SAXException JavaDoc
254     {
255         AttributesImpl JavaDoc newattr = new AttributesImpl JavaDoc( attr );
256         int hrefIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_HREF );
257         int roleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ROLE );
258         int titleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_TITLE );
259         int labelIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_LABEL );
260         if ( hrefIndex > -1 )
261             newattr.setValue( hrefIndex, href );
262         if ( roleIndex > -1 )
263             newattr.setValue( roleIndex, role );
264         if ( titleIndex > -1 )
265             newattr.setValue( titleIndex, title );
266         if ( labelIndex > -1 )
267             newattr.setValue( labelIndex, label );
268         super.startElement( uri, name, raw, newattr );
269     }
270
271     public void startArc( String JavaDoc arcrole, String JavaDoc title, String JavaDoc show, String JavaDoc actuate, String JavaDoc from, String JavaDoc to, String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attr ) throws SAXException JavaDoc
272     {
273         AttributesImpl JavaDoc newattr = new AttributesImpl JavaDoc( attr );
274         int arcroleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ARCROLE );
275         int titleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_TITLE );
276         int showIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_SHOW );
277         int actuateIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ACTUATE );
278         int fromIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_FROM );
279         int toIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_TO );
280         if ( arcroleIndex > -1 )
281             newattr.setValue( arcroleIndex, arcrole );
282         if ( titleIndex > -1 )
283             newattr.setValue( titleIndex, title );
284         if ( showIndex > -1 )
285             newattr.setValue( showIndex, show );
286         if ( actuateIndex > -1 )
287             newattr.setValue( actuateIndex, actuate );
288         if ( fromIndex > -1 )
289             newattr.setValue( actuateIndex, from );
290         if ( toIndex > -1 )
291             newattr.setValue( actuateIndex, to );
292         super.startElement( uri, name, raw, newattr );
293     }
294
295     public void linkResource( String JavaDoc role, String JavaDoc title, String JavaDoc label, String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attr ) throws SAXException JavaDoc
296     {
297         AttributesImpl JavaDoc newattr = new AttributesImpl JavaDoc( attr );
298         int roleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ROLE );
299         int titleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_TITLE );
300         int labelIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_LABEL );
301         if ( roleIndex > -1 )
302             newattr.setValue( roleIndex, role );
303         if ( titleIndex > -1 )
304             newattr.setValue( titleIndex, title );
305         if ( labelIndex > -1 )
306             newattr.setValue( labelIndex, label );
307         super.startElement( uri, name, raw, newattr );
308     }
309
310     public void linkTitle( String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attr ) throws SAXException JavaDoc
311     {
312         super.startElement( uri, name, raw, attr );
313     }
314
315     public void endExtendedLink( String JavaDoc uri, String JavaDoc name, String JavaDoc raw ) throws SAXException JavaDoc
316     {
317         super.endElement( uri, name, raw );
318     }
319
320     public void endLocator( String JavaDoc uri, String JavaDoc name, String JavaDoc raw ) throws SAXException JavaDoc
321     {
322         super.endElement( uri, name, raw );
323     }
324
325     public void endArc( String JavaDoc uri, String JavaDoc name, String JavaDoc raw ) throws SAXException JavaDoc
326     {
327         super.endElement( uri, name, raw );
328     }
329 }
330
331
Popular Tags