1 package org.jahia.services.htmlparser; 2 3 import org.w3c.dom.Document ; 4 import org.w3c.dom.Node ; 5 import org.w3c.dom.NodeList ; 6 import org.w3c.dom.Element ; 7 import java.util.Map ; 8 import java.util.HashMap ; 9 import java.util.Set ; 10 import java.util.HashSet ; 11 import java.util.Iterator ; 12 13 import org.jahia.utils.JahiaTools; 14 15 23 24 public class ActiveXServerURLCleanupDOMVisitor implements HtmlDOMVisitor { 25 26 private static org.apache.log4j.Logger logger = 27 org.apache.log4j.Logger.getLogger( ActiveXServerURLCleanupDOMVisitor.class); 28 29 private String serverURL = ""; 30 31 private String [][] tagAndAttributesWithLinks = { 32 35 { "a", "href" }, 36 { "img", "src" }, 37 { "img", "longdesc" }, 38 { "img", "usemap" }, 39 { "area", "href" }, 40 { "link", "href" }, 41 { "object", "classid" }, 42 { "object", "codebase" }, 43 { "object", "data" }, 44 { "object", "usemap" }, 45 { "q", "cite" }, 46 { "blockquote", "cite" }, 47 { "ins", "cite" }, 48 { "del", "cite" }, 49 { "form", "action" }, 50 { "input", "src" }, 51 { "input", "usemap" }, 52 { "head", "profile" }, 53 { "base", "href" }, 54 { "script", "src" }, 55 { "script", "for" } 56 }; 57 58 private Map linkAttributesByTagName = new HashMap (); 59 60 64 public ActiveXServerURLCleanupDOMVisitor(String serverURL) { 65 this.serverURL = serverURL; 66 if ( this.serverURL == null ){ 67 this.serverURL = ""; 68 } else { 69 this.serverURL = serverURL.trim(); 70 } 71 if ( !"".equals(this.serverURL) ){ 72 this.serverURL = JahiaTools.replacePatternIgnoreCase(this.serverURL,"&",TidyHtmlParser.AMPERSAND); 73 for (int i = 0; i < tagAndAttributesWithLinks.length; i++) { 74 String tagName = tagAndAttributesWithLinks[i][0]; 75 String attributeName = tagAndAttributesWithLinks[i][1]; 76 Set tagAttributes = null; 77 if (!linkAttributesByTagName.containsKey(tagName)) { 78 tagAttributes = new HashSet (); 79 linkAttributesByTagName.put(tagName, tagAttributes); 80 } 81 else { 82 tagAttributes = (Set ) linkAttributesByTagName.get(tagName); 83 } 84 tagAttributes.add(attributeName); 85 } 86 } 87 } 88 89 public void init(int siteId) { 90 } 91 92 public Document parseDOM(Document doc) { 93 if ( doc != null && !"".equals(serverURL) ){ 94 removeServerNameFromURLs(doc.getDocumentElement()); 95 } 96 return doc; 97 } 98 99 private void removeServerNameFromURLs(Node node){ 100 101 String servletPath = ""; 102 int pos = serverURL.lastIndexOf("/"); 103 if ( pos != -1 ){ 104 servletPath = serverURL.substring(0,pos+1); 105 } 106 107 String serverName = ""; 108 int pos2 = serverURL.indexOf("//"); 109 if ( pos2 != -1 ){ 110 pos = serverURL.substring(pos2+2,serverURL.length()).indexOf("/"); 111 if ( pos != -1 ){ 112 serverName = serverURL.substring(0, pos + pos2+2); 113 } 114 } 115 116 if ( node == null ){ 117 return; 118 } 119 if (node.getNodeType() == Node.ELEMENT_NODE) { 120 Element curElement = (Element ) node; 121 Set linkAttributes = (Set ) linkAttributesByTagName.get(curElement.getTagName().toLowerCase()); 122 if (linkAttributes != null) { 123 Iterator attributeIter = linkAttributes.iterator(); 124 while (attributeIter.hasNext()) { 125 String curLinkAttribute = (String ) attributeIter.next(); 126 if ( curElement.hasAttribute(curLinkAttribute) ){ 127 String curLink = curElement.getAttribute( 128 curLinkAttribute); 129 if (curLink != null) { 130 logger.debug("Found link [" + curLink + 131 "] on tag [" + curElement.getTagName() + 132 "] with attribute [" + 133 curLinkAttribute + "]"); 134 curLink = JahiaTools.replacePattern(curLink, 135 this.serverURL, ""); 136 if (!"".equals(servletPath)) { 137 curLink = JahiaTools.replacePattern( 138 curLink, servletPath, ""); 139 } 140 if (!"".equals(serverName)) { 141 curLink = JahiaTools.replacePattern( 142 curLink, serverName, ""); 143 } 144 curElement.setAttribute(curLinkAttribute, curLink); 145 } 146 } 147 } 148 } 149 } 150 151 NodeList childNodes = node.getChildNodes(); 152 for (int i=0; i < childNodes.getLength(); i++) { 153 removeServerNameFromURLs(childNodes.item(i)); 154 } 155 } 156 } 157 | Popular Tags |