1 11 package org.eclipse.pde.internal.core.feature; 12 13 import java.io.InputStream ; 14 15 import javax.xml.parsers.SAXParser ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.pde.core.IModelChangedEvent; 19 import org.eclipse.pde.core.ModelChangedEvent; 20 import org.eclipse.pde.internal.core.AbstractNLModel; 21 import org.eclipse.pde.internal.core.PDECore; 22 import org.eclipse.pde.internal.core.XMLCopyrightHandler; 23 import org.eclipse.pde.internal.core.XMLDefaultHandler; 24 import org.eclipse.pde.internal.core.ifeature.IFeature; 25 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 26 import org.eclipse.pde.internal.core.ifeature.IFeatureModelFactory; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Node ; 29 import org.xml.sax.SAXException ; 30 31 public abstract class AbstractFeatureModel 32 extends AbstractNLModel 33 implements IFeatureModel { 34 protected transient Feature feature; 35 private transient IFeatureModelFactory factory; 36 private boolean enabled = true; 37 38 public AbstractFeatureModel() { 39 super(); 40 } 41 public IFeature getFeature() { 42 if (feature == null) { 43 Feature f = new Feature(); 44 f.model = this; 45 this.feature = f; 46 } 47 return feature; 48 } 49 public IFeatureModelFactory getFactory() { 50 if (factory == null) 51 factory = new FeatureFactory(this); 52 return factory; 53 } 54 public String getInstallLocation() { 55 return null; 56 } 57 public boolean isEditable() { 58 return true; 59 } 60 public boolean isEnabled() { 61 return enabled; 62 } 63 public void load(InputStream stream, boolean outOfSync) throws CoreException { 64 try { 65 SAXParser parser = getSaxParser(); 66 XMLDefaultHandler handler = new XMLDefaultHandler(); 67 XMLCopyrightHandler chandler = new XMLCopyrightHandler(handler); 68 parser.setProperty("http://xml.org/sax/properties/lexical-handler", chandler); parser.parse(stream, handler); 70 if (handler.isPrepared()) { 71 processDocument(handler.getDocument()); 72 String copyright = chandler.getCopyright(); 73 if (copyright != null) 74 feature.setCopyright(copyright); 75 setLoaded(true); 76 if (!outOfSync) 77 updateTimeStamp(); 78 } 79 } catch (SAXException e) { 80 } catch (Exception e) { 81 PDECore.logException(e); 82 } 83 } 84 85 public boolean isValid() { 86 if (!isLoaded()) return false; 87 IFeature feature = getFeature(); 88 return feature!=null && feature.isValid(); 89 } 90 91 private void processDocument(Document doc) { 92 Node rootNode = doc.getDocumentElement(); 93 if (feature == null) { 94 feature = new Feature(); 95 feature.model = this; 96 } else { 97 feature.reset(); 98 } 99 feature.parse(rootNode); 100 } 101 public void reload(InputStream stream, boolean outOfSync) 102 throws CoreException { 103 if (feature != null) 104 feature.reset(); 105 load(stream, outOfSync); 106 fireModelChanged( 107 new ModelChangedEvent(this, 108 IModelChangedEvent.WORLD_CHANGED, 109 new Object [] { feature }, 110 null)); 111 } 112 public void setEnabled(boolean enabled) { 113 this.enabled = enabled; 114 } 115 } 116 | Popular Tags |