1 16 package org.outerj.daisy.htmlcleaner; 17 18 import java.util.*; 19 20 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 imgAlternateSrcAttr; 35 String linkAlternateHrefAttr; 36 private boolean initialised = false; 37 38 HtmlCleanerTemplate() { 39 } 41 42 void addOutputElement(String tagName, int beforeOpen, int afterOpen, int beforeClose, int afterClose, boolean inline) { 43 if (initialised) 44 throw new IllegalStateException (); 45 if (tagName == null) 46 throw new NullPointerException (); 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 (); 54 this.maxLineWidth = lineWidth; 55 } 56 57 void addAllowedSpanClass(String clazz) { 58 if (initialised) 59 throw new IllegalStateException (); 60 if (clazz == null) 61 throw new NullPointerException (); 62 allowedSpanClasses.add(clazz); 63 } 64 65 void addAllowedDivClass(String clazz) { 66 if (initialised) 67 throw new IllegalStateException (); 68 if (clazz == null) 69 throw new NullPointerException (); 70 allowedDivClasses.add(clazz); 71 } 72 73 void addAllowedParaClass(String clazz) { 74 if (initialised) 75 throw new IllegalStateException (); 76 if (clazz == null) 77 throw new NullPointerException (); 78 allowedParaClasses.add(clazz); 79 } 80 81 void addAllowedPreClass(String clazz) { 82 if (initialised) 83 throw new IllegalStateException (); 84 if (clazz == null) 85 throw new NullPointerException (); 86 allowedPreClasses.add(clazz); 87 } 88 89 void addAllowedElement(String tagName, String [] attributes) { 90 if (initialised) 91 throw new IllegalStateException (); 92 if (tagName == null) 93 throw new NullPointerException (); 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 { 104 if (initialised) 105 throw new IllegalStateException (); 106 Map full = new XhtmlDescriptorBuilder().build(); 110 relax(full); 111 narrow(full, descriptors); 112 descriptors = full; 113 initialised = true; 114 } 115 116 120 private void narrow(Map full, Map subset) { 121 String [] fullKeys = (String [])full.keySet().toArray(new String [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 [] childNames = (String [])elementDescriptor.getChildren().toArray(new String [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 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 name) { 162 this.imgAlternateSrcAttr = name; 163 } 164 165 public void setLinkAlternateHrefAttr(String linkAlternateHrefAttr) { 166 this.linkAlternateHrefAttr = linkAlternateHrefAttr; 167 } 168 } 169 | Popular Tags |