KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > htmlparser > ActiveXServerURLCleanupDOMVisitor


1 package org.jahia.services.htmlparser;
2
3 import org.w3c.dom.Document JavaDoc;
4 import org.w3c.dom.Node JavaDoc;
5 import org.w3c.dom.NodeList JavaDoc;
6 import org.w3c.dom.Element JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Set JavaDoc;
10 import java.util.HashSet JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13 import org.jahia.utils.JahiaTools;
14
15 /**
16  * <p>Title: Used to transform absolute url to relative url by removing the precised server URL </p>
17  * <p>Description: </p>
18  * <p>Copyright: Copyright (c) 2002</p>
19  * <p>Company: Jahia Ltd</p>
20  * @author Khue Nguyen
21  * @version 1.0
22  */

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 JavaDoc serverURL = "";
30
31     private String JavaDoc[][] tagAndAttributesWithLinks = {
32         /* This list is based on the HTML 4.01 DTD, available here :
33            http://www.w3.org/TR/html401/sgml/dtd.html
34          */

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 JavaDoc linkAttributesByTagName = new HashMap JavaDoc();
59
60     /**
61      *
62      * @param serverURL String
63      */

64     public ActiveXServerURLCleanupDOMVisitor(String JavaDoc 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 JavaDoc tagName = tagAndAttributesWithLinks[i][0];
75                 String JavaDoc attributeName = tagAndAttributesWithLinks[i][1];
76                 Set JavaDoc tagAttributes = null;
77                 if (!linkAttributesByTagName.containsKey(tagName)) {
78                     tagAttributes = new HashSet JavaDoc();
79                     linkAttributesByTagName.put(tagName, tagAttributes);
80                 }
81                 else {
82                     tagAttributes = (Set JavaDoc) linkAttributesByTagName.get(tagName);
83                 }
84                 tagAttributes.add(attributeName);
85             }
86         }
87     }
88
89     public void init(int siteId) {
90     }
91
92     public Document JavaDoc parseDOM(Document JavaDoc doc) {
93         if ( doc != null && !"".equals(serverURL) ){
94             removeServerNameFromURLs(doc.getDocumentElement());
95         }
96         return doc;
97     }
98
99     private void removeServerNameFromURLs(Node JavaDoc node){
100
101         String JavaDoc servletPath = "";
102         int pos = serverURL.lastIndexOf("/");
103         if ( pos != -1 ){
104             servletPath = serverURL.substring(0,pos+1);
105         }
106
107         String JavaDoc 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 JavaDoc curElement = (Element JavaDoc) node;
121             Set JavaDoc linkAttributes = (Set JavaDoc) linkAttributesByTagName.get(curElement.getTagName().toLowerCase());
122             if (linkAttributes != null) {
123                 Iterator JavaDoc attributeIter = linkAttributes.iterator();
124                 while (attributeIter.hasNext()) {
125                     String JavaDoc curLinkAttribute = (String JavaDoc) attributeIter.next();
126                     if ( curElement.hasAttribute(curLinkAttribute) ){
127                         String JavaDoc 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 JavaDoc childNodes = node.getChildNodes();
152         for (int i=0; i < childNodes.getLength(); i++) {
153             removeServerNameFromURLs(childNodes.item(i));
154         }
155     }
156 }
157
Popular Tags