KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > serialization > LinkSerializer


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.serialization;
17
18 import org.apache.cocoon.Constants;
19 import org.apache.cocoon.xml.xlink.ExtendedXLinkPipe;
20 import org.xml.sax.Attributes JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
22
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.PrintStream JavaDoc;
26
27 /**
28  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
29  * @version CVS $Id: LinkSerializer.java 30932 2004-07-29 17:35:38Z vgritsenko $
30  */

31
32 public class LinkSerializer
33     extends ExtendedXLinkPipe
34     implements Serializer {
35
36     private PrintStream JavaDoc out;
37
38     /**
39      * Set the {@link OutputStream} where the requested resource should
40      * be serialized.
41      */

42     public void setOutputStream(OutputStream JavaDoc out) throws IOException JavaDoc {
43         this.out = new PrintStream JavaDoc(out);
44     }
45
46     /**
47      * Get the mime-type of the output of this <code>Component</code>.
48      */

49     public String JavaDoc getMimeType() {
50         return Constants.LINK_CONTENT_TYPE;
51     }
52
53     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)
54     throws SAXException JavaDoc {
55         if (traversable(href)) {
56             print(href);
57         }
58         super.simpleLink(href, role, arcrole, title, show, actuate, uri, name, raw, attr);
59     }
60
61     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)
62     throws SAXException JavaDoc {
63         if (traversable(href)) {
64             print(href);
65         }
66         super.startLocator(href, role, title, label, uri, name, raw, attr);
67     }
68
69     private boolean traversable(String JavaDoc href) {
70         if (href.length() == 0) return false;
71         if (href.charAt(0) == '#') return false;
72         if (href.indexOf("://") != -1) return false;
73         if (href.startsWith("mailto:")) return false;
74         if (href.startsWith("news:")) return false;
75         if (href.startsWith("javascript:")) return false;
76         return true;
77     }
78
79     private void print(String JavaDoc href) {
80         int ankerPos = href.indexOf('#');
81         if (ankerPos == -1) {
82             // TODO: Xalan encodes international characters into URL encoding
83
out.println(href);
84         } else {
85             out.println(href.substring(0, ankerPos));
86         }
87     }
88
89     /**
90      * Test if the component wants to set the content length
91      */

92     public boolean shouldSetContentLength() {
93         return false;
94     }
95
96     /**
97      * Recyclable
98      */

99     public void recycle() {
100         super.recycle();
101         this.out = null;
102     }
103 }
104
Popular Tags