KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > target > TargetModel


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.target;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15
16 import javax.xml.parsers.SAXParser JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.pde.core.IModelChangedEvent;
20 import org.eclipse.pde.core.ModelChangedEvent;
21 import org.eclipse.pde.internal.core.AbstractModel;
22 import org.eclipse.pde.internal.core.PDECore;
23 import org.eclipse.pde.internal.core.XMLDefaultHandler;
24 import org.eclipse.pde.internal.core.itarget.ITarget;
25 import org.eclipse.pde.internal.core.itarget.ITargetModel;
26 import org.eclipse.pde.internal.core.itarget.ITargetModelFactory;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29
30 public class TargetModel extends AbstractModel implements ITargetModel {
31
32     private static final long serialVersionUID = 1L;
33     
34     private ITargetModelFactory fFactory;
35     private ITarget fTarget;
36
37     protected void updateTimeStamp() {
38     }
39
40     public ITarget getTarget() {
41         if (fTarget == null)
42             fTarget = getFactory().createTarget();
43         return fTarget;
44     }
45
46     public ITargetModelFactory getFactory() {
47         if (fFactory == null)
48             fFactory = new TargetModelFactory(this);
49         return fFactory;
50     }
51
52     public String JavaDoc getInstallLocation() {
53         return null;
54     }
55
56     public boolean isInSync() {
57         return true;
58     }
59
60     public void load() throws CoreException {
61     }
62
63     public void load(InputStream JavaDoc stream, boolean outOfSync) throws CoreException {
64         try {
65             SAXParser JavaDoc parser = getSaxParser();
66             XMLDefaultHandler handler = new XMLDefaultHandler();
67             parser.parse(stream, handler);
68             if (handler.isPrepared()) {
69                 processDocument(handler.getDocument());
70                 setLoaded(true);
71             }
72         } catch (Exception JavaDoc e) {
73             PDECore.logException(e);
74         } finally {
75             try {
76                 if (stream != null)
77                     stream.close();
78             } catch (IOException JavaDoc e) {
79             }
80         }
81     }
82
83     public void reload(InputStream JavaDoc source, boolean outOfSync) throws CoreException {
84         load(source, outOfSync);
85         fireModelChanged(
86                 new ModelChangedEvent(this,
87                     IModelChangedEvent.WORLD_CHANGED,
88                     new Object JavaDoc[] { fTarget },
89                     null));
90     }
91
92     public boolean isEditable() {
93         return false;
94     }
95     
96     private void processDocument(Document JavaDoc doc) {
97         Node JavaDoc rootNode = doc.getDocumentElement();
98         if (fTarget == null) {
99             fTarget = getFactory().createTarget();
100         } else {
101             fTarget.reset();
102         }
103         fTarget.parse(rootNode);
104     }
105
106
107 }
108
Popular Tags