1 19 package org.openharmonise.him.editors; 20 21 import java.io.BufferedReader ; 22 import java.io.ByteArrayInputStream ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.io.StringReader ; 28 import java.net.MalformedURLException ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import java.util.StringTokenizer ; 32 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 import javax.xml.parsers.FactoryConfigurationError ; 35 import javax.xml.parsers.ParserConfigurationException ; 36 37 import org.openharmonise.commons.net.*; 38 import org.openharmonise.commons.xml.*; 39 import org.openharmonise.commons.xml.namespace.*; 40 import org.openharmonise.him.harmonise.*; 41 import org.openharmonise.vfs.*; 42 import org.openharmonise.vfs.status.*; 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.Element ; 45 import org.w3c.dom.Node ; 46 import org.w3c.dom.NodeList ; 47 import org.xml.sax.InputSource ; 48 import org.xml.sax.SAXException ; 49 50 51 60 public class XSLTEditor extends GenericEditor implements Editor { 61 62 private boolean m_bResourceCreated = false; 63 64 67 public XSLTEditor() { 68 super(); 69 } 70 71 74 public PathStatusWrapper createNew(String sPath, AbstractVirtualFileSystem vfs) { 75 ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(null, new VFSStatus()); 76 77 VirtualFile vfFile = new VirtualFile(sPath); 78 79 vfs.getVirtualFileSystemView().setContentType(vfFile, MimeTypeMapping.XSLT.getMimeType()); 80 81 StringBuffer sBuff = new StringBuffer (); 82 InputStream is = null; 83 84 is = XMetaLEditor.class.getResourceAsStream("/org/openharmonise/him/icons/xml/newXSLT.xsl"); 85 86 BufferedReader buff = new BufferedReader ( new InputStreamReader (is)); 87 88 String sLine = null; 89 try { 90 while((sLine=buff.readLine())!=null) { 91 sBuff.append(sLine); 92 } 93 Document xml = null; 94 try { 95 xml = 96 DocumentBuilderFactory 97 .newInstance() 98 .newDocumentBuilder() 99 .parse( 100 new org.xml.sax.InputSource ( 101 new StringReader (sBuff.toString()))); 102 } catch (SAXException e1) { 103 e1.printStackTrace(); 104 } catch (ParserConfigurationException e1) { 105 e1.printStackTrace(); 106 } catch (FactoryConfigurationError e1) { 107 e1.printStackTrace(); 108 } 109 110 if(xml!=null) { 111 Element rootEl = (Element ) xml.getDocumentElement(); 112 113 XMLPrettyPrint printer = new XMLPrettyPrint(); 114 115 vfFile.setContent(printer.printNode(rootEl).getBytes()); 116 } 117 118 statusWrapper = vfs.addVirtualFile(sPath, vfFile); 119 120 vfFile = vfs.getVirtualFile(sPath).getResource(); 121 122 if(statusWrapper.getStatus().isOK()) { 123 this.m_bResourceCreated = true; 124 return new PathStatusWrapper(createWorkingFile(vfFile), statusWrapper.getStatus()); 125 } 126 } catch (IOException e) { 127 e.printStackTrace(); 128 } catch (NamespaceClashException e) { 129 e.printStackTrace(); 130 } 131 statusWrapper.getStatus().setStatusLevel(StatusData.LEVEL_ERROR); 132 return new PathStatusWrapper(null, statusWrapper.getStatus()); 133 } 134 protected String createPreviewFile(VirtualFile vfFile){ 135 return createWorkingFile(vfFile,m_sPreviewFilePath, true); 136 } 137 protected String createWorkingFile(VirtualFile vfFile) { 138 return createWorkingFile(vfFile,m_sEditFilePath, true); 139 } 140 141 144 private String createWorkingFile(VirtualFile vfFile, String path, boolean bOverWrite) { 145 String sVirtualPath = vfFile.getFullPath(); 146 String sRelativeVirtualPath = sVirtualPath.substring(HarmonisePaths.PATH_XSLT.length() + 1); 147 148 String sBuildPath = path; 149 StringTokenizer sTok = new StringTokenizer (sRelativeVirtualPath, "/"); 150 while(sTok.hasMoreTokens()) { 151 String sToken = sTok.nextToken(); 152 if(sTok.hasMoreTokens()) { 153 sBuildPath = sBuildPath + "\\" + sToken; 154 File dir = new File (sBuildPath); 155 if(!dir.exists()) { 156 dir.mkdir(); 157 } 158 } 159 } 160 161 String sRealFilePath = sBuildPath + "\\" + super.getFileName(vfFile); 162 163 File file = new File (sRealFilePath); 164 if(bOverWrite == false && file.exists()){ 165 return file.getAbsolutePath(); 166 } else { 167 return super.createWorkingFile(vfFile, file); 168 } 169 } 170 171 174 public PathStatusWrapper open(String sPath, AbstractVirtualFileSystem vfs) { 175 VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource(); 176 177 String sWorkingFilePath = createWorkingFile(vfFile); 178 179 File fFile = new File (sWorkingFilePath); 180 181 try { 182 Process proc5 = Runtime.getRuntime().exec( "rundll32 url.dll,FileProtocolHandler file:/" + fFile.toURI().getPath() ); 183 } catch (MalformedURLException e1) { 184 e1.printStackTrace(); 185 } catch (IOException e1) { 186 e1.printStackTrace(); 187 } 188 189 return new PathStatusWrapper(sWorkingFilePath, new VFSStatus()); 190 } 191 public void downloadImports(VirtualFile vfFile) { 192 downloadImports(vfFile, this.m_sEditFilePath); 193 } 194 public void downloadPreviewImports(VirtualFile vfFile) { 195 downloadImports(vfFile, this.m_sPreviewFilePath); 196 } 197 203 private void downloadImports(VirtualFile vfFile, String path) { 204 205 Document document = null; 206 try { 207 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 208 factory.setNamespaceAware(true); 209 document = factory.newDocumentBuilder().parse( new InputSource ( new InputStreamReader ( new ByteArrayInputStream (vfFile.getContent()) ) ) ); 210 } catch (SAXException e) { 211 e.printStackTrace(System.out); 212 } catch (ParserConfigurationException e) { 213 e.printStackTrace(System.out); 214 } catch (FactoryConfigurationError e) { 215 e.printStackTrace(System.out); 216 } catch (IOException e) { 217 e.printStackTrace(); 218 } 219 220 if(document!=null) { 221 Element elRoot = document.getDocumentElement(); 222 NodeList nl = elRoot.getElementsByTagNameNS(NamespaceType.XSLT.getURI(), "import"); 223 for(int i=0; i<nl.getLength(); i++) { 224 Node node = nl.item(i); 225 if(node.getNodeType()==Node.ELEMENT_NODE) { 226 Element element = (Element )node; 227 if(element.hasAttribute("href")) { 228 String sHREF = element.getAttribute("href"); 229 sHREF = sHREF.substring(0,sHREF.lastIndexOf(".")); 230 String sServerPath = ((VersionedVirtualFile)vfFile).getLiveVersionPath(); 232 if(sServerPath == null){ 233 sServerPath = vfFile.getFullPath(); 234 } 235 String sVirtualPath = this.buildNewPath(sServerPath, sHREF); 236 VirtualFile vfImport = vfFile.getVFS().getVirtualFile(sVirtualPath).getResource(); 237 if(vfImport!=null) { 238 this.createWorkingFile(vfImport,path, false); 239 this.downloadImports(vfImport,path); 240 } else { 241 } 243 } 244 } 245 } 246 nl = elRoot.getElementsByTagNameNS(NamespaceType.XSLT.getURI(), "include"); 247 for(int i=0; i<nl.getLength(); i++) { 248 Node node = nl.item(i); 249 if(node.getNodeType()==Node.ELEMENT_NODE) { 250 Element element = (Element )node; 251 if(element.hasAttribute("href")) { 252 String sHREF = element.getAttribute("href"); 253 sHREF = sHREF.substring(0,sHREF.lastIndexOf(".")); 254 255 String sVirtualPath = this.buildNewPath(vfFile.getFullPath(), sHREF); 256 VirtualFile vfImport = vfFile.getVFS().getVirtualFile(sVirtualPath).getResource(); 257 if(vfImport!=null) { 258 this.createWorkingFile(vfImport,path, false); 259 this.downloadImports(vfImport,path); 260 } else { 261 262 } 263 } 264 } 265 } 266 } 267 } 268 269 private String buildNewPath(String sBasePath, String sRelativePath) { 270 ArrayList aBasePathSegmenets = new ArrayList (); 271 ArrayList aRelativePathSegmenets = new ArrayList (); 272 273 StringTokenizer sTok = new StringTokenizer (sBasePath, "/\\"); 274 while(sTok.hasMoreTokens()) { 275 String sTemp = sTok.nextToken(); 276 if(sTok.hasMoreTokens()) { 277 aBasePathSegmenets.add(sTemp); 278 } 279 } 280 281 sTok = new StringTokenizer (sRelativePath, "/\\"); 282 while(sTok.hasMoreTokens()) { 283 String sTemp = sTok.nextToken(); 284 aRelativePathSegmenets.add(sTemp); 285 } 286 287 Iterator itor = aRelativePathSegmenets.iterator(); 288 while (itor.hasNext()) { 289 String sRelativeSegment = (String ) itor.next(); 290 if(sRelativeSegment.equals(".")) { 291 } else if(sRelativeSegment.equals("..")) { 293 aBasePathSegmenets.remove(aBasePathSegmenets.size()-1); 294 } 295 } 296 297 StringBuffer sBuff = new StringBuffer (); 298 itor = aBasePathSegmenets.iterator(); 299 while (itor.hasNext()) { 300 String sBaseSegment = (String ) itor.next(); 301 sBuff.append("/").append(sBaseSegment); 302 } 303 304 itor = aRelativePathSegmenets.iterator(); 305 while (itor.hasNext()) { 306 String sRelativeSegment = (String ) itor.next(); 307 if(!sRelativeSegment.equals(".") && !sRelativeSegment.equals("..")) { 308 sBuff.append("/").append(sRelativeSegment); 309 } 310 } 311 312 return sBuff.toString(); 313 } 314 315 318 public boolean hasResourceBeenCreated() { 319 return this.m_bResourceCreated; 320 } 321 }
| Popular Tags
|