1 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 ; 21 import org.xml.sax.SAXException ; 22 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.io.PrintStream ; 26 27 31 32 public class LinkSerializer 33 extends ExtendedXLinkPipe 34 implements Serializer { 35 36 private PrintStream out; 37 38 42 public void setOutputStream(OutputStream out) throws IOException { 43 this.out = new PrintStream (out); 44 } 45 46 49 public String getMimeType() { 50 return Constants.LINK_CONTENT_TYPE; 51 } 52 53 public void simpleLink(String href, String role, String arcrole, String title, String show, String actuate, String uri, String name, String raw, Attributes attr) 54 throws SAXException { 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 href, String role, String title, String label, String uri, String name, String raw, Attributes attr) 62 throws SAXException { 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 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 href) { 80 int ankerPos = href.indexOf('#'); 81 if (ankerPos == -1) { 82 out.println(href); 84 } else { 85 out.println(href.substring(0, ankerPos)); 86 } 87 } 88 89 92 public boolean shouldSetContentLength() { 93 return false; 94 } 95 96 99 public void recycle() { 100 super.recycle(); 101 this.out = null; 102 } 103 } 104 | Popular Tags |