📄 xpdlhandler.java
字号:
package org.enhydra.jawe.base.xpdlhandler;import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;import java.nio.channels.FileLock;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.enhydra.jawe.JaWEComponent;import org.enhydra.jawe.ResourceManager;import org.enhydra.shark.xpdl.XMLInterface;import org.enhydra.shark.xpdl.XMLInterfaceForJDK13;import org.enhydra.shark.xpdl.XMLUtil;import org.enhydra.shark.xpdl.elements.ExternalPackage;import org.enhydra.shark.xpdl.elements.Package;/** * Class which purpose is to provide methods which are * used by classes that represents program apstraction of * XML elements. These methods offers support for reading or * writting an XML document and for generating the tooltips for * for the classes that needs it. */public class XPDLHandler extends XMLInterfaceForJDK13 { protected XPDLHandlerSettings settings; private Map fileLocks=new HashMap(); private Map rndAccessFiles=new HashMap(); public XPDLHandler() { settings = new XPDLHandlerSettings(); settings.init((JaWEComponent) null); } public XPDLHandler(XPDLHandlerSettings settings) throws Exception { this.settings = settings; this.settings.init((JaWEComponent) null); } public Package getMainPackage () { return (Package)xmlFileToPackage.get(mainPackageReference); } public String getMainPackageId () { Package mainPackage=getMainPackage(); if (mainPackage!=null) { return mainPackage.getId(); } return null; } public String getMainPackageFilename () { return mainPackageReference; } /** * This method has to be called when new package is created. * @param pkg */ public void registerPackage (Package pkg) { ArrayList l=(ArrayList)idToPackages.get(pkg.getId()); if (l==null) { l=new ArrayList(); } l.add(pkg); idToPackages.put(pkg.getId(),l); } /** * This method has to be called from the newly created package after its * Id is entered. * @param pkg */ public void changePackageId (Package pkg,String oldId,String newId) { ArrayList l=(ArrayList)idToPackages.remove(oldId);// if (l!=null) {//// } l=(ArrayList)idToPackages.get(newId); if (l==null) { l=new ArrayList(); } l.add(pkg); idToPackages.put(newId,l); } /** * This method is called when first saving new package, or when saving it with * a different name. * @param filename * @param pkg */ public void registerPackageFilename (String filename,Package pkg) { String uToRem=getAbsoluteFilePath(pkg); boolean isMainPackage=false; if (uToRem!=null) { if (uToRem.equals(mainPackageReference)) { isMainPackage=true; } xmlFileToPackage.remove(uToRem); } // release lock try { FileLock fl=(FileLock)fileLocks.remove(pkg); fl.release(); } catch (Exception ex) {} // close the file stream try { RandomAccessFile raf=(RandomAccessFile)rndAccessFiles.remove(pkg); raf.close(); } catch (Exception ex) {} String cp=XMLUtil.getCanonicalPath(filename, false); xmlFileToPackage.put(cp,pkg); if (isMainPackage || mainPackageReference==null) { mainPackageReference=cp; } File f=new File(cp); try { RandomAccessFile raf=new RandomAccessFile(f,"rw"); rndAccessFiles.put(pkg,raf); if (settings.isFileLockingEnabled()) { FileLock fl=raf.getChannel().tryLock(); if (fl!=null) { fileLocks.put(pkg,fl); // this should never happend } else { System.out.println("Can't lock"); } } // this happens when using jdk1.4.0 under Linux, where // the locking is not supported } catch (IOException ex) { } catch (Exception ex) {} // register parent directory with the package try { packageToParentDirectory.put(pkg,f.getParentFile().getCanonicalPath()); } catch (Exception ex) { packageToParentDirectory.put(pkg,f.getParentFile().getAbsolutePath()); } } public RandomAccessFile getRaf (Package pkg){ return (RandomAccessFile)rndAccessFiles.get(pkg); } // In this implementation, if handleExternalPackages is set to false, it is assumed // that JaWE is opening transient Package public Package openPackage (String pkgReference, boolean handleExternalPackages) { parsingErrorMessages.clear(); if (mainPackageReference==null && handleExternalPackages) { mainPackageReference=pkgReference; } // this method opens the package. It also opens all of it's external packages // if handleExternalPackages is set to true Package pkg=openPackageRecursively(pkgReference,handleExternalPackages); if (pkg!=null) { pkg.setTransient(!handleExternalPackages); System.setProperty("user.dir",getParentDirectory(pkg)); } return pkg; } public void printDebug () { super.printDebug(); System.out.println("fileLocks="+fileLocks); System.out.println("rndAccessFiles="+rndAccessFiles); System.out.println("main package reference="+mainPackageReference); } // Recursive implementation protected Package openPackageRecursively (String pkgReference, boolean handleExternalPackages) { Package pkg=null; File f=null; String oldP=pkgReference; String baseDirectory=null; pkgReference=XMLUtil.getCanonicalPath(pkgReference,"",false); if (pkgReference==null) { Set fem=new HashSet(); fem.add("File does not exist"); parsingErrorMessages.put(oldP,fem); return null; } f=new File(pkgReference); try { baseDirectory=f.getParentFile().getCanonicalPath(); } catch (Exception ex) { baseDirectory=f.getParentFile().getAbsolutePath(); } if (xmlFileToPackage.containsKey(pkgReference)) { return getPackageByFilename(pkgReference); } pkg=parseDocument(pkgReference,true); try { // trying to open main package file as 'rw' // and to lock it exclusivly if (oldP.equals(mainPackageReference)) { RandomAccessFile raf=null; try { raf=new RandomAccessFile(f,"rw"); } catch (Exception ex) { raf=new RandomAccessFile(f,"r"); } rndAccessFiles.put(pkg,raf); if (settings.isFileLockingEnabled()) { FileLock fl=raf.getChannel().tryLock(); // this happens if the main package is not already locked if (fl!=null) { fileLocks.put(pkg,fl); // this happens if the file is already opened as 'rw' and locked // exclusivly, or if it is opened as 'r' and locked as shared } else { Set errorMessages = new HashSet(); errorMessages.add(ResourceManager.getLanguageDependentString("ErrorTheFileIsLocked")); parsingErrorMessages.put(pkgReference,errorMessages); return null; } } // trying to open external package file as 'rw' // and to lock it exclusivly } else { RandomAccessFile raf=null; try { raf=new RandomAccessFile(f,"rw"); } catch (Exception ex) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -