1 31 32 package com.opencms.legacy; 33 34 import org.opencms.importexport.CmsCompatibleCheck; 35 import org.opencms.importexport.CmsImportVersion2; 36 import org.opencms.main.CmsException; 37 import org.opencms.main.OpenCms; 38 import org.opencms.report.I_CmsReport; 39 import org.opencms.util.CmsStringUtil; 40 import org.opencms.xml.CmsXmlException; 41 import org.opencms.xml.CmsXmlUtils; 42 43 import com.opencms.template.*; 44 45 import java.io.ByteArrayInputStream ; 46 import java.io.InputStream ; 47 import java.io.UnsupportedEncodingException ; 48 import java.util.Enumeration ; 49 import java.util.Hashtable ; 50 import java.util.List ; 51 52 import org.dom4j.Attribute; 53 import org.dom4j.Document; 54 import org.dom4j.DocumentHelper; 55 import org.dom4j.Element; 56 import org.xml.sax.InputSource ; 57 58 75 public class CmsImportVersion1 extends CmsImportVersion2 { 76 77 78 private static final int C_IMPORT_VERSION = 1; 79 80 81 private static final String C_VFS_PATH_OLD_BODIES = "/content/bodys/"; 82 83 86 public int getVersion() { 87 88 return CmsImportVersion1.C_IMPORT_VERSION; 89 } 90 91 98 private byte[] convertFile(String filename, byte[] byteContent) { 99 100 byte[] returnValue = byteContent; 101 if (!filename.startsWith("/")) { 102 filename = "/" + filename; 103 } 104 105 String fileContent = new String (byteContent); 106 String encoding = getEncoding(fileContent); 107 if (!"".equals(encoding)) { 108 try { 110 fileContent = new String (byteContent, encoding); 112 } catch (UnsupportedEncodingException e) { 113 if (DEBUG > 0) { 115 System.err.println("[" 116 + this.getClass().getName() 117 + ".convertFile()]: Encoding not supported, using default encoding."); 118 } 119 } 120 } else { 121 if (DEBUG > 0) { 123 System.err.println("[" 124 + this.getClass().getName() 125 + ".convertFile()]: Encoding not set, using default encoding and setting it in <?xml...?>."); 126 } 127 encoding = OpenCms.getSystemInfo().getDefaultEncoding(); 128 fileContent = setEncoding(fileContent, encoding); 129 } 130 if (filename.indexOf("frametemplates") != -1) { 132 fileContent = scanFrameTemplate(fileContent); 133 } 134 if (filename.indexOf(C_VFS_PATH_OLD_BODIES) != -1 || filename.indexOf(CmsCompatibleCheck.VFS_PATH_BODIES) != -1) { 136 if (DEBUG > 0) { 137 System.err.println("[" + this.getClass().getName() + ".convertFile()]: Starting scan of body page."); 138 } 139 fileContent = convertPageBody(fileContent, filename); 140 } 141 fileContent = setDirectories(fileContent, m_cms.getRequestContext().getDirectoryTranslator().getTranslations()); 143 144 try { 146 returnValue = fileContent.getBytes(encoding); 147 } catch (UnsupportedEncodingException e) { 148 returnValue = fileContent.getBytes(); 150 } 151 return returnValue; 152 } 153 154 162 163 private String convertPageBody(String content, String fileName) { 164 165 String nodeName = null; 166 167 boolean createTemplateTags = false; 169 Hashtable templateElements = new Hashtable (); 170 171 boolean found = false; 173 for (int i = 0; i < m_webAppNames.size(); i++) { 174 if (content.indexOf((String )m_webAppNames.get(i)) != -1) { 175 found = true; 176 } 177 } 178 if (content.indexOf("<edittemplate>") != -1 || content.indexOf("<EDITTEMPLATE>") != -1) { 180 found = true; 181 } 182 183 if (found) { 185 InputStream in = new ByteArrayInputStream (content.getBytes()); 186 String editString, templateString; 187 try { 188 InputSource source = new InputSource (in); 190 Document doc = CmsXmlUtils.unmarshalHelper(source, null); 191 192 nodeName = "edittemplate"; 194 Element root = doc.getRootElement(); 195 List editNodes = root.elements(nodeName.toLowerCase()); 196 editNodes.addAll(root.elements(nodeName.toUpperCase())); 197 if (editNodes.size() < 1) { 199 if (DEBUG > 0) { 200 System.err.println("[" 201 + this.getClass().getName() 202 + ".convertPageBody()]: No <edittemplate> found, creating it."); 203 } 204 205 createTemplateTags = true; 206 207 nodeName = "TEMPLATE"; 208 List templateNodes = root.elements(nodeName.toLowerCase()); 209 List attributes; 210 templateNodes.addAll(root.elements(nodeName.toUpperCase())); 211 212 Element templateTag; 214 for (int i = 0; i < templateNodes.size(); i++) { 215 216 templateTag = (Element)templateNodes.get(i); 218 editString = templateTag.getText(); 219 templateString = editString; 220 221 try { 223 templateString = CmsXmlTemplateLinkConverter.convertFromImport( 224 templateString, 225 m_webappUrl, 226 fileName); 227 } catch (CmsException e) { 228 throw new CmsLegacyException("[" 229 + this.getClass().getName() 230 + ".convertPageBody()] can't parse the content: ", e); 231 } 232 233 attributes = ((Element)templateNodes.get(i)).attributes(); 235 236 String templateName = ""; 237 238 if (attributes.size() > 0) { 239 templateName = ((Attribute)attributes.get(0)).getName(); 240 } 241 242 nodeName = "edittemplate"; 244 Element newNode = DocumentHelper.createElement(nodeName.toLowerCase()); 245 if (newNode == null) { 246 newNode = root.addElement(nodeName.toUpperCase()); 247 } 248 newNode.addCDATA(editString); 249 attributes = newNode.attributes(); 251 if (!templateName.equals("")) { 252 newNode.addAttribute("name", templateName); 253 } 254 255 ((Element)root.elements("XMLTEMPLATE").get(0)).add(newNode); 257 if (templateName.equals("")) { 259 templateName = "noNameKey"; 260 } 261 templateElements.put(templateName, templateString); 262 } 263 while (templateNodes.size() > 0) { 265 ((Element)root.elements("XMLTEMPLATE").get(0)).remove((Element)templateNodes.get(0)); 266 } 267 268 } 269 Element editTemplate; 271 for (int i = 0; i < editNodes.size(); i++) { 272 editTemplate = (Element)editNodes.get(i); 274 editString = editTemplate.getText(); 275 for (int k = 0; k < m_webAppNames.size(); k++) { 276 editString = CmsStringUtil.substitute( 277 editString, 278 (String )m_webAppNames.get(k), 279 CmsStringUtil.MACRO_OPENCMS_CONTEXT + "/"); 280 } 281 282 editTemplate.clearContent(); 284 editTemplate.addCDATA(editString); 285 286 } 287 289 content = CmsXmlUtils.marshal(doc, OpenCms.getSystemInfo().getDefaultEncoding()); 290 if (createTemplateTags) { 292 content = content.substring(0, content.lastIndexOf("</XMLTEMPLATE>")); 293 Enumeration en = templateElements.keys(); 295 while (en.hasMoreElements()) { 296 String key = (String )en.nextElement(); 297 String value = (String )templateElements.get(key); 298 if (key.equals("noNameKey")) { 300 content += "\n<TEMPLATE><![CDATA[" + value; 301 } else { 302 content += "\n<TEMPLATE name=\"" + key + "\"><![CDATA[" + value; 304 } 305 content += "]]></TEMPLATE>\n"; 306 } 307 content += "\n</XMLTEMPLATE>"; 308 } 309 310 } catch (Exception exc) { 311 } 313 } 314 return content; 315 } 316 317 323 private String scanFrameTemplate(String content) { 324 325 if (content.toLowerCase().indexOf("http-equiv=\"content-type\"") == -1) { 327 content = CmsStringUtil 328 .substitute( 329 content, 330 "</head>", 331 "<meta http-equiv=\"content-type\" content=\"text/html; charset=]]><method name=\"getEncoding\"/><![CDATA[\">\n</head>"); 332 } else { 333 if (content.toLowerCase().indexOf("charset=]]><method name=\"getencoding\"/>") == -1) { 335 String fileStart = content.substring(0, content.toLowerCase().indexOf("charset=") + 8); 336 String editContent = content.substring(content.toLowerCase().indexOf("charset=")); 337 editContent = editContent.substring(editContent.indexOf("\"")); 338 String newEncoding = "]]><method name=\"getEncoding\"/><![CDATA["; 339 content = fileStart + newEncoding + editContent; 340 } 341 } 342 return content; 343 } 344 345 353 protected byte[] convertContent(String source, String destination, byte[] content, String resType) { 354 355 if (getVersion() < 2) { 357 if ("page".equals(resType) || ("plain".equals(resType)) || ("XMLTemplate".equals(resType))) { 359 if (DEBUG > 0) { 360 System.err.println("#########################"); 361 System.err.println("[" 362 + this.getClass().getName() 363 + ".convertContent()]: starting conversion of \"" 364 + resType 365 + "\" resource " 366 + source 367 + "."); 368 } 369 content = convertFile(source, content); 371 } 372 if (getVersion() == 0) { 374 if (!(new CmsCompatibleCheck()).isTemplateCompatible(m_importPath + destination, content, resType)) { 376 resType = CmsResourceTypeCompatiblePlain.getStaticTypeName(); 377 m_report.print(Messages.get().container(Messages.RPT_MUST_SET_TO_1, resType), I_CmsReport.FORMAT_WARNING); 378 } 379 } 380 } 381 382 return super.convertContent(source, destination, content, resType); 384 } 385 386 393 public static Document getXmlDocument(InputStream stream) throws CmsXmlException { 394 395 return CmsXmlUtils.unmarshalHelper(new InputSource (stream), null); 396 } 397 398 } 399 | Popular Tags |