1 11 package org.eclipse.pde.internal.core.site; 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.AbstractModel; 21 import org.eclipse.pde.internal.core.PDECore; 22 import org.eclipse.pde.internal.core.XMLDefaultHandler; 23 import org.eclipse.pde.internal.core.isite.ISite; 24 import org.eclipse.pde.internal.core.isite.ISiteModel; 25 import org.eclipse.pde.internal.core.isite.ISiteModelFactory; 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.Node ; 28 29 public abstract class AbstractSiteModel 30 extends AbstractModel 31 implements ISiteModel { 32 protected transient Site site; 33 private transient ISiteModelFactory factory; 34 private boolean enabled = true; 35 36 public AbstractSiteModel() { 37 super(); 38 } 39 public ISite getSite() { 40 if (site == null) { 41 Site s = new Site(); 42 s.model = this; 43 this.site = s; 44 } 45 return site; 46 } 47 public ISiteModelFactory getFactory() { 48 if (factory == null) 49 factory = new SiteModelFactory(this); 50 return factory; 51 } 52 public String getInstallLocation() { 53 return null; 54 } 55 public boolean isEditable() { 56 return true; 57 } 58 public boolean isEnabled() { 59 return enabled; 60 } 61 public boolean isValid() { 62 if (!isLoaded() || site==null) return false; 63 return site.isValid(); 64 } 65 public void load(InputStream stream, boolean outOfSync) throws CoreException { 66 try { 67 SAXParser parser = getSaxParser(); 68 XMLDefaultHandler handler = new XMLDefaultHandler(); 69 parser.parse(stream, handler); 70 if (handler.isPrepared()) { 71 processDocument(handler.getDocument()); 72 setLoaded(true); 73 if (!outOfSync) 74 updateTimeStamp(); 75 } 76 } catch (Exception e) { 77 PDECore.logException(e); 78 } 79 } 80 81 private void processDocument(Document doc) { 82 Node rootNode = doc.getDocumentElement(); 83 if (site == null) { 84 site = new Site(); 85 site.model = this; 86 } else { 87 site.reset(); 88 } 89 site.parse(rootNode); 90 } 91 public void reload(InputStream stream, boolean outOfSync) 92 throws CoreException { 93 if (site != null) 94 site.reset(); 95 load(stream, outOfSync); 96 fireModelChanged( 97 new ModelChangedEvent(this, 98 IModelChangedEvent.WORLD_CHANGED, 99 new Object [] { site }, 100 null)); 101 } 102 public void setEnabled(boolean enabled) { 103 this.enabled = enabled; 104 } 105 } 106 | Popular Tags |