1 19 package org.openharmonise.him.editors; 20 21 import java.awt.*; 22 import java.io.*; 23 import java.net.*; 24 import java.rmi.*; 25 26 import javax.swing.*; 27 import javax.xml.parsers.*; 28 import javax.xml.rpc.*; 29 30 import org.openharmonise.commons.xml.*; 31 import org.openharmonise.commons.xml.namespace.*; 32 import org.openharmonise.him.configuration.*; 33 import org.openharmonise.him.editors.preview.*; 34 import org.openharmonise.him.harmonise.*; 35 import org.openharmonise.him.publish.*; 36 import org.openharmonise.him.window.*; 37 import org.openharmonise.vfs.*; 38 import org.openharmonise.vfs.gui.*; 39 import org.openharmonise.vfs.status.*; 40 import org.w3c.dom.*; 41 import org.xml.sax.*; 42 43 50 public class CompositionEditor extends GenericEditor implements Editor { 51 52 private boolean m_bResourceCreated = false; 53 54 private static final String TAG_HARMONISE_PUBLISH_SERVICE = 55 "HarmonisePublishService"; 56 protected String m_sBrowserPath = null; 57 60 public CompositionEditor() { 61 super(); 62 setup(); 63 } 64 65 68 private void setup() { 69 String sValue = 70 ConfigStore.getInstance().getPropertyValue("BROWSER_PATH"); 71 if (sValue == null || sValue.length() < 1) { 72 File fPath = 74 new File("C:/Program Files/Internet Explorer/IEXPLORE.EXE"); 75 if (fPath.exists()) { 76 this.m_sBrowserPath = fPath.getAbsolutePath(); 77 ConfigStore.getInstance().setProperty( 78 "BROWSER_PATH", 79 this.m_sBrowserPath); 80 } else { 81 JFileChooser chooser = new JFileChooser(); 82 chooser.setDialogTitle("Select Browser path"); 83 int returnVal = 84 chooser.showOpenDialog( 85 DisplayManager.getInstance().getMainWindow()); 86 87 this.m_sBrowserPath = 88 chooser.getSelectedFile().getAbsolutePath(); 89 ConfigStore.getInstance().setProperty( 90 "BROWSER_PATH", 91 this.m_sBrowserPath); 92 93 } 94 } else { 95 this.m_sBrowserPath = sValue; 96 } 97 98 } 99 100 103 public PathStatusWrapper createNew(String sPath, AbstractVirtualFileSystem vfs) { 104 ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(null, new VFSStatus()); 105 106 VirtualFile vfFile = new VirtualFile(sPath); 107 108 vfs.getVirtualFileSystemView().setContentType(vfFile, "text/xml"); 109 110 StringBuffer sBuff = new StringBuffer (); 111 InputStream is = null; 112 113 if (sPath.startsWith(HarmonisePaths.PATH_OBJECT_TEMPLATES)) { 114 is = 115 XMetaLEditor.class.getResourceAsStream( 116 "/org/openharmonise/him/icons/xml/newObjectTemplate.xml"); 117 } else { 118 is = 119 XMetaLEditor.class.getResourceAsStream( 120 "/org/openharmonise/him/icons/xml/newPageTemplate.xml"); 121 } 122 123 BufferedReader buff = new BufferedReader(new InputStreamReader(is)); 124 125 String sLine = null; 126 try { 127 while ((sLine = buff.readLine()) != null) { 128 sBuff.append(sLine); 129 } 130 Document xml = null; 131 try { 132 xml = 133 DocumentBuilderFactory 134 .newInstance() 135 .newDocumentBuilder() 136 .parse( 137 new org.xml.sax.InputSource ( 138 new StringReader(sBuff.toString()))); 139 } catch (SAXException e1) { 140 e1.printStackTrace(); 141 } catch (ParserConfigurationException e1) { 142 e1.printStackTrace(); 143 } catch (FactoryConfigurationError e1) { 144 e1.printStackTrace(); 145 } 146 147 if (xml != null) { 148 Element rootEl = (Element) xml.getDocumentElement(); 149 150 XMLPrettyPrint printer = new XMLPrettyPrint(); 151 152 vfFile.setContent(printer.printNode(rootEl).getBytes()); 153 } 154 155 statusWrapper = vfs.addVirtualFile(sPath, vfFile); 156 157 vfFile = vfs.getVirtualFile(sPath).getResource(); 158 159 this.m_bResourceCreated = true; 160 161 PathStatusWrapper pathStatus = new PathStatusWrapper(super.createWorkingFile(vfFile), statusWrapper.getStatus()); 162 163 return pathStatus; 164 } catch (IOException e) { 165 e.printStackTrace(); 166 } catch (NamespaceClashException e) { 167 e.printStackTrace(); 168 } 169 statusWrapper.getStatus().setStatusLevel(StatusData.LEVEL_ERROR); 170 return new PathStatusWrapper(null, statusWrapper.getStatus()); 171 } 172 173 176 public boolean hasResourceBeenCreated() { 177 return this.m_bResourceCreated; 178 } 179 180 183 public PathStatusWrapper preview(String sPath, AbstractVirtualFileSystem vfs) { 184 VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource(); 185 String sFilename = null; 186 String sMessage = null; 187 try { 188 String sOut = invokePublishService(vfFile, vfs, null); 189 if(sOut != null){ 190 sFilename = this.getFileName(vfFile); 191 File fFile = super.createPreviewFile(sFilename, sOut.getBytes()); 192 193 Runtime.getRuntime().exec( 194 "\"" 195 + this.m_sBrowserPath 196 + "\" " 197 + "file:/" 198 + fFile.toURI().getPath()); 199 } 200 } catch (SAXException e) { 201 e.getLocalizedMessage(); 202 e.printStackTrace(System.err); 203 } catch (Exception e) { 204 e.printStackTrace(System.err); 205 } 206 207 return new PathStatusWrapper(null, new VFSStatus()); 208 } 209 public static String invokePublishService( 210 VirtualFile vfFile, 211 AbstractVirtualFileSystem vfs, Document state) 212 throws 213 SAXException, 214 IOException, 215 ParserConfigurationException, 216 RemoteException, 217 ServiceException { 218 String sOutXml = null; 219 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 220 factory.setNamespaceAware(true); 221 Document pageXml = 222 factory.newDocumentBuilder().parse( 223 new org.xml.sax.InputSource ( 224 new StringReader(new String (vfFile.getContent())))); 225 Document previewXml = 226 DocumentBuilderFactory 227 .newInstance() 228 .newDocumentBuilder() 229 .newDocument(); 230 Element rootEl = 231 previewXml.createElement(TAG_HARMONISE_PUBLISH_SERVICE); 232 previewXml.appendChild(rootEl); 233 Document stateDoc = null; 234 if(state == null){ 235 stateDoc = getState(vfFile); 236 } else { 237 stateDoc = state; 238 } 239 Node node = null; 240 if(stateDoc != null){ 241 node = XMLUtils.copyNode(stateDoc.getDocumentElement(), previewXml); 242 rootEl.appendChild(node); 243 node = XMLUtils.copyNode(pageXml.getDocumentElement(), previewXml); 244 rootEl.appendChild(node); 245 246 String sInXml = previewXml.getDocumentElement().toString(); 247 URI uri = vfs.getURI(); 249 String sURL = 250 uri.getScheme() 251 + "://" 252 + uri.getHost() 253 + ":" 254 + uri.getPort() 255 + "/webdav/services/PublishService"; 256 URL endPointURL = new URL(sURL); 257 sOutXml = 258 PublishServiceClient.getPublishXML(endPointURL, sInXml); 259 } 260 return sOutXml; 261 } 262 private static Document getState(VirtualFile vfFile) { 263 Frame frame = new Frame(); 264 frame.setIconImage( 265 ((ImageIcon) IconManager 266 .getInstance() 267 .getIcon("16-page-definition.gif")) 268 .getImage()); 269 String sVFile = null; 270 if(vfFile.isVersionable() && ((VersionedVirtualFile)vfFile).getState().equals(VirtualFile.STATE_PENDING) && ((VersionedVirtualFile)vfFile).getLiveVersionPath()!=null ) { 271 sVFile = ((VersionedVirtualFile)vfFile).getLiveVersionPath(); 272 } else { 273 sVFile = vfFile.getFullPath(); 274 } 275 StateDialog sd = new StateDialog(frame, "state selector", sVFile); 276 Document doc = sd.getState(); 277 if(doc == null){ 278 sd.show(); 279 doc = sd.getState(); 280 } 281 return doc; 282 } 283 } 284
| Popular Tags
|