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.lenya.cms.cocoon.serialization;
018:
019: import org.apache.cocoon.serialization.Serializer;
020: import org.apache.cocoon.Constants;
021: import org.apache.cocoon.xml.xlink.ExtendedXLinkPipe;
022: import org.xml.sax.Attributes;
023: import org.xml.sax.SAXException;
024:
025: import java.io.IOException;
026: import java.io.OutputStream;
027: import java.io.PrintStream;
028:
029: /**
030: * Link serializer.
031: * TODO: add meaningful javadocs
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 be serialized.
040: */
041: public void setOutputStream(OutputStream out) throws IOException {
042: this .out = new PrintStream(out);
043: }
044:
045: /**
046: * Get the mime-type of the output of this <code>Component</code>.
047: */
048: public String getMimeType() {
049: return Constants.LINK_CONTENT_TYPE;
050: }
051:
052: public void simpleLink(String href, String role, String arcrole,
053: String title, String show, String actuate, String uri,
054: String name, String raw, Attributes attr)
055: throws SAXException {
056: print(href);
057: super .simpleLink(href, role, arcrole, title, show, actuate,
058: uri, name, raw, attr);
059: }
060:
061: public void startLocator(String href, String role, String title,
062: String label, String uri, String name, String raw,
063: Attributes attr) throws SAXException {
064: if (traversable(href)) {
065: print(href);
066: }
067: super .startLocator(href, role, title, label, uri, name, raw,
068: attr);
069: }
070:
071: private boolean traversable(String href) {
072: if (href.length() == 0)
073: return false;
074: if (href.charAt(0) == '#')
075: return false;
076: if (href.indexOf("://") != -1)
077: return false;
078: if (href.startsWith("mailto:"))
079: return false;
080: if (href.startsWith("news:"))
081: return false;
082: if (href.startsWith("javascript:"))
083: return false;
084: return true;
085: }
086:
087: private void print(String href) {
088: int ankerPos = href.indexOf('#');
089: if (ankerPos == -1) {
090: // TODO: Xalan encodes international characters into URL encoding
091: out.println(href);
092: } else {
093: out.println(href.substring(0, ankerPos));
094: }
095: }
096:
097: /**
098: * Test if the component wants to set the content length
099: */
100: public boolean shouldSetContentLength() {
101: return false;
102: }
103:
104: /**
105: * Recyclable
106: */
107: public void recycle() {
108: super.recycle();
109: this.out = null;
110: }
111: }
|