kchong | 7416cdb | 2005-11-30 22:22:22 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2004, 2005 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 | *******************************************************************************/ |
csalter | 771f515 | 2005-05-16 04:27:36 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.xsd.ui.internal.text; |
| 12 | |
csalter | c7926c0 | 2006-07-18 16:19:46 +0000 | [diff] [blame] | 13 | import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
csalter | 8fd5c1e | 2006-05-09 04:22:25 +0000 | [diff] [blame] | 14 | import org.eclipse.wst.xsd.ui.internal.util.ModelReconcileAdapter; |
csalter | 771f515 | 2005-05-16 04:27:36 +0000 | [diff] [blame] | 15 | import org.eclipse.xsd.XSDConcreteComponent; |
| 16 | import org.eclipse.xsd.XSDSchema; |
csalter | 9be70e9 | 2006-05-09 18:30:28 +0000 | [diff] [blame] | 17 | import org.eclipse.xsd.util.XSDConstants; |
csalter | 771f515 | 2005-05-16 04:27:36 +0000 | [diff] [blame] | 18 | import org.w3c.dom.Document; |
| 19 | import org.w3c.dom.Element; |
| 20 | import org.w3c.dom.Node; |
| 21 | |
csalter | 8fd5c1e | 2006-05-09 04:22:25 +0000 | [diff] [blame] | 22 | public class XSDModelReconcileAdapter extends ModelReconcileAdapter |
csalter | 771f515 | 2005-05-16 04:27:36 +0000 | [diff] [blame] | 23 | { |
csalter | 8fd5c1e | 2006-05-09 04:22:25 +0000 | [diff] [blame] | 24 | protected XSDSchema schema; |
csalter | 771f515 | 2005-05-16 04:27:36 +0000 | [diff] [blame] | 25 | |
csalter | 8fd5c1e | 2006-05-09 04:22:25 +0000 | [diff] [blame] | 26 | public XSDModelReconcileAdapter(Document document, XSDSchema schema) |
csalter | 771f515 | 2005-05-16 04:27:36 +0000 | [diff] [blame] | 27 | { |
csalter | 8fd5c1e | 2006-05-09 04:22:25 +0000 | [diff] [blame] | 28 | super(document); |
| 29 | this.schema = schema; |
csalter | 771f515 | 2005-05-16 04:27:36 +0000 | [diff] [blame] | 30 | } |
csalter | 8fd5c1e | 2006-05-09 04:22:25 +0000 | [diff] [blame] | 31 | |
| 32 | protected void handleNodeChanged(Node node) |
csalter | 771f515 | 2005-05-16 04:27:36 +0000 | [diff] [blame] | 33 | { |
kchong | 38c5287 | 2006-09-06 19:41:40 +0000 | [diff] [blame] | 34 | // Make sure that the node is in the XSD Schema namespace. We don't need to |
| 35 | // reconcile the model for other nodes like text or nodes in other namespaces |
| 36 | // like the ones normally occurring in annotations. |
| 37 | |
| 38 | try |
| 39 | { |
| 40 | if (!shouldReconcileModelFor(node)) |
| 41 | { |
| 42 | return; |
| 43 | } |
| 44 | } |
| 45 | catch (Exception e) |
| 46 | { |
| 47 | } |
| 48 | |
csalter | 9be70e9 | 2006-05-09 18:30:28 +0000 | [diff] [blame] | 49 | if (node instanceof Element) |
| 50 | { |
| 51 | XSDConcreteComponent concreteComponent = schema.getCorrespondingComponent(node); |
| 52 | concreteComponent.elementChanged((Element)node); |
| 53 | } |
| 54 | else if (node instanceof Document) |
| 55 | { |
| 56 | // The document changed so we may need to fix up the |
| 57 | // definition's root element |
| 58 | Document document = (Document)node; |
| 59 | Element schemaElement = document.getDocumentElement(); |
| 60 | if (schemaElement != null && schemaElement != schema.getElement()) |
| 61 | { |
| 62 | // here we handle the case where a new 'schema' element was added |
| 63 | //(e.g. the file was totally blank and then we type in the root element) |
| 64 | // |
| 65 | if (schemaElement.getLocalName().equals(XSDConstants.SCHEMA_ELEMENT_TAG)) |
| 66 | { |
| 67 | //System.out.println("****** Setting new schema"); |
| 68 | schema.setElement(schemaElement); |
| 69 | } |
| 70 | } |
| 71 | else if (schemaElement != null) |
| 72 | { |
| 73 | // handle the case where the definition element's content has changed |
| 74 | // TODO (cs) do we really need to handle this case? |
| 75 | schema.elementChanged(schemaElement); |
| 76 | } |
| 77 | else if (schemaElement == null) |
| 78 | { |
| 79 | // if there's no root element clear out the schema |
| 80 | // |
| 81 | schema.getContents().clear(); |
| 82 | // TODO (cs) I'm not sure why the above isn't enough |
| 83 | // to clear out all of the component lists |
| 84 | // for now I've just added a few more lines to do additional clearing |
| 85 | // |
| 86 | schema.getElementDeclarations().clear(); |
| 87 | schema.getTypeDefinitions().clear(); |
| 88 | schema.getAttributeDeclarations().clear(); |
| 89 | schema.getModelGroupDefinitions().clear(); |
kchong | 0289244 | 2006-05-15 21:16:59 +0000 | [diff] [blame] | 90 | schema.getAttributeGroupDefinitions().clear(); |
| 91 | |
| 92 | schema.setElement(null); |
csalter | 9be70e9 | 2006-05-09 18:30:28 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
csalter | c7926c0 | 2006-07-18 16:19:46 +0000 | [diff] [blame] | 95 | } |
kchong | 38c5287 | 2006-09-06 19:41:40 +0000 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Checks if a change to the given node should trigger a model update. |
| 99 | * |
| 100 | * @param changedNode |
| 101 | * the DOM node to test. |
| 102 | * @return true if the change to the given node should trigger a model update. |
| 103 | */ |
| 104 | protected boolean shouldReconcileModelFor(Node changedNode) |
| 105 | { |
| 106 | // No need to reconcile if the node is in a different namespace as it is the |
| 107 | // case for nodes deeply nested in appinfo or documentation elements. |
| 108 | |
| 109 | String nodeNamespace = changedNode.getNamespaceURI(); |
| 110 | String schemaNamespace = schema.getSchemaForSchemaNamespace(); |
| 111 | |
| 112 | if (!schemaNamespace.equals(nodeNamespace)) |
| 113 | { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | // When a child node is added to an appinfo or documentation element |
| 118 | // No need to reconcile if the node is the first child of appinfo or documentation |
| 119 | // elements. |
| 120 | |
| 121 | Node parentNode = changedNode.getParentNode(); |
| 122 | |
| 123 | if (parentNode != null) |
| 124 | { |
| 125 | String nodeName = changedNode.getLocalName(); |
| 126 | |
| 127 | if (XSDConstants.APPINFO_ELEMENT_TAG.equals(nodeName) || XSDConstants.DOCUMENTATION_ELEMENT_TAG.equals(nodeName)) |
| 128 | { |
| 129 | return false; |
| 130 | } |
| 131 | } |
| 132 | return true; |
| 133 | } |
| 134 | |
csalter | c7926c0 | 2006-07-18 16:19:46 +0000 | [diff] [blame] | 135 | public void modelDirtyStateChanged(IStructuredModel model, boolean isDirty) |
| 136 | { |
| 137 | if (!isDirty) |
| 138 | { |
| 139 | // TODO need a way to tell the views to stop refreshing (temporarily) |
| 140 | // |
| 141 | /* |
| 142 | schema.reset(); |
| 143 | |
| 144 | // For some reason type references don't get fixed up when an import is removed |
| 145 | // even if we call schema.reset() explictly. To work around this we iterate thru |
| 146 | // the type references and recompute them incase a schema did infact change |
| 147 | // |
| 148 | for (Iterator i = schema.getElementDeclarations().iterator(); i.hasNext(); ) |
| 149 | { |
| 150 | XSDElementDeclarationImpl ed = (XSDElementDeclarationImpl)i.next(); |
| 151 | //ed.elementAttributesChanged(ed.getElement()); |
| 152 | XSDTypeDefinition td = ed.getTypeDefinition(); |
| 153 | td = ed.resolveTypeDefinition(td.getSchema().getTargetNamespace(), td.getName()); |
| 154 | ed.setTypeDefinition(td); |
| 155 | }*/ |
| 156 | } |
csalter | 8fd5c1e | 2006-05-09 04:22:25 +0000 | [diff] [blame] | 157 | } |
csalter | 771f515 | 2005-05-16 04:27:36 +0000 | [diff] [blame] | 158 | } |