001: /*******************************************************************************
002: * Copyright (c) 2003, 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.text.plugin;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.jface.text.IDocument;
014: import org.eclipse.pde.core.plugin.IPluginBase;
015: import org.eclipse.pde.internal.core.text.DocumentHandler;
016: import org.eclipse.pde.internal.core.text.IDocumentAttributeNode;
017: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
018: import org.eclipse.pde.internal.core.text.IDocumentTextNode;
019: import org.xml.sax.SAXException;
020:
021: public class PluginDocumentHandler extends DocumentHandler {
022:
023: private PluginModelBase fModel;
024: private String fSchemaVersion;
025: protected PluginDocumentNodeFactory fFactory;
026:
027: /**
028: * @param model
029: */
030: public PluginDocumentHandler(PluginModelBase model,
031: boolean reconciling) {
032: super (reconciling);
033: fModel = model;
034: fFactory = (PluginDocumentNodeFactory) getModel()
035: .getPluginFactory();
036: }
037:
038: /* (non-Javadoc)
039: * @see org.eclipse.pde.internal.ui.model.plugin.DocumentHandler#getDocument()
040: */
041: protected IDocument getDocument() {
042: return fModel.getDocument();
043: }
044:
045: /* (non-Javadoc)
046: * @see org.xml.sax.helpers.DefaultHandler#endDocument()
047: */
048: public void endDocument() throws SAXException {
049: IPluginBase pluginBase = fModel.getPluginBase();
050: try {
051: if (pluginBase != null)
052: pluginBase.setSchemaVersion(fSchemaVersion);
053: } catch (CoreException e) {
054: }
055: }
056:
057: /* (non-Javadoc)
058: * @see org.xml.sax.helpers.DefaultHandler#processingInstruction(java.lang.String, java.lang.String)
059: */
060: public void processingInstruction(String target, String data)
061: throws SAXException {
062: if ("eclipse".equals(target)) { //$NON-NLS-1$
063: fSchemaVersion = "version=\"3.0\"".equals(data) ? "3.0" : "3.2"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
064: }
065: }
066:
067: /* (non-Javadoc)
068: * @see org.eclipse.pde.internal.ui.model.DocumentHandler#startDocument()
069: */
070: public void startDocument() throws SAXException {
071: super .startDocument();
072: fSchemaVersion = null;
073: }
074:
075: protected PluginModelBase getModel() {
076: return fModel;
077: }
078:
079: /* (non-Javadoc)
080: * @see org.eclipse.pde.internal.ui.model.plugin.DocumentHandler#getDocumentNode(java.lang.String, org.eclipse.pde.internal.ui.model.IDocumentNode)
081: */
082: protected IDocumentElementNode getDocumentNode(String name,
083: IDocumentElementNode parent) {
084: IDocumentElementNode node = null;
085: if (parent == null) {
086: node = (IDocumentElementNode) getModel().getPluginBase();
087: if (node != null) {
088: node.setOffset(-1);
089: node.setLength(-1);
090: }
091: } else {
092: IDocumentElementNode[] children = parent.getChildNodes();
093: for (int i = 0; i < children.length; i++) {
094: if (children[i].getOffset() < 0) {
095: if (name.equals(children[i].getXMLTagName())) {
096: node = children[i];
097: }
098: break;
099: }
100: }
101: }
102:
103: if (node == null)
104: return fFactory.createDocumentNode(name, parent);
105:
106: IDocumentAttributeNode[] attrs = node.getNodeAttributes();
107: for (int i = 0; i < attrs.length; i++) {
108: attrs[i].setNameOffset(-1);
109: attrs[i].setNameLength(-1);
110: attrs[i].setValueOffset(-1);
111: attrs[i].setValueLength(-1);
112: }
113:
114: for (int i = 0; i < node.getChildNodes().length; i++) {
115: IDocumentElementNode child = node.getChildAt(i);
116: child.setOffset(-1);
117: child.setLength(-1);
118: }
119:
120: // clear text nodes if the user is typing on the source page
121: // they will be recreated in the characters() method
122: if (isReconciling()) {
123: node.removeTextNode();
124: node.setIsErrorNode(false);
125: }
126:
127: return node;
128: }
129:
130: /* (non-Javadoc)
131: * @see org.eclipse.pde.internal.ui.model.plugin.DocumentHandler#getDocumentAttribute(java.lang.String, java.lang.String, org.eclipse.pde.internal.ui.model.IDocumentNode)
132: */
133: protected IDocumentAttributeNode getDocumentAttribute(String name,
134: String value, IDocumentElementNode parent) {
135: IDocumentAttributeNode attr = parent.getDocumentAttribute(name);
136: try {
137: if (attr == null) {
138: attr = fFactory.createAttribute(name, value, parent);
139: } else {
140: if (!name.equals(attr.getAttributeName()))
141: attr.setAttributeName(name);
142: if (!value.equals(attr.getAttributeValue()))
143: attr.setAttributeValue(value);
144: }
145: } catch (CoreException e) {
146: }
147: return attr;
148: }
149:
150: /* (non-Javadoc)
151: * @see org.eclipse.pde.internal.core.text.DocumentHandler#getDocumentTextNode()
152: */
153: protected IDocumentTextNode getDocumentTextNode(String content,
154: IDocumentElementNode parent) {
155:
156: IDocumentTextNode textNode = parent.getTextNode();
157: if (textNode == null) {
158: if (content.trim().length() > 0) {
159: textNode = fFactory.createDocumentTextNode(content,
160: parent);
161: }
162: } else {
163: String newContent = textNode.getText() + content;
164: textNode.setText(newContent);
165: }
166: return textNode;
167: }
168:
169: }
|