1 23 24 package org.enhydra.xml.xmlc.compiler; 25 26 import org.enhydra.xml.xmlc.XMLCException; 27 import org.enhydra.xml.xmlc.dom.XMLCDocument; 28 import org.enhydra.xml.xmlc.metadata.DOMEdits; 29 import org.enhydra.xml.xmlc.metadata.DeleteElement; 30 import org.enhydra.xml.xmlc.metadata.ElementEdit; 31 import org.enhydra.xml.xmlc.metadata.MetaData; 32 import org.enhydra.xml.xmlc.metadata.URLMapping; 33 import org.enhydra.xml.xmlc.metadata.URLRegExpMapping; 34 import org.w3c.dom.Attr ; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.NamedNodeMap ; 37 import org.w3c.dom.Node ; 38 39 42 public class EditDOM { 43 46 private DeleteElement[] deleteElements; 47 private URLMapping[] urlMappings; 48 private URLRegExpMapping[] urlRegExpMappings; 49 50 53 private XMLCDocument xmlcDocument; 54 55 58 private boolean isHtml; 59 60 63 public EditDOM(MetaData metaData) { 64 DOMEdits domEdits = metaData.getDOMEdits(); 65 if (domEdits != null) { 66 deleteElements = domEdits.getDeleteElements(); 67 urlMappings = domEdits.getURLMappings(); 68 urlRegExpMappings = domEdits.getURLRegExpMappings(); 69 } 70 if (deleteElements == null) { 72 deleteElements = new DeleteElement[0]; 73 } 74 if (urlMappings == null) { 75 urlMappings = new URLMapping[0]; 76 } 77 if (urlRegExpMappings == null) { 78 urlRegExpMappings = new URLRegExpMapping[0]; 79 } 80 } 81 82 85 private boolean elementMatchesConstraints(Element element, 86 ElementEdit elementEdit) { 87 if (!elementEdit.matchesElementIdConstraints(getElementId(element))) { 88 return false; 89 } 90 String [] elementClasses = xmlcDocument.getElementClassNames(element); 91 if (elementClasses == null) { 92 if (!elementEdit.matchesElementClassConstraints(null)) { 94 return false; 95 } 96 } else { 97 boolean classMatched = false; 99 for (int idx = 0; (idx < elementClasses.length) && (!classMatched); idx++) { 100 if (elementEdit.matchesElementClassConstraints(elementClasses[idx])) { 101 classMatched = true; 102 } 103 } 104 if (!classMatched) { 105 return false; 106 } 107 } 108 109 if (!elementEdit.matchesElementTagConstraints(element.getTagName(), 110 isHtml)) { 111 return false; 112 } 113 114 return true; 116 } 117 118 122 private boolean processDeleteElement(Element element, 123 DeleteElement deleteElement) { 124 if (elementMatchesConstraints(element, deleteElement)) { 125 element.getParentNode().removeChild(element); 126 return true; 127 } 128 return false; 129 } 130 131 135 private boolean processDeleteElements(Element element) { 136 for (int idx = 0; idx < deleteElements.length; idx++) { 137 if (processDeleteElement(element, deleteElements[idx])) { 138 return true; 139 } 140 } 141 return false; 142 } 143 144 149 private boolean processURLMapping(Element element, 150 String attrName, 151 String oldURL, 152 URLMapping urlMapping) { 153 if (elementMatchesConstraints(element, urlMapping) 154 && urlMapping.matchesEditAttrConstraints(attrName, isHtml)) { 155 String url = urlMapping.getUrl(); 157 if ((url == null) || url.equals(oldURL)) { 158 element.setAttribute(attrName, urlMapping.getNewUrl()); 159 return true; 160 } 161 } 162 return false; 163 } 164 165 170 private boolean processURLMappings(Element element, 171 String attrName, 172 String oldURL) { 173 for (int idx = 0; idx < urlMappings.length; idx++) { 174 if (processURLMapping(element, attrName, oldURL, urlMappings[idx])) { 175 return true; 176 } 177 } 178 return false; 179 } 180 181 186 private boolean processURLRegExpMapping(Element element, 187 String attrName, 188 String oldURL, 189 URLRegExpMapping urlRegExpMapping) 190 throws XMLCException { 191 if (elementMatchesConstraints(element, urlRegExpMapping) 192 && urlRegExpMapping.matchesEditAttrConstraints(attrName, isHtml)) { 193 String newURL = urlRegExpMapping.mapURL(oldURL); 194 if (newURL != null) { 195 element.setAttribute(attrName, newURL); 196 return true; 197 } 198 } 199 return false; 200 } 201 202 207 private boolean processURLRegExpMappings(Element element, 208 String attrName, 209 String oldURL) 210 throws XMLCException { 211 for (int idx = 0; idx < urlRegExpMappings.length; idx++) { 212 if (processURLRegExpMapping(element, attrName, oldURL, 213 urlRegExpMappings[idx])) { 214 return true; 215 } 216 } 217 return false; 218 } 219 220 224 private void editElementURL(Element element, 225 String attrName) throws XMLCException { 226 Attr attr = element.getAttributeNode(attrName); 227 if ((attr != null) && attr.getSpecified()) { 228 String oldURL = attr.getValue(); 229 if (!processURLMappings(element, attrName, oldURL)) { 230 processURLRegExpMappings(element, attrName, oldURL); 231 } 232 } 233 } 234 235 238 private void editElementURLs(Element element) throws XMLCException { 239 NamedNodeMap attrs = element.getAttributes(); 240 if (attrs != null) { 241 for (int idx = 0; idx < attrs.getLength(); idx++) { 242 String name = attrs.item(idx).getNodeName(); 243 if (xmlcDocument.isURLAttribute(element, name)) { 244 editElementURL(element, name); 245 } 246 } 247 } 248 } 249 250 253 private void editNodes(Node node) 254 throws XMLCException { 255 if (node instanceof Element) { 256 node = editElement((Element)node); 257 if (node == null) return; 259 } 260 261 Node child = node.getFirstChild(); 263 while (child != null) { 264 Node nextChild = child.getNextSibling(); editNodes(child); 266 child = nextChild; 267 } 268 } 269 270 281 protected Node editElement(Element element) throws XMLCException { 282 if (processDeleteElements(element)) { 285 return null; 286 } 287 editElementURLs(element); 288 return element; 289 } 290 291 294 public void edit(XMLCDocument xmlcDoc) 295 throws XMLCException { 296 this.xmlcDocument = xmlcDoc; 297 isHtml = xmlcDocument.isHtmlDocument(); 298 editNodes(xmlcDoc.getDocument()); 299 } 300 301 302 protected String getElementId(Element element) { 303 return xmlcDocument.getElementId(element); 304 } 305 } 306 | Popular Tags |