1 31 32 package org.opencms.importexport; 33 34 import org.opencms.file.types.CmsResourceTypeFolder; 35 import org.opencms.file.types.CmsResourceTypePlain; 36 import org.opencms.workplace.CmsWorkplace; 37 38 import java.util.List ; 39 40 import org.dom4j.Document; 41 import org.dom4j.DocumentHelper; 42 import org.dom4j.Element; 43 import org.dom4j.Node; 44 45 55 public class CmsCompatibleCheck { 56 57 58 public static final String VFS_PATH_BODIES = "/system/bodies/"; 59 60 61 public static final String VFS_PATH_DEFAULTMODULE = CmsWorkplace.VFS_PATH_MODULES + "default/"; 62 63 64 public static final String VFS_PATH_DEFAULT_TEMPLATES = VFS_PATH_DEFAULTMODULE + CmsWorkplace.VFS_DIR_TEMPLATES; 65 66 67 public static final String XML_CONTROL_DEFAULT_CLASS = "com.opencms.template.CmsXmlTemplate"; 68 69 72 public CmsCompatibleCheck() { 73 74 } 76 77 85 public boolean isTemplateCompatible(String name, byte[] content, String type) { 86 87 if (CmsResourceTypeFolder.RESOURCE_TYPE_NAME.equals(type)) { 89 return true; 90 } 91 if (name == null) { 92 return false; 93 } 94 if (name.startsWith(CmsCompatibleCheck.VFS_PATH_BODIES)) { 95 if (!CmsResourceTypePlain.getStaticTypeName().equals(type)) { 97 return false; 99 } 100 try { 102 Document xmlDoc = DocumentHelper.parseText(new String (content)); 103 for (Node n = (Node)xmlDoc.content().get(0); n != null; n = treeWalker(xmlDoc, n)) { 104 short ntype = n.getNodeType(); 105 if (((ntype > Node.CDATA_SECTION_NODE) && ntype < Node.DOCUMENT_TYPE_NODE) 106 || (ntype == Node.ATTRIBUTE_NODE)) { 107 return false; 108 } 109 if (n.getNodeType() == Node.ELEMENT_NODE) { 110 String tagName = n.getName(); 111 if (!("template".equalsIgnoreCase(tagName) || "xmltemplate".equalsIgnoreCase(tagName))) { 112 return false; 113 } 114 } 115 } 116 } catch (Exception e) { 117 return false; 118 } 119 120 } else if (name.startsWith(CmsCompatibleCheck.VFS_PATH_DEFAULT_TEMPLATES) 121 || (name.startsWith(CmsWorkplace.VFS_PATH_MODULES) && name.indexOf("/" + CmsWorkplace.VFS_DIR_TEMPLATES) > -1)) { 122 if (!CmsResourceTypePlain.getStaticTypeName().equals(type)) { 124 return false; 126 } 127 try { 129 Document xmlDoc = DocumentHelper.parseText(new String (content)); 130 131 List list = xmlDoc.getRootElement().content(); 134 list = ((Element)list.get(0)).content(); 135 int counterEldefs = 0; 136 int counterTeplate = 0; 137 for (int i = 0; i < list.size(); i++) { 138 Node n = (Node)list.get(i); 139 short nodeType = n.getNodeType(); 140 if (nodeType == Node.ELEMENT_NODE) { 141 String nodeName = n.getName(); 143 if ("elementdef".equalsIgnoreCase(nodeName)) { 144 if (!checkElementDefOk((Element)n)) { 146 return false; 147 } 148 counterEldefs++; 149 } else if ("template".equalsIgnoreCase(nodeName)) { 150 if (!checkTemplateTagOk((Element)n)) { 152 return false; 153 } 154 counterTeplate++; 155 } else { 156 return false; 158 } 159 160 } else if (nodeType == Node.TEXT_NODE) { 161 String nodeValue = n.getText(); 163 if ((nodeValue != null) && (nodeValue.trim().length() > 0)) { 164 return false; 165 } 166 } else { 167 return false; 169 } 170 } 171 if (counterEldefs != 2 || counterTeplate != 1) { 172 return false; 174 } 175 176 } catch (Exception e) { 177 return false; 178 } 179 } 180 return true; 181 } 182 183 189 private boolean checkElementDefOk(Element el) { 190 191 String elementName = el.attribute("name").getText(); 193 if (!("contenttemplate".equalsIgnoreCase(elementName) || "frametemplate".equalsIgnoreCase(elementName))) { 194 return false; 196 } 197 String elClass = CmsImport.getChildElementTextValue(el, "CLASS"); 199 if (!CmsCompatibleCheck.XML_CONTROL_DEFAULT_CLASS.equals(elClass)) { 200 return false; 201 } 202 String elTemplate = CmsImport.getChildElementTextValue(el, "TEMPLATE"); 203 if (elTemplate == null || elTemplate.indexOf(elementName) < 1) { 204 return false; 207 } 208 return true; 209 } 210 211 217 private boolean checkTemplateTagOk(Element el) { 218 219 List list = el.elements(); 220 if (list.size() > 3) { 221 return false; 223 } 224 for (int i = 0; i < list.size(); i++) { 225 Node n = (Node)list.get(i); 226 short ntype = n.getNodeType(); 227 if (ntype == Node.TEXT_NODE) { 228 String nodeValue = n.getText(); 229 if ((nodeValue != null) && (nodeValue.trim().length() > 0)) { 230 return false; 231 } 232 } else if (ntype == Node.ELEMENT_NODE) { 233 if (!"element".equalsIgnoreCase(n.getName())) { 235 return false; 236 } 237 if (!"frametemplate".equals(((Element)n).attribute("name").getText())) { 238 return false; 239 } 240 } else { 241 return false; 242 } 243 } 244 return true; 245 } 246 247 253 private Node getNextSibling(Node node) { 254 255 Node sibling = null; 256 List content = null; 257 int i = 0; 258 259 Node parent = node.getParent(); 260 if (parent != null) { 261 content = ((Element)parent).content(); 262 i = content.indexOf(node); 263 if (i < content.size() - 1) { 264 sibling = (Node)content.get(i + 1); 265 } 266 } 267 268 return sibling; 269 } 270 271 278 private Node treeWalker(Node root, Node n) { 279 280 Node nextnode = null; 281 if (n.hasContent()) { 282 nextnode = (Node)((Element)n).content().get(0); 285 } else { 286 nextnode = treeWalkerBreadth(root, n); 289 } 290 return nextnode; 291 } 292 293 300 private Node treeWalkerBreadth(Node root, Node n) { 301 302 if (n == root) { 303 return null; 304 } 305 Node nextnode = null; 306 Node parent = null; 307 nextnode = getNextSibling(n); 308 parent = n.getParent(); 309 while (nextnode == null && parent != null && parent != root) { 310 nextnode = getNextSibling(parent); 314 parent = parent.getParent(); 315 } 316 return nextnode; 317 } 318 } | Popular Tags |