1 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 ; 20 21 public class PluginDocumentHandler extends DocumentHandler { 22 23 private PluginModelBase fModel; 24 private String fSchemaVersion; 25 protected PluginDocumentNodeFactory fFactory; 26 27 30 public PluginDocumentHandler(PluginModelBase model, boolean reconciling) { 31 super(reconciling); 32 fModel = model; 33 fFactory = (PluginDocumentNodeFactory)getModel().getPluginFactory(); 34 } 35 36 39 protected IDocument getDocument() { 40 return fModel.getDocument(); 41 } 42 43 46 public void endDocument() throws SAXException { 47 IPluginBase pluginBase = fModel.getPluginBase(); 48 try { 49 if (pluginBase != null) 50 pluginBase.setSchemaVersion(fSchemaVersion); 51 } catch (CoreException e) { 52 } 53 } 54 55 56 59 public void processingInstruction(String target, String data) throws SAXException { 60 if ("eclipse".equals(target)) { fSchemaVersion = "version=\"3.0\"".equals(data) ? "3.0" : "3.2"; } 63 } 64 65 68 public void startDocument() throws SAXException { 69 super.startDocument(); 70 fSchemaVersion = null; 71 } 72 73 protected PluginModelBase getModel() { 74 return fModel; 75 } 76 77 80 protected IDocumentNode getDocumentNode(String 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 if (isReconciling()) { 120 node.removeTextNode(); 121 node.setIsErrorNode(false); 122 } 123 124 return node; 125 } 126 127 130 protected IDocumentAttribute getDocumentAttribute(String name, String 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 |