001: /*******************************************************************************
002: * Copyright (c) 2007 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;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.jface.text.IDocument;
014:
015: /**
016: * NodeDocumentHandler
017: *
018: */
019: public abstract class NodeDocumentHandler extends DocumentHandler {
020:
021: private IDocumentNodeFactory fFactory;
022:
023: // TODO: MP: TEO: LOW: Make PluginDocumentHandler extend this
024:
025: /**
026: * @param reconciling
027: */
028: public NodeDocumentHandler(boolean reconciling,
029: IDocumentNodeFactory factory) {
030: super (reconciling);
031: fFactory = factory;
032: }
033:
034: /**
035: * @return
036: */
037: protected IDocumentNodeFactory getFactory() {
038: return fFactory;
039: }
040:
041: /* (non-Javadoc)
042: * @see org.eclipse.pde.internal.core.text.DocumentHandler#getDocument()
043: */
044: protected abstract IDocument getDocument();
045:
046: /* (non-Javadoc)
047: * @see org.eclipse.pde.internal.ui.model.plugin.DocumentHandler#getDocumentAttribute(java.lang.String, java.lang.String, org.eclipse.pde.internal.ui.model.IDocumentNode)
048: */
049: protected IDocumentAttributeNode getDocumentAttribute(String name,
050: String value, IDocumentElementNode parent) {
051: IDocumentAttributeNode attr = parent.getDocumentAttribute(name);
052: try {
053: if (attr == null) {
054: attr = fFactory.createAttribute(name, value, parent);
055: } else {
056: if (!name.equals(attr.getAttributeName()))
057: attr.setAttributeName(name);
058: if (!value.equals(attr.getAttributeValue()))
059: attr.setAttributeValue(value);
060: }
061: } catch (CoreException e) {
062: }
063: return attr;
064: }
065:
066: /**
067: * @return
068: */
069: protected abstract IDocumentElementNode getRootNode();
070:
071: /* (non-Javadoc)
072: * @see org.eclipse.pde.internal.ui.model.plugin.DocumentHandler#getDocumentNode(java.lang.String, org.eclipse.pde.internal.ui.model.IDocumentNode)
073: */
074: protected IDocumentElementNode getDocumentNode(String name,
075: IDocumentElementNode parent) {
076: IDocumentElementNode node = null;
077: if (parent == null) {
078: node = getRootNode();
079: if (node != null) {
080: node.setOffset(-1);
081: node.setLength(-1);
082: }
083: } else {
084: IDocumentElementNode[] children = parent.getChildNodes();
085: for (int i = 0; i < children.length; i++) {
086: if (children[i].getOffset() < 0) {
087: if (name.equals(children[i].getXMLTagName())) {
088: node = children[i];
089: }
090: break;
091: }
092: }
093: }
094:
095: if (node == null)
096: return fFactory.createDocumentNode(name, parent);
097:
098: IDocumentAttributeNode[] attrs = node.getNodeAttributes();
099: for (int i = 0; i < attrs.length; i++) {
100: attrs[i].setNameOffset(-1);
101: attrs[i].setNameLength(-1);
102: attrs[i].setValueOffset(-1);
103: attrs[i].setValueLength(-1);
104: }
105:
106: for (int i = 0; i < node.getChildNodes().length; i++) {
107: IDocumentElementNode child = node.getChildAt(i);
108: child.setOffset(-1);
109: child.setLength(-1);
110: }
111:
112: // clear text nodes if the user is typing on the source page
113: // they will be recreated in the characters() method
114: if (isReconciling()) {
115: node.removeTextNode();
116: node.setIsErrorNode(false);
117: }
118:
119: return node;
120: }
121:
122: /* (non-Javadoc)
123: * @see org.eclipse.pde.internal.core.text.DocumentHandler#getDocumentTextNode()
124: */
125: protected IDocumentTextNode getDocumentTextNode(String content,
126: IDocumentElementNode parent) {
127:
128: IDocumentTextNode textNode = parent.getTextNode();
129: if (textNode == null) {
130: if (content.trim().length() > 0) {
131: textNode = fFactory.createDocumentTextNode(content,
132: parent);
133: }
134: } else {
135: String newContent = textNode.getText() + content;
136: textNode.setText(newContent);
137: }
138: return textNode;
139: }
140:
141: }
|