001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.product;
011:
012: import java.io.IOException;
013: import java.io.InputStream;
014:
015: import javax.xml.parsers.SAXParser;
016:
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.pde.core.IModelChangedEvent;
019: import org.eclipse.pde.core.ModelChangedEvent;
020: import org.eclipse.pde.internal.core.AbstractModel;
021: import org.eclipse.pde.internal.core.PDECore;
022: import org.eclipse.pde.internal.core.XMLDefaultHandler;
023: import org.eclipse.pde.internal.core.iproduct.IProduct;
024: import org.eclipse.pde.internal.core.iproduct.IProductModel;
025: import org.eclipse.pde.internal.core.iproduct.IProductModelFactory;
026: import org.w3c.dom.Document;
027: import org.w3c.dom.Node;
028:
029: public class ProductModel extends AbstractModel implements
030: IProductModel {
031:
032: private static final long serialVersionUID = 1L;
033:
034: private IProductModelFactory fFactory;
035: private IProduct fProduct;
036:
037: /* (non-Javadoc)
038: * @see org.eclipse.pde.internal.core.AbstractModel#updateTimeStamp()
039: */
040: protected void updateTimeStamp() {
041: }
042:
043: /* (non-Javadoc)
044: * @see org.eclipse.pde.internal.core.iproduct.IProductModel#getProduct()
045: */
046: public IProduct getProduct() {
047: if (fProduct == null)
048: fProduct = getFactory().createProduct();
049: return fProduct;
050: }
051:
052: /* (non-Javadoc)
053: * @see org.eclipse.pde.internal.core.iproduct.IProductModel#getFactory()
054: */
055: public IProductModelFactory getFactory() {
056: if (fFactory == null)
057: fFactory = new ProductModelFactory(this );
058: return fFactory;
059: }
060:
061: /* (non-Javadoc)
062: * @see org.eclipse.pde.internal.core.iproduct.IProductModel#getInstallLocation()
063: */
064: public String getInstallLocation() {
065: return null;
066: }
067:
068: /* (non-Javadoc)
069: * @see org.eclipse.pde.core.IModel#isInSync()
070: */
071: public boolean isInSync() {
072: return true;
073: }
074:
075: /* (non-Javadoc)
076: * @see org.eclipse.pde.core.IModel#load()
077: */
078: public void load() throws CoreException {
079: }
080:
081: /* (non-Javadoc)
082: * @see org.eclipse.pde.core.IModel#load(java.io.InputStream, boolean)
083: */
084: public void load(InputStream stream, boolean outOfSync)
085: throws CoreException {
086: try {
087: SAXParser parser = getSaxParser();
088: XMLDefaultHandler handler = new XMLDefaultHandler();
089: parser.parse(stream, handler);
090: if (handler.isPrepared()) {
091: processDocument(handler.getDocument());
092: setLoaded(true);
093: }
094: } catch (Exception e) {
095: PDECore.logException(e);
096: } finally {
097: try {
098: if (stream != null)
099: stream.close();
100: } catch (IOException e) {
101: }
102: }
103: }
104:
105: /* (non-Javadoc)
106: * @see org.eclipse.pde.core.IModel#reload(java.io.InputStream, boolean)
107: */
108: public void reload(InputStream source, boolean outOfSync)
109: throws CoreException {
110: load(source, outOfSync);
111: fireModelChanged(new ModelChangedEvent(this ,
112: IModelChangedEvent.WORLD_CHANGED,
113: new Object[] { fProduct }, null));
114: }
115:
116: /* (non-Javadoc)
117: * @see org.eclipse.pde.core.IBaseModel#isEditable()
118: */
119: public boolean isEditable() {
120: return false;
121: }
122:
123: private void processDocument(Document doc) {
124: Node rootNode = doc.getDocumentElement();
125: if (fProduct == null) {
126: fProduct = getFactory().createProduct();
127: } else {
128: fProduct.reset();
129: }
130: fProduct.parse(rootNode);
131: }
132: }
|