Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9e20be69cc11f6ba23557c8c2c3026b7fd305706 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*******************************************************************************
 * Copyright (c) 2002, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.wst.xml.core.internal.Logger;
import org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMDocument;
import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNode;
import org.eclipse.wst.xml.core.internal.contentmodel.modelquery.CMDocumentManager;
import org.eclipse.wst.xml.core.internal.contentmodel.modelquery.CMDocumentReferenceProvider;
import org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache;
import org.eclipse.wst.xml.core.internal.contentmodel.util.DOMNamespaceHelper;
import org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceInfo;
import org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceTable;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


/**
 *
 */
public abstract class XMLAssociationProvider extends BaseAssociationProvider implements CMDocumentReferenceProvider
{              
  protected CMDocumentCache cmDocumentCache; 
  protected CMDocumentManagerImpl documentManager;

  public XMLAssociationProvider(CMDocumentCache cmDocumentCache)
  {
    this.cmDocumentCache = cmDocumentCache; 
    documentManager = new CMDocumentManagerImpl(cmDocumentCache, this);
  }                              

  public CMDocumentManager getCMDocumentManager()
  {
    return documentManager;
  }
 
      
  public static String[] getDoctypeInfo(Document document)
  {   
    String[] result = null;
    DocumentType doctype = document.getDoctype();

    // defect 206833 ... here we test for DTDs that are declared inline
    // since we currently have no way of making use of inline DTDs we ingore them
    // so that the implict DTD (if any) can be used
    if (doctype != null && (doctype.getPublicId() != null || doctype.getSystemId() != null))
    {
      result = new String[2];
      result[0] = doctype.getPublicId();
      result[1] = doctype.getSystemId();
    }   
    else if (getImplictDoctype(document) != null)
    {
      result = getImplictDoctype(document);
    }                  
    return result;
  }   


  protected static String[] getImplictDoctype(Document document)
  { 
    String[] result = null;
    /*
    DOMExtension domExtension = DOMExtensionProviderRegistry.getInstance().getDOMExtension(document);
    if (domExtension != null)
    {
      result = domExtension.getImplicitDoctype();
    }*/
    return result;
  }

  public CMDocument getCorrespondingCMDocument(Node node)
  {        
    return getCorrespondingCMDocument(node, true);
  }

  protected CMDocument getCorrespondingCMDocument(Node node, boolean getDocumentFromCMNode)
  {            
    CMDocument result = null;
    try
    {
      Document document = node.getNodeType() == Node.DOCUMENT_NODE ? (Document)node : node.getOwnerDocument();
   
      String[] doctypeInfo = getDoctypeInfo(document);

      if (doctypeInfo != null)
      {
        result = getCMDocument(doctypeInfo[0], doctypeInfo[1], "DTD"); //$NON-NLS-1$
      }                                             
      // defect 211236 ... in some cases calling this method can result in a cycle
      // we use the getDocumentFromCMNode as a flag to avoid this 
      // TODO... see if there is a way to re-organize to avoid the need for this flag
      else if (getDocumentFromCMNode)
      {
        CMNode cmNode = getCMNode(node);
        if (cmNode != null)       
        {                
          // todo... add a getCMDocument() methods to CMNode
          // for now use the getProperty interface
          result = (CMDocument)cmNode.getProperty("CMDocument"); //$NON-NLS-1$
        }
      }
    }
    catch (Exception e)
    {
      Logger.logException("exception locating CMDocument for " + node, e); //$NON-NLS-1$
    }
    return result;
  }    

      
  public CMDocument getCMDocument(Element element, String uri)
  {
    CMDocument result = null;
    NamespaceTable namespaceTable = new NamespaceTable(element.getOwnerDocument());
    namespaceTable.addElementLineage(element);
    NamespaceInfo namespaceInfo = namespaceTable.getNamespaceInfoForURI(uri);
    if (namespaceInfo != null)
    {
      result = getCMDocument(namespaceInfo.uri, namespaceInfo.locationHint, "XSD"); //$NON-NLS-1$
    }
    return result;
  }         
  
                          
  public CMDocument getCMDocument(String publicId, String systemId, String type)
  {                   
    //String resolvedGrammarURI = resolveGrammarURI(document, publicId, systemId);
    return documentManager.getCMDocument(publicId, systemId, type);  
  }

  //public CMDocument getCMDocument(Document document, String publicId, String systemId)
  //{                   
  //  //String resolvedGrammarURI = resolveGrammarURI(document, publicId, systemId);
  //  return documentManager.getCMDocument(publicId, systemId);  
  //}
   
  public String resolveGrammarURI(String publicId, String systemId)
  {
    return resolveGrammarURI(null, publicId, systemId);
  }


  /**
   * This method should be specialized in order to implement specialized uri resolution
   */
  protected String resolveGrammarURI(Document document, String publicId, String systemId)
  {
    return systemId;
  }
  

  public CMElementDeclaration getCMElementDeclaration(Element element)
  { 
    CMElementDeclaration result = null; 
    Document document = element.getOwnerDocument();
    String[] doctypeInfo = getDoctypeInfo(document);
    if (doctypeInfo != null)
    {   
      // we have detected doctype information so we assume that we can locate the CMElementDeclaration 
      // in the CMDocument's table of global elements 
      CMDocument cmDocument = getCorrespondingCMDocument(element, false);

      // TODO... consider replacing above with 
      // CMDocument cmDocument = getCMDocument(document, doctypeInfo[0], doctypeInfo[1]);

      if (cmDocument != null)
      {        
        result = (CMElementDeclaration)cmDocument.getElements().getNamedItem(element.getNodeName());    
                 
        // this is a hack to get our xsl code assist working... we might want to handle similar
        // grammar behaviour via some established model query setting 
        if (result == null && getImplictDoctype(document) != null)
        {         
          Node parent = element.getParentNode();
          if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE)
          {
            result = getCMElementDeclaration((Element)parent);
          }
        }
      }
    }    
    else
    {  
      // here we use a namespaceTable to consider if the root element has any namespace information
      //
      NamespaceTable namespaceTable = new NamespaceTable(element.getOwnerDocument());
      List list = NamespaceTable.getElementLineage(element);
      Element rootElement = (Element)list.get(0);
      namespaceTable.addElement(rootElement);
         
      if (namespaceTable.isNamespaceEncountered())
      {                                         
        // we assume that this is an XMLSchema style namespace aware document
        result = getCMElementDeclaration(element, list, namespaceTable);
      }
      else
      { 
        // we assume that this is an inferred CMDocument for a DTD style 'namespaceless' document
        CMDocument cmDocument = getCMDocument("", "", "DTD"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        if (cmDocument != null)
        {
          result = (CMElementDeclaration)cmDocument.getElements().getNamedItem(element.getNodeName()); 
        }
      }
    }             
    return result;
  } 
     

  protected CMElementDeclaration getCMElementDeclaration(Element targetElement, List list, NamespaceTable namespaceTable)
  {         
    CMElementDeclaration currentED = null;
    try
    {    
      int listSize = list.size();
      for (int i = 0; i < listSize; i++)
      {
        Element element = (Element)list.get(i);                                     
                    
        if (i != 0)
        {
          namespaceTable.addElement(element);                        
        }

        String nodeName = element.getNodeName();
        String unprefixedName = DOMNamespaceHelper.getUnprefixedName(nodeName);
        String prefix = DOMNamespaceHelper.getPrefix(nodeName);
 
        CMElementDeclaration ed = null;
 
        // see if the element is a local of the currentED
        //             
        if (currentED != null)
        {  
          ed = (CMElementDeclaration)currentED.getLocalElements().getNamedItem(unprefixedName);
        } 
                                                                   
        if (ed == null) 
        {               
          NamespaceInfo namespaceInfo = namespaceTable.getNamespaceInfoForPrefix(prefix);                   
          if (namespaceInfo != null) 
          {
            CMDocument cmDocument = getCMDocument(namespaceInfo.uri, namespaceInfo.locationHint, "XSD"); //$NON-NLS-1$
            if (cmDocument != null)
            { 
              ed = (CMElementDeclaration)cmDocument.getElements().getNamedItem(unprefixedName);   
            }                                        
          }
        }                                                   
        currentED = ed;     

        // handle XSIType     
        if (currentED != null)
        {
          CMElementDeclaration derivedED = getDerivedCMElementDeclaration(element, currentED, namespaceTable);
          if (derivedED != null)
          {                           
            currentED = derivedED;
          }    
        }
      }                                       
    }
    catch (Exception e)
    { 
      Logger.logException("exception locating element declaration for " + targetElement, e); //$NON-NLS-1$
    } 
  
    return currentED;
  }  
      

  protected CMElementDeclaration getDerivedCMElementDeclaration(Element element, CMElementDeclaration ed, NamespaceTable namespaceTable)
  {                      
    CMElementDeclaration result = null;
    String xsiPrefix = namespaceTable.getPrefixForURI("http://www.w3.org/2001/XMLSchema-instance"); //$NON-NLS-1$
    if (xsiPrefix != null)
    {
      String xsiTypeValue = element.getAttribute(xsiPrefix + ":type"); //$NON-NLS-1$
      if (xsiTypeValue != null)
      {  
        String typePrefix = DOMNamespaceHelper.getPrefix(xsiTypeValue);
        String typeName = DOMNamespaceHelper.getUnprefixedName(xsiTypeValue);
        String typeURI = namespaceTable.getURIForPrefix(typePrefix);
        String uriQualifiedTypeName = typeName;
        if (typeURI != null && typeURI.length() > 0) 
        {
          uriQualifiedTypeName = "[" +  typeURI + "]" + typeName; //$NON-NLS-1$ //$NON-NLS-2$
        }
        result = (CMElementDeclaration)ed.getProperty("DerivedElementDeclaration=" + uriQualifiedTypeName);   //$NON-NLS-1$
      }
    }                                                                                                    
    return result;
  }   


  public CMAttributeDeclaration getCMAttributeDeclaration(Attr attr)
  {
    CMAttributeDeclaration result = null;
    Element element = attr.getOwnerElement();
    if (element != null)
    {
      CMElementDeclaration ed = getCMElementDeclaration(element);
      if (ed != null)
      {                                                
        result = (CMAttributeDeclaration)ed.getAttributes().getNamedItem(attr.getName());
        if (result == null)
        {                                              
          // try to get the unprefixed name             
          String name = DOMNamespaceHelper.getUnprefixedName(attr.getName());
          result = (CMAttributeDeclaration)ed.getAttributes().getNamedItem(name);
        }                                                                        
        if (result == null)
        {
          // todo... perhaps this is a globally defined attribute... 
        }
      }
    }
    return result;
  }               

  /**
   * This method returns a list of CMDocumentReferences associated with a particular node or subtree
   */                                                                                                          
  public List getCMDocumentReferences(Node node, boolean deep)
  { 
    List result = new ArrayList();  
    Document document = (node.getNodeType() == Node.DOCUMENT_NODE) ? (Document)node : node.getOwnerDocument();
    DocumentType doctype = document.getDoctype();
    // defect 206833 ... here we test for DTDs that are declared inline
    // since we currently have no way of making use of inline DTDs we ingore them
    // so that the implict DTD (if any) can be used
    if (doctype != null && (doctype.getPublicId() != null || doctype.getSystemId() != null))
    {                                                                               
      String uri = resolveGrammarURI(document, doctype.getPublicId(), doctype.getSystemId());
      result.add(new CMDocumentReferenceImpl(doctype.getPublicId(), uri));
    }   
    else if (getImplictDoctype(document) != null)
    {                  
      String[] implicitDoctype = getImplictDoctype(document);
      String uri = resolveGrammarURI(document, implicitDoctype[0], implicitDoctype[1]);
      result.add(new CMDocumentReferenceImpl(implicitDoctype[0], uri));
    }                              
    else
    {   
      NamespaceTable namespaceTable = new NamespaceTable(document);
      if (node.getNodeType() == Node.ELEMENT_NODE)
      {
		    namespaceTable.addElement((Element)node);
      }                                     
      if (deep)
      {
        addChildElementsToNamespaceTable(node, namespaceTable);
      }
	    List list = namespaceTable.getNamespaceInfoList();
		  for (Iterator i = list.iterator(); i.hasNext();) 
      {
			  NamespaceInfo info = (NamespaceInfo) i.next();    
        String uri = resolveGrammarURI(document, info.uri, info.locationHint);
        result.add(new CMDocumentReferenceImpl(info.uri, uri));
		  }	
    } 
    return result;
  }

  protected void addChildElementsToNamespaceTable(Node node, NamespaceTable namespaceTable)
  {
    NodeList nodeList = node.getChildNodes();
	  if (nodeList != null) 
    {
		  int nodeListLength = nodeList.getLength();
		  for (int i = 0; i < nodeListLength; i++) 
      {
			  Node childNode = nodeList.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE)
        {   
          namespaceTable.addElement((Element)childNode);
          addChildElementsToNamespaceTable(childNode, namespaceTable);
        }
		  }
	  }
  }  
}

Back to the top