blob: 1f8a5bd364389ffa16b2af85c48efb3661b4e5ff [file] [log] [blame]
kchong7416cdb2005-11-30 22:22:22 +00001/*******************************************************************************
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 *******************************************************************************/
csalter771f5152005-05-16 04:27:36 +000011package org.eclipse.wst.xsd.ui.internal.text;
12
csalterc7926c02006-07-18 16:19:46 +000013import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
csalter8fd5c1e2006-05-09 04:22:25 +000014import org.eclipse.wst.xsd.ui.internal.util.ModelReconcileAdapter;
csalter771f5152005-05-16 04:27:36 +000015import org.eclipse.xsd.XSDConcreteComponent;
16import org.eclipse.xsd.XSDSchema;
csalter9be70e92006-05-09 18:30:28 +000017import org.eclipse.xsd.util.XSDConstants;
csalter771f5152005-05-16 04:27:36 +000018import org.w3c.dom.Document;
19import org.w3c.dom.Element;
20import org.w3c.dom.Node;
21
csalter8fd5c1e2006-05-09 04:22:25 +000022public class XSDModelReconcileAdapter extends ModelReconcileAdapter
csalter771f5152005-05-16 04:27:36 +000023{
csalter8fd5c1e2006-05-09 04:22:25 +000024 protected XSDSchema schema;
csalter771f5152005-05-16 04:27:36 +000025
csalter8fd5c1e2006-05-09 04:22:25 +000026 public XSDModelReconcileAdapter(Document document, XSDSchema schema)
csalter771f5152005-05-16 04:27:36 +000027 {
csalter8fd5c1e2006-05-09 04:22:25 +000028 super(document);
29 this.schema = schema;
csalter771f5152005-05-16 04:27:36 +000030 }
csalter8fd5c1e2006-05-09 04:22:25 +000031
32 protected void handleNodeChanged(Node node)
csalter771f5152005-05-16 04:27:36 +000033 {
kchong38c52872006-09-06 19:41:40 +000034 // 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
csalter9be70e92006-05-09 18:30:28 +000049 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();
kchong02892442006-05-15 21:16:59 +000090 schema.getAttributeGroupDefinitions().clear();
91
92 schema.setElement(null);
csalter9be70e92006-05-09 18:30:28 +000093 }
94 }
csalterc7926c02006-07-18 16:19:46 +000095 }
kchong38c52872006-09-06 19:41:40 +000096
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
csalterc7926c02006-07-18 16:19:46 +0000135 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 }
csalter8fd5c1e2006-05-09 04:22:25 +0000157 }
csalter771f5152005-05-16 04:27:36 +0000158}