KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > text > plugin > PluginDocumentHandler


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.text.plugin;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.pde.core.plugin.IPluginBase;
16 import org.eclipse.pde.internal.core.text.DocumentHandler;
17 import org.eclipse.pde.internal.core.text.IDocumentAttribute;
18 import org.eclipse.pde.internal.core.text.IDocumentNode;
19 import org.xml.sax.SAXException JavaDoc;
20
21 public class PluginDocumentHandler extends DocumentHandler {
22     
23     private PluginModelBase fModel;
24     private String JavaDoc fSchemaVersion;
25     protected PluginDocumentNodeFactory fFactory;
26
27     /**
28      * @param model
29      */

30     public PluginDocumentHandler(PluginModelBase model, boolean reconciling) {
31         super(reconciling);
32         fModel = model;
33         fFactory = (PluginDocumentNodeFactory)getModel().getPluginFactory();
34     }
35     
36     /* (non-Javadoc)
37      * @see org.eclipse.pde.internal.ui.model.plugin.DocumentHandler#getDocument()
38      */

39     protected IDocument getDocument() {
40         return fModel.getDocument();
41     }
42     
43     /* (non-Javadoc)
44      * @see org.xml.sax.helpers.DefaultHandler#endDocument()
45      */

46     public void endDocument() throws SAXException JavaDoc {
47         IPluginBase pluginBase = fModel.getPluginBase();
48         try {
49             if (pluginBase != null)
50                 pluginBase.setSchemaVersion(fSchemaVersion);
51         } catch (CoreException e) {
52         }
53     }
54     
55
56     /* (non-Javadoc)
57      * @see org.xml.sax.helpers.DefaultHandler#processingInstruction(java.lang.String, java.lang.String)
58      */

59     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
60         if ("eclipse".equals(target)) { //$NON-NLS-1$
61
fSchemaVersion = "version=\"3.0\"".equals(data) ? "3.0" : "3.2"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
62
}
63     }
64     
65     /* (non-Javadoc)
66      * @see org.eclipse.pde.internal.ui.model.DocumentHandler#startDocument()
67      */

68     public void startDocument() throws SAXException JavaDoc {
69         super.startDocument();
70         fSchemaVersion = null;
71     }
72     
73     protected PluginModelBase getModel() {
74         return fModel;
75     }
76     
77     /* (non-Javadoc)
78      * @see org.eclipse.pde.internal.ui.model.plugin.DocumentHandler#getDocumentNode(java.lang.String, org.eclipse.pde.internal.ui.model.IDocumentNode)
79      */

80     protected IDocumentNode getDocumentNode(String JavaDoc name, IDocumentNode parent) {
81         IDocumentNode node = null;
82         if (parent == null) {
83             node = (IDocumentNode)getModel().getPluginBase();
84             if (node != null) {
85                 node.setOffset(-1);
86                 node.setLength(-1);
87             }
88         } else {
89             IDocumentNode[] children = parent.getChildNodes();
90             for (int i = 0; i < children.length; i++) {
91                 if (children[i].getOffset() < 0) {
92                     if (name.equals(children[i].getXMLTagName())) {
93                         node = children[i];
94                     }
95                     break;
96                 }
97             }
98         }
99         
100         if (node == null)
101             return fFactory.createDocumentNode(name, parent);
102         
103         IDocumentAttribute[] attrs = node.getNodeAttributes();
104         for (int i = 0; i < attrs.length; i++) {
105             attrs[i].setNameOffset(-1);
106             attrs[i].setNameLength(-1);
107             attrs[i].setValueOffset(-1);
108             attrs[i].setValueLength(-1);
109         }
110         
111         for (int i = 0; i < node.getChildNodes().length; i++) {
112             IDocumentNode child = node.getChildAt(i);
113             child.setOffset(-1);
114             child.setLength(-1);
115         }
116         
117         // clear text nodes if the user is typing on the source page
118
// they will be recreated in the characters() method
119
if (isReconciling()) {
120             node.removeTextNode();
121             node.setIsErrorNode(false);
122         }
123         
124         return node;
125     }
126     
127     /* (non-Javadoc)
128      * @see org.eclipse.pde.internal.ui.model.plugin.DocumentHandler#getDocumentAttribute(java.lang.String, java.lang.String, org.eclipse.pde.internal.ui.model.IDocumentNode)
129      */

130     protected IDocumentAttribute getDocumentAttribute(String JavaDoc name, String JavaDoc value, IDocumentNode parent) {
131         IDocumentAttribute attr = parent.getDocumentAttribute(name);
132         try {
133             if (attr == null) {
134                 attr = fFactory.createAttribute(name, value, parent);
135             } else {
136                 if (!name.equals(attr.getAttributeName()))
137                     attr.setAttributeName(name);
138                 if (!value.equals(attr.getAttributeValue()))
139                     attr.setAttributeValue(value);
140             }
141         } catch (CoreException e) {
142         }
143         return attr;
144     }
145     
146 }
147
Popular Tags