1 19 package org.openharmonise.him.editors; 20 21 import java.io.*; 22 import java.util.*; 23 24 import javax.swing.*; 25 import javax.xml.parsers.*; 26 27 import org.openharmonise.commons.xml.*; 28 import org.openharmonise.commons.xml.namespace.*; 29 import org.openharmonise.him.configuration.*; 30 import org.openharmonise.him.editors.filefilters.XMLFilter; 31 import org.openharmonise.him.window.*; 32 import org.openharmonise.him.window.messages.*; 33 import org.openharmonise.vfs.*; 34 import org.openharmonise.vfs.metadata.*; 35 import org.openharmonise.vfs.metadata.value.*; 36 import org.openharmonise.vfs.status.*; 37 import org.w3c.dom.*; 38 import org.xml.sax.*; 39 40 41 48 public class XMetaLEditor extends CompositionEditor implements Editor { 49 50 private boolean m_bResourceCreated = false; 51 52 55 private static String m_sXMetalPath = null; 56 57 60 private static boolean m_bXMetalAvailable = true; 61 private static boolean m_bTested = false; 62 63 public XMetaLEditor() { 64 super(); 65 } 66 67 70 public PathStatusWrapper open(String sPath, AbstractVirtualFileSystem vfs) { 71 PathStatusWrapper pathStatus = new PathStatusWrapper(null, new VFSStatus()); 72 73 String sWorkingFilePath = null; 74 75 VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource(); 76 77 if(isXMetalAvailable()) { 78 VFSStatus status = new VFSStatus(); 79 try { 80 sWorkingFilePath = createWorkingFile(vfFile); 81 XMetaLController.getInstance().openFile(sWorkingFilePath); 82 } catch (Exception e1) { 83 e1.printStackTrace(); 84 status.setStatusCode(StatusData.STATUS_INVALID_RESOURCE_STATE); 85 } finally { 86 pathStatus = new PathStatusWrapper(sWorkingFilePath, status); 87 } 88 } else { 89 FileAssetEditor edit = new FileAssetEditor(); 90 pathStatus = edit.open(sPath, vfs); 91 } 92 93 return pathStatus; 94 } 95 96 99 public PathStatusWrapper createNew(String sPath, AbstractVirtualFileSystem vfs) { 100 ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(null, new VFSStatus()); 101 102 VirtualFile vfFile = new VirtualFile(sPath); 103 104 vfs.getVirtualFileSystemView().setContentType(vfFile, "text/xml"); 105 106 StringBuffer sBuff = new StringBuffer (); 107 InputStream is = null; 108 109 is = XMetaLEditor.class.getResourceAsStream("/org/openharmonise/him/icons/xml/newdoc.xml"); 110 111 BufferedReader buff = new BufferedReader( new InputStreamReader(is)); 112 113 String sLine = null; 114 try { 115 while((sLine=buff.readLine())!=null) { 116 sBuff.append(sLine); 117 } 118 Document xml = null; 119 try { 120 xml = 121 DocumentBuilderFactory 122 .newInstance() 123 .newDocumentBuilder() 124 .parse( 125 new org.xml.sax.InputSource ( 126 new StringReader(sBuff.toString()))); 127 } catch (SAXException e1) { 128 e1.printStackTrace(); 129 } catch (ParserConfigurationException e1) { 130 e1.printStackTrace(); 131 } catch (FactoryConfigurationError e1) { 132 e1.printStackTrace(); 133 } 134 135 if(xml!=null) { 136 Element rootEl = (Element) xml.getDocumentElement(); 137 138 XMLPrettyPrint printer = new XMLPrettyPrint(); 139 140 vfFile.setContent(printer.printNode(rootEl).getBytes()); 141 } 142 143 statusWrapper = vfs.addVirtualFile(sPath, vfFile); 144 145 vfFile = vfs.getVirtualFile(sPath).getResource(); 146 147 if(statusWrapper.getStatus().isOK()) { 148 this.m_bResourceCreated = true; 149 return new PathStatusWrapper(super.createWorkingFile(vfFile), statusWrapper.getStatus()); 150 } 151 } catch (IOException e) { 152 e.printStackTrace(); 153 } catch (NamespaceClashException e) { 154 e.printStackTrace(); 155 } 156 statusWrapper.getStatus().setStatusLevel(StatusData.LEVEL_ERROR); 157 return new PathStatusWrapper(null, statusWrapper.getStatus()); 158 } 159 160 164 private boolean isXMetalAvailable() { 165 if(m_bTested == false){ 166 String sValue = ConfigStore.getInstance().getPropertyValue("XMETAL_PATH"); 167 if(sValue==null || sValue.length()<1) { 168 File fPath = new File("C:\\Program Files\\Corel\\XMetaL 4\\Author"); 169 if(fPath.exists()) { 170 XMetaLEditor.m_sXMetalPath = fPath.getAbsolutePath(); 171 ConfigStore.getInstance().setProperty("XMETAL_PATH", XMetaLEditor.m_sXMetalPath); 172 } else { 173 JFileChooser chooser = new JFileChooser(); 174 chooser.setDialogTitle("Select XMetaL 4: Author path"); 175 int returnVal = 176 chooser.showOpenDialog( 177 DisplayManager.getInstance().getMainWindow()); 178 if (returnVal == JFileChooser.APPROVE_OPTION) { 179 XMetaLEditor.m_sXMetalPath = chooser.getSelectedFile().getAbsolutePath(); 180 ConfigStore.getInstance().setProperty("XMETAL_PATH", XMetaLEditor.m_sXMetalPath); 181 } else { 182 m_bXMetalAvailable = false; 183 } 184 } 185 } else { 186 XMetaLEditor.m_sXMetalPath = sValue; 187 } 188 189 if(m_bXMetalAvailable && XMetaLEditor.m_sXMetalPath!=null && XMetaLEditor.m_sXMetalPath.length()>0) { 190 File file = new File(XMetaLEditor.m_sXMetalPath + "\\rules\\harmonise-document.xsd"); 191 192 InputStream is = XMetaLEditor.class.getResourceAsStream("/org/openharmonise/him/icons/files/harmonise-document.xsd"); 193 FileOutputStream fo = null; 194 195 try { 196 fo = new FileOutputStream(file); 197 int nValue; 198 while((nValue=is.read())!=-1) { 199 fo.write(nValue); 200 } 201 } catch (FileNotFoundException e) { 202 e.printStackTrace(); 203 } catch (IOException e) { 204 e.printStackTrace(); 205 } 206 207 file = new File(XMetaLEditor.m_sXMetalPath + "\\rules\\harmonise-document.xac"); 208 209 is = XMetaLEditor.class.getResourceAsStream("/org/openharmonise/him/icons/files/harmonise-document.xac"); 210 fo = null; 211 212 try { 213 fo = new FileOutputStream(file); 214 int nValue; 215 while((nValue=is.read())!=-1) { 216 fo.write(nValue); 217 } 218 } catch (FileNotFoundException e) { 219 e.printStackTrace(); 220 } catch (IOException e) { 221 e.printStackTrace(); 222 } 223 224 file = new File(this.m_sXMetalPath + "\\rules\\harmonise-document.rld"); 225 226 is = XMetaLEditor.class.getResourceAsStream("/org/openharmonise/him/icons/files/harmonise-document.rld"); 227 fo = null; 228 229 try { 230 fo = new FileOutputStream(file); 231 int nValue; 232 while((nValue=is.read())!=-1) { 233 fo.write(nValue); 234 } 235 } catch (FileNotFoundException e) { 236 e.printStackTrace(); 237 } catch (IOException e) { 238 e.printStackTrace(); 239 } 240 241 file = new File(this.m_sXMetalPath + "\\rules\\harmonise-document.tbr"); 242 243 is = XMetaLEditor.class.getResourceAsStream("/org/openharmonise/him/icons/files/harmonise-document.tbr"); 244 fo = null; 245 246 try { 247 fo = new FileOutputStream(file); 248 int nValue; 249 while((nValue=is.read())!=-1) { 250 fo.write(nValue); 251 } 252 } catch (FileNotFoundException e) { 253 e.printStackTrace(); 254 } catch (IOException e) { 255 e.printStackTrace(); 256 } 257 258 259 } 260 m_bTested = true; 261 } 262 return m_bXMetalAvailable; 263 } 264 265 268 protected String createWorkingFile(VirtualFile vfFile) { 269 String sFilepath = super.createWorkingFile(vfFile); 270 271 if(sFilepath==null) { 272 return null; 273 } 274 275 File fFile = new File(sFilepath); 276 277 FileOutputStream fos = null; 278 279 Document xml = null; 280 try { 281 ByteArrayInputStream bais = new ByteArrayInputStream(vfFile.getContent()); 282 InputStreamReader isr = new InputStreamReader(bais, "UTF-8"); 283 284 xml = 285 DocumentBuilderFactory 286 .newInstance() 287 .newDocumentBuilder() 288 .parse( 289 new org.xml.sax.InputSource (isr)); 290 } catch (SAXException e1) { 291 e1.printStackTrace(); 292 } catch (ParserConfigurationException e1) { 293 e1.printStackTrace(); 294 } catch (FactoryConfigurationError e1) { 295 e1.printStackTrace(); 296 } catch (IOException e) { 297 e.printStackTrace(); 298 } 299 300 if(xml!=null) { 301 Element rootEl = (Element) xml.getDocumentElement(); 302 303 String sXMPath = XMetaLEditor.m_sXMetalPath; 304 sXMPath = replace(sXMPath, "\\", "/"); 305 sXMPath = replace(sXMPath, " ", "%20"); 306 307 String sValue = "http://www.simulacramedia.com/harmonise/document file:///" + sXMPath + "/Rules/harmonise-document.xsd"; 308 309 rootEl.setAttribute("xsi:schemaLocation", sValue); 310 rootEl.setAttribute("xmlns", "http://www.simulacramedia.com/harmonise/document"); 311 rootEl.setAttribute("xmlns:" + NamespaceType.XML_SCHEMA_INSTANCE.getPrefix(), NamespaceType.XML_SCHEMA_INSTANCE.getURI()); 312 313 XMLPrettyPrint printer = new XMLPrettyPrint(); 314 OutputStreamWriter osw = null; 315 try { 316 317 fos = new FileOutputStream(fFile); 318 319 osw = new OutputStreamWriter(fos, "UTF-8"); 320 osw.write(printer.printNode(rootEl)); 321 } catch (FileNotFoundException e) { 322 e.printStackTrace(); 323 } catch (IOException e) { 324 e.printStackTrace(); 325 } catch (NamespaceClashException e) { 326 e.printStackTrace(); 327 } finally { 328 try { 329 osw.close(); 330 fos.close(); 331 } catch (IOException e1) { 332 e1.printStackTrace(); 333 } 334 } 335 } 336 337 return fFile.getAbsolutePath(); 338 } 339 340 String replace(String s, String one, String another) { 341 if (s==null || one==null || another==null || s.equals("")) return ""; 343 String res = ""; 344 int i = s.indexOf(one,0); 345 int lastpos = 0; 346 while (i != -1) { 347 res += s.substring(lastpos,i) + another; 348 lastpos = i + one.length(); 349 i = s.indexOf(one,lastpos); 350 } 351 res += s.substring(lastpos); return res; 353 } 354 355 358 public StatusData export(String sPath, AbstractVirtualFileSystem vfs) { 359 VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource(); 360 String sFilename = super.getFileName(vfFile); 361 362 String sTempPath = m_sEditFilePath + sFilename; 363 File fTempFile = new File(sTempPath); 364 365 JFileChooser chooser = new JFileChooser(); 366 chooser.setSelectedFile(fTempFile); 367 368 chooser.setFileFilter(new XMLFilter()); 369 370 int returnVal = 371 chooser.showSaveDialog( 372 DisplayManager.getInstance().getMainWindow()); 373 if (returnVal == JFileChooser.APPROVE_OPTION) { 374 File fFile = chooser.getSelectedFile(); 375 super.createWorkingFile(vfFile, fFile); 376 } 377 378 return new VFSStatus(); 379 } 380 381 384 public PathStatusWrapper createNew(String sPath, byte[] content, AbstractVirtualFileSystem vfs) { 385 ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(null, new VFSStatus()); 386 387 VirtualFile vfFile = new VirtualFile(sPath); 388 389 vfs.getVirtualFileSystemView().setContentType(vfFile, "text/xml"); 390 391 vfFile.setContent(content); 392 393 394 statusWrapper = vfs.addVirtualFile(sPath, vfFile); 395 396 vfFile = vfs.getVirtualFile(sPath).getResource(); 397 this.m_bResourceCreated = true; 398 399 400 if(statusWrapper.getStatus().isOK()) { 401 this.m_bResourceCreated = true; 402 return new PathStatusWrapper(super.createWorkingFile(vfFile), statusWrapper.getStatus()); 403 } else { 404 statusWrapper.getStatus().setStatusLevel(StatusData.LEVEL_ERROR); 405 return new PathStatusWrapper(null, statusWrapper.getStatus()); 406 } 407 } 408 409 412 public boolean hasResourceBeenCreated() { 413 return this.m_bResourceCreated; 414 } 415 416 419 public PathStatusWrapper preview(String sPath, AbstractVirtualFileSystem vfs) { 420 VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource(); 421 PropertyInstance propInst = vfFile.getProperty(NamespaceType.OHRM.getURI(), "harmonise-id"); 422 String sId = getPropValue(propInst); 423 424 System.out.println("Doc id: " + sId); 425 426 String pagePath = getPreviewPagePath(vfFile,vfs); 427 if(pagePath != null) { 428 PageDefinitionEditor pde = new PageDefinitionEditor(); 429 Document stateDoc = null; 430 try { 431 stateDoc = DocumentBuilderFactory 432 .newInstance() 433 .newDocumentBuilder().newDocument(); 434 Element stateRoot = stateDoc.createElement("State"); 435 Element docEl = stateDoc.createElement("Document"); 436 docEl.setAttribute("id", sId); 437 stateRoot.appendChild(docEl); 438 stateDoc.appendChild(stateRoot); 439 } catch (ParserConfigurationException e) { 440 e.printStackTrace(); 441 } catch (FactoryConfigurationError e) { 442 e.printStackTrace(); 443 } 444 445 pde.preview(pagePath, vfs, stateDoc); 446 } 447 return new PathStatusWrapper(null, new VFSStatus()); 448 } 449 private String getPreviewPagePath(VirtualFile vfFile, AbstractVirtualFileSystem vfs){ 450 String path = null; 451 PropertyInstance propInst = vfFile.getProperty(NamespaceType.OHRM.getURI(),"PREVIEWPAGE"); 452 if(propInst != null){ 453 List values = propInst.getValues(); 454 Iterator iter = values.iterator(); 455 if(iter.hasNext()){ 456 ResourceValue val = (ResourceValue) iter.next(); 457 path = val.getValue(); 458 } 459 } 460 if(path == null) { 461 String fullPath = ((VersionedVirtualFile)vfFile).getLiveVersionPath(); 462 if(fullPath == null){ 463 fullPath = vfFile.getFilePath(); 464 } 465 ((VersionedVirtualFile)vfFile).getLiveVersionPath(); 466 System.out.println("Trying " + fullPath); 468 if(fullPath.equalsIgnoreCase("/webdav")){ 469 path = "/webdav/Website/Page Definition/defaultpreview"; 471 VirtualFile vFile = vfs.getVirtualFile(path).getResource(); 472 if(vFile == null){ 473 String sMsg = "You have no preview page for this collection and no default preview page set up."; 474 MessageHandler.getInstance().fireMessageEvent(sMsg, MessageHandler.TYPE_ERROR); 475 return null; 476 } 477 } else { 478 path = getPreviewPagePath(vfs.getVirtualFile(fullPath).getResource(), vfs); 479 } 480 } 481 return path; 482 } 483 484 485 private String getPropValue(PropertyInstance propInst){ 486 String sValue = null; 487 if(propInst!=null && propInst.getValues().size()>0) { 488 if(propInst.getValues().get(0) instanceof StringValue) { 489 StringValue value = (StringValue) propInst.getValues().get(0); 490 if(value.getValue()!=null && value.getValue().length()>0) { 491 sValue = value.getValue(); 492 } 493 } 494 } 495 return sValue; 496 } 497 } 498 | Popular Tags |