KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > htmlcleaner > HtmlCleanerTemplate


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.htmlcleaner;
17
18 import java.util.*;
19
20 /**
21  * This is a thread-safe, reusable object containing the configuration for
22  * the HtmlCleaner. Instances of this object can be obtained from the
23  * {@link HtmlCleanerFactory}. A concrete HtmlCleaner can be obtained
24  * by using the method {@link #newHtmlCleaner()}.
25  */

26 public class HtmlCleanerTemplate {
27     int maxLineWidth = 80;
28     Map outputElementDescriptors = new HashMap();
29     Set allowedSpanClasses = new HashSet();
30     Set allowedDivClasses = new HashSet();
31     Set allowedParaClasses = new HashSet();
32     Set allowedPreClasses = new HashSet();
33     Map descriptors = new HashMap();
34     String JavaDoc imgAlternateSrcAttr;
35     String JavaDoc linkAlternateHrefAttr;
36     private boolean initialised = false;
37
38     HtmlCleanerTemplate() {
39         // package-private constructor
40
}
41
42     void addOutputElement(String JavaDoc tagName, int beforeOpen, int afterOpen, int beforeClose, int afterClose, boolean inline) {
43         if (initialised)
44             throw new IllegalStateException JavaDoc();
45         if (tagName == null)
46             throw new NullPointerException JavaDoc();
47         OutputElementDescriptor descriptor = new OutputElementDescriptor(beforeOpen, afterOpen, beforeClose, afterClose, inline);
48         outputElementDescriptors.put(tagName, descriptor);
49     }
50
51     void setMaxLineWidth(int lineWidth) {
52         if (initialised)
53             throw new IllegalStateException JavaDoc();
54         this.maxLineWidth = lineWidth;
55     }
56
57     void addAllowedSpanClass(String JavaDoc clazz) {
58         if (initialised)
59             throw new IllegalStateException JavaDoc();
60         if (clazz == null)
61             throw new NullPointerException JavaDoc();
62         allowedSpanClasses.add(clazz);
63     }
64
65     void addAllowedDivClass(String JavaDoc clazz) {
66         if (initialised)
67             throw new IllegalStateException JavaDoc();
68         if (clazz == null)
69             throw new NullPointerException JavaDoc();
70         allowedDivClasses.add(clazz);
71     }
72
73     void addAllowedParaClass(String JavaDoc clazz) {
74         if (initialised)
75             throw new IllegalStateException JavaDoc();
76         if (clazz == null)
77             throw new NullPointerException JavaDoc();
78         allowedParaClasses.add(clazz);
79     }
80
81     void addAllowedPreClass(String JavaDoc clazz) {
82         if (initialised)
83             throw new IllegalStateException JavaDoc();
84         if (clazz == null)
85             throw new NullPointerException JavaDoc();
86         allowedPreClasses.add(clazz);
87     }
88
89     void addAllowedElement(String JavaDoc tagName, String JavaDoc[] attributes) {
90         if (initialised)
91             throw new IllegalStateException JavaDoc();
92         if (tagName == null)
93             throw new NullPointerException JavaDoc();
94
95         ElementDescriptor descriptor = new ElementDescriptor(tagName);
96         for (int i = 0; i < attributes.length; i++) {
97             descriptor.addAttribute(attributes[i]);
98         }
99
100         descriptors.put(tagName, descriptor);
101     }
102
103     void initialize() throws Exception JavaDoc {
104         if (initialised)
105             throw new IllegalStateException JavaDoc();
106         // build our descriptor model:
107
// - retrieve the one for XHTML (so that we have information about content models)
108
// - filter it to only contain the elements the user configured
109
Map full = new XhtmlDescriptorBuilder().build();
110         relax(full);
111         narrow(full, descriptors);
112         descriptors = full;
113         initialised = true;
114     }
115
116     /**
117      * Modifies the full map so that it only contains elements and attributes
118      * from the subset, but retains the child element information.
119      */

120     private void narrow(Map full, Map subset) {
121         String JavaDoc[] fullKeys = (String JavaDoc[])full.keySet().toArray(new String JavaDoc[full.size()]);
122         for (int i = 0; i < fullKeys.length; i++) {
123             if (!subset.containsKey(fullKeys[i]))
124                 full.remove(fullKeys[i]);
125         }
126
127         Iterator descriptorIt = full.values().iterator();
128         while (descriptorIt.hasNext()) {
129             ElementDescriptor elementDescriptor = (ElementDescriptor)descriptorIt.next();
130             String JavaDoc[] childNames = (String JavaDoc[])elementDescriptor.getChildren().toArray(new String JavaDoc[0]);
131             Set newChilds = new HashSet();
132             for (int i = 0; i < childNames.length; i++) {
133                 if (subset.containsKey(childNames[i]))
134                     newChilds.add(childNames[i]);
135             }
136             elementDescriptor.setChildren(newChilds);
137             elementDescriptor.setAttributes(((ElementDescriptor)subset.get(elementDescriptor.getName())).getAttributes());
138         }
139     }
140
141     private void relax(Map descriptors) {
142         // HTML doesn't allow ul's to be nested directly, but that's what all these HTML
143
// editors create, so relax that restriction a bit
144
ElementDescriptor ulDescriptor = (ElementDescriptor)descriptors.get("ul");
145         if (ulDescriptor != null) {
146             ulDescriptor.getChildren().add("ul");
147             ulDescriptor.getChildren().add("ol");
148         }
149
150         ElementDescriptor olDescriptor = (ElementDescriptor)descriptors.get("ol");
151         if (olDescriptor != null) {
152             olDescriptor.getChildren().add("ul");
153             olDescriptor.getChildren().add("ol");
154         }
155     }
156
157     public HtmlCleaner newHtmlCleaner() {
158         return new HtmlCleaner(this);
159     }
160
161     void setImgAlternateSrcAttr(String JavaDoc name) {
162         this.imgAlternateSrcAttr = name;
163     }
164
165     public void setLinkAlternateHrefAttr(String JavaDoc linkAlternateHrefAttr) {
166         this.linkAlternateHrefAttr = linkAlternateHrefAttr;
167     }
168 }
169
Popular Tags