1 19 package org.openharmonise.him.editors; 20 21 import java.awt.*; 22 import java.io.*; 23 import java.net.*; 24 25 import javax.swing.*; 26 import javax.xml.parsers.*; 27 import javax.xml.transform.*; 28 import javax.xml.transform.dom.*; 29 import javax.xml.transform.stream.*; 30 31 import org.openharmonise.commons.net.*; 32 import org.openharmonise.him.editors.pagegui.*; 33 import org.openharmonise.him.window.messages.*; 34 import org.openharmonise.vfs.*; 35 import org.openharmonise.vfs.gui.*; 36 import org.openharmonise.vfs.status.*; 37 import org.w3c.dom.*; 38 39 46 public class PageDefinitionEditor extends CompositionEditor implements Editor { 47 48 private boolean m_bResourceCreated = false; 49 50 53 public PageDefinitionEditor() { 54 super(); 55 } 56 57 60 public PathStatusWrapper open(String sPath, AbstractVirtualFileSystem vfs) { 61 VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource(); 62 63 String sTitle = "Page Definition Editor"; 64 65 Frame frame = new Frame(); 66 frame.setIconImage( 67 ((ImageIcon) IconManager 68 .getInstance() 69 .getIcon("16-page-definition.gif")) 70 .getImage()); 71 PageDefinitionDialog dialog = new PageDefinitionDialog(frame, sTitle); 72 73 dialog.setData(vfFile.getContent()); 74 75 dialog.show(); 76 77 if (dialog.getData() != null) { 78 vfFile.setContent(dialog.getData()); 79 } 80 81 return new PathStatusWrapper(null, new VFSStatus()); 82 } 83 84 87 public PathStatusWrapper createNew(String sPath, AbstractVirtualFileSystem vfs) { 88 ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(null, new VFSStatus()); 89 90 String sTitle = "Page Definition Editor"; 91 92 Frame frame = new Frame(); 93 frame.setIconImage( 94 ((ImageIcon) IconManager 95 .getInstance() 96 .getIcon("16-page-definition.gif")) 97 .getImage()); 98 PageDefinitionDialog dialog = new PageDefinitionDialog(frame, sTitle); 99 100 dialog.show(); 101 102 if (dialog.getData() != null) { 103 VirtualFile vfFile = new VirtualFile(sPath); 104 105 if (sPath.startsWith(".webdav/Content/Assets/links")) { 106 vfs.getVirtualFileSystemView().setContentType( 107 vfFile, 108 (String ) MimeTypeMapping.getMimeTypes("xml").get(0)); 109 } 110 111 vfFile.setContent(dialog.getData()); 112 statusWrapper = vfs.addVirtualFile(sPath, vfFile); 113 if(statusWrapper.getStatus().isOK()) { 114 this.m_bResourceCreated = true; 115 } 116 } 117 118 return new PathStatusWrapper(null, statusWrapper.getStatus()); 119 } 120 121 124 public StatusData discardChanges(String sPath, AbstractVirtualFileSystem vfs) { 125 return new VFSStatus(); 126 } 127 128 131 public StatusData export(String sPath, AbstractVirtualFileSystem vfs) { 132 return new VFSStatus(); 133 } 134 135 138 public PathStatusWrapper createNew( 139 String sPath, 140 byte[] content, 141 AbstractVirtualFileSystem vfs) { 142 143 ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(null, new VFSStatus()); 144 145 VirtualFile vfFile = new VirtualFile(sPath); 146 147 if (sPath.startsWith(".webdav/Content/Assets/links")) { 148 vfs.getVirtualFileSystemView().setContentType( 149 vfFile, 150 (String ) MimeTypeMapping.getMimeTypes("xml").get(0)); 151 } 152 153 vfFile.setContent(content); 154 statusWrapper = vfs.addVirtualFile(sPath, vfFile); 155 if(statusWrapper.getStatus().isOK()) { 156 this.m_bResourceCreated = true; 157 } 158 159 return new PathStatusWrapper(null, statusWrapper.getStatus()); 160 } 161 162 165 public boolean hasResourceBeenCreated() { 166 return this.m_bResourceCreated; 167 } 168 169 172 public PathStatusWrapper preview(String sPath, AbstractVirtualFileSystem vfs){ 173 return preview(sPath, vfs, null); 174 } 175 public PathStatusWrapper preview(String sPath, AbstractVirtualFileSystem vfs, Document state) { 176 VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource(); 177 String sPageFileName = vfFile.getFileName(); 178 VirtualFile vfXmlFile = null; 179 VirtualFile vfXslFile = null; 180 String sMessage = null; 181 try { 182 Document xml = 183 DocumentBuilderFactory 184 .newInstance() 185 .newDocumentBuilder() 186 .parse( 187 new org.xml.sax.InputSource ( 188 new StringReader(new String (vfFile.getContent())))); 189 190 Element rootEl = xml.getDocumentElement(); 191 NodeList nl = rootEl.getChildNodes(); 192 String sMimeType = null; 193 for (int i = 0; i < nl.getLength(); i++) { 194 Node node = nl.item(i); 195 if (node.getNodeType() == Node.ELEMENT_NODE) { 196 Element element = (Element) node; 197 if (element.getTagName().equals("XML")) { 198 NodeList nl2 = element.getChildNodes(); 199 for (int j = 0; j < nl2.getLength(); j++) { 200 Node child = nl2.item(j); 201 if (child.getNodeType() == Node.ELEMENT_NODE 202 && ((Element) child).getTagName().equals("href") 203 && child.getChildNodes().getLength() == 1 204 && child.getFirstChild().getNodeType() 205 == Node.TEXT_NODE) { 206 Text txt = (Text) child.getFirstChild(); 207 vfXmlFile = 208 vfs.getVirtualFile(txt.getNodeValue()).getResource(); 209 } 210 } 211 } else if (element.getTagName().equals("XSL")) { 212 NodeList nl2 = element.getChildNodes(); 213 for (int j = 0; j < nl2.getLength(); j++) { 214 Node child = nl2.item(j); 215 if (child.getNodeType() == Node.ELEMENT_NODE 216 && child.getChildNodes().getLength() == 1 217 && child.getFirstChild().getNodeType() 218 == Node.TEXT_NODE) { 219 if (((Element) child) 220 .getTagName() 221 .equals("href")) { 222 Text txt = (Text) child.getFirstChild(); 223 vfXslFile = 224 vfs.getVirtualFile(txt.getNodeValue()).getResource(); 225 } else if ( 226 ((Element) child).getTagName().equals( 227 "mimetype")) { 228 sMimeType = 229 ((Text) child.getFirstChild()) 230 .getData(); 231 } 232 } 233 } 234 } 235 } 236 } 237 if (vfXmlFile != null && vfXslFile != null) { 238 String sOut = invokePublishService(vfXmlFile, vfs, state); 240 if(sOut != null){ 241 Document pageXml = 242 DocumentBuilderFactory 243 .newInstance() 244 .newDocumentBuilder() 245 .parse( 246 new org.xml.sax.InputSource ( 247 new StringReader(sOut))); 248 XSLTEditor xsltEditor = new XSLTEditor(); 250 TransformerFactory factory = TransformerFactory.newInstance(); 251 252 String sWorkingFilePath = 253 xsltEditor.createPreviewFile(vfXslFile); 254 File file = new File(sWorkingFilePath); 255 xsltEditor.downloadPreviewImports(vfXslFile); 256 StreamSource ssource = new StreamSource(new FileReader(file)); 257 ssource.setSystemId(file); 258 Templates templates = factory.newTemplates(ssource); 259 String sFilename = 260 createPreviewFileName(getFileName(vfFile), sMimeType); 261 sWorkingFilePath = createPreviewFile(sFilename); 262 File fFile = new File(sWorkingFilePath); 263 264 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 265 render(pageXml, templates, bos); 266 String result = bos.toString(); 267 String newResult = resolveRelativePaths(result, vfs); 268 BufferedWriter out = 269 new BufferedWriter(new FileWriter(sWorkingFilePath)); 270 out.write(newResult); 271 bos.close(); 272 out.close(); 273 Runtime.getRuntime().exec( 274 "\"" 275 + m_sBrowserPath 276 + "\" " 277 + "file:/" 278 + fFile.toURI().getPath()); 279 } 280 } 281 } catch (Exception e) { 282 sMessage = "There was a problem previewing " + sPageFileName + ":\n" + 283 e.getLocalizedMessage(); 284 e.printStackTrace(System.err); 285 } 286 if(sMessage != null){ 287 MessageHandler.getInstance().fireMessageEvent( 288 sMessage, 289 MessageHandler.TYPE_ERROR); 290 } 291 return new PathStatusWrapper(null, new VFSStatus()); 292 } 293 public void render(Document doc, Templates templates, OutputStream out) 294 throws Exception { 295 try { 296 OutputStreamWriter outwriter = new OutputStreamWriter(out, "UTF-8"); 297 298 Transformer trans = templates.newTransformer(); 299 300 DOMSource ds = new DOMSource(doc.getDocumentElement()); 301 302 StreamResult res = new StreamResult(outwriter); 303 304 trans.transform(ds, res); 305 } catch (TransformerConfigurationException e) { 306 throw new Exception ("Error occured with configuration", e); 307 } catch (TransformerException e) { 308 throw new Exception ("Error occured with tramsformation", e); 309 } catch (UnsupportedEncodingException e) { 310 throw new Exception ("Unsupported encoding error", e); 311 } 312 } 313 protected String createPreviewFileName( 314 String sFilename, 315 String sMimeType) { 316 String sNewFilename = sFilename; 317 if (sMimeType != null) { 318 int index = sFilename.lastIndexOf("."); 319 sNewFilename = sFilename.substring(0, index + 1); 320 sNewFilename += MimeTypeMapping.getExtensionFromMimeType(sMimeType); 321 } 322 return sNewFilename; 323 } 324 protected String resolveRelativePaths( 325 String sContents, 326 AbstractVirtualFileSystem vfs) { 327 URI uri = vfs.getURI(); 328 String sURL = 329 uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort(); 330 String sNewContents = 331 replaceAllIgnoreCase(sContents, "href=\"/", "href=\"" + sURL + "/"); 332 sNewContents = 333 replaceAllIgnoreCase(sNewContents, "src=\"/", "src=\"" + sURL + "/"); 334 sURL += "/webdav/servlet/"; 335 sNewContents = sNewContents.replaceAll("\"HarmoniseServlet", "\"" + sURL + "HarmoniseServlet"); 336 sNewContents = sNewContents.replaceAll("'HarmoniseServlet", "'" + sURL + "HarmoniseServlet"); 337 return sNewContents; 338 } 339 public String replaceAllIgnoreCase(String in, String arg0, String arg1) { 340 String out = in; 341 String lower = in.toLowerCase(); 342 int index = lower.indexOf(arg0.toLowerCase()); 343 if (index > 0) { 344 out = out.substring(0, index) + arg1 345 + out.substring(index + arg0.length()); 346 out = replaceAllIgnoreCase(out, arg0, arg1); 347 } 348 return out; 349 } 350 } 351
| Popular Tags
|