001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.serialization;
018:
019: import org.apache.cocoon.Constants;
020: import org.apache.cocoon.xml.xlink.ExtendedXLinkPipe;
021: import org.xml.sax.Attributes;
022: import org.xml.sax.SAXException;
023:
024: import java.io.IOException;
025: import java.io.OutputStream;
026: import java.io.PrintStream;
027:
028: /**
029: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
030: * @version CVS $Id: LinkSerializer.java 433543 2006-08-22 06:22:54Z crossley $
031: */
032:
033: public class LinkSerializer extends ExtendedXLinkPipe implements
034: Serializer {
035:
036: private PrintStream out;
037:
038: /**
039: * Set the {@link OutputStream} where the requested resource should
040: * be serialized.
041: */
042: public void setOutputStream(OutputStream out) throws IOException {
043: this .out = new PrintStream(out);
044: }
045:
046: /**
047: * Get the mime-type of the output of this <code>Component</code>.
048: */
049: public String getMimeType() {
050: return Constants.LINK_CONTENT_TYPE;
051: }
052:
053: public void simpleLink(String href, String role, String arcrole,
054: String title, String show, String actuate, String uri,
055: String name, String raw, Attributes attr)
056: throws SAXException {
057: if (traversable(href)) {
058: print(href);
059: }
060: super .simpleLink(href, role, arcrole, title, show, actuate,
061: uri, name, raw, attr);
062: }
063:
064: public void startLocator(String href, String role, String title,
065: String label, String uri, String name, String raw,
066: Attributes attr) throws SAXException {
067: if (traversable(href)) {
068: print(href);
069: }
070: super .startLocator(href, role, title, label, uri, name, raw,
071: attr);
072: }
073:
074: private boolean traversable(String href) {
075: if (href.length() == 0)
076: return false;
077: if (href.charAt(0) == '#')
078: return false;
079: if (href.indexOf("://") != -1)
080: return false;
081: if (href.startsWith("mailto:"))
082: return false;
083: if (href.startsWith("news:"))
084: return false;
085: if (href.startsWith("javascript:"))
086: return false;
087: return true;
088: }
089:
090: private void print(String href) {
091: int ankerPos = href.indexOf('#');
092: if (ankerPos == -1) {
093: // TODO: Xalan encodes international characters into URL encoding
094: out.println(href);
095: } else {
096: out.println(href.substring(0, ankerPos));
097: }
098: }
099:
100: /**
101: * Test if the component wants to set the content length
102: */
103: public boolean shouldSetContentLength() {
104: return false;
105: }
106:
107: /**
108: * Recyclable
109: */
110: public void recycle() {
111: super.recycle();
112: this.out = null;
113: }
114: }
|