Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 8b096ded7df9c138b4ea4b8039d80d04c5d9ccd4 (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
/*******************************************************************************
 * Copyright (c) 2002 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.util;

import org.eclipse.wst.xml.core.internal.contentmodel.CMDocument;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNode;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;


public class DOMNamespaceHelper
{                
  protected static String getURIForPrefix(Element element, String prefix)
  {              
    String result = null;
	  String nsAttrName = null;
	  if (prefix != null && prefix.length() > 0) 
    {
		  nsAttrName = "xmlns:" + prefix; //$NON-NLS-1$
	  }
	  else 
    {
		  nsAttrName = "xmlns"; //$NON-NLS-1$
	  }
            
    // assume the node is qualified... look up the URI base on the prefix
    //
	  for (Node node = element; node != null; node = node.getParentNode()) 
    {
	   	if (node.getNodeType() == Node.ELEMENT_NODE)
      {
        Element theElement = (Element)node;
		    Attr attr = theElement.getAttributeNode(nsAttrName);
		    if (attr != null) 
        {
          result = attr.getValue();
        }
      }
      else
      {
        break;
      }
    }
         
    // see if we can find some info from an 'implicit' namespace
    //
    if (result == null)
    {                                
      NamespaceTable table = new NamespaceTable(element.getOwnerDocument());
      result = table.getURIForPrefix(prefix);
    }
    return result;
	}

  public static String getNamespaceURI(Node node)
  {            
    String result = null;
    if (node.getNodeType() == Node.ELEMENT_NODE)
    {               
      Element element = (Element)node;
      String prefix = element.getPrefix();
      result = getURIForPrefix(element, prefix);
    }
    else if (node.getNodeType() == Node.ATTRIBUTE_NODE)
    {      
      Attr attr = (Attr)node;
      String prefix = attr.getPrefix();
      result = getURIForPrefix(attr.getOwnerElement(), prefix);
    }
    return result;
  }                 

  // todo... this is an ugly hack... needs to be fixed
  //
  public static String computePrefix(CMNode cmNode, Node parentNode)
  {
    String result = null;
    for (Node node = parentNode; node != null; node = node.getParentNode())
    {
      if (node.getNodeType() == Node.ELEMENT_NODE)
      {
        result = getPrefix(node.getNodeName());
        if (result != null)
        {
          break;
        }
      }
    }
    return result;
  }        


  public static String getPrefix(String name)
  {
    String prefix = null;
    int index = name.indexOf(":"); //$NON-NLS-1$
    if (index != -1)
    {
      prefix = name.substring(0, index);
    }
    return prefix;
  }


  public static String getUnprefixedName(String name)
  {
    int index = name.indexOf(":"); //$NON-NLS-1$
    if (index != -1)
    {
      name = name.substring(index + 1);
    }
    return name;
  }


  public static String computeName(CMNode cmNode, Node parent, String prefix)
  {     
    return computeName(cmNode, parent, prefix, null);
  }   


  public static String computeName(CMNode cmNode, Node parent, String prefix, NamespaceTable namespaceTable)
  {
    String result = cmNode.getNodeName();

    // if the cmNode has a hard coded prefix then we don't need to do anything
    //
    if (getPrefix(result) == null)
    {              
      String qualification = (String)cmNode.getProperty("http://org.eclipse.wst/cm/properties/nsPrefixQualification"); //$NON-NLS-1$
      // see if we need a namespace prefix
      //
      if (qualification != null && qualification.equals("qualified")) //$NON-NLS-1$
      {            
        if (prefix == null)                 
        {
          // todo... add getCMDocument() method to CMNode
          // for now use this getProperty() hack
          CMDocument cmDocument = (CMDocument)cmNode.getProperty("CMDocument"); //$NON-NLS-1$
          if (cmDocument != null)          
          {     
            String namespaceURI = (String)cmDocument.getProperty("http://org.eclipse.wst/cm/properties/targetNamespaceURI");    //$NON-NLS-1$
            if (namespaceURI != null)
            {   
              // use the NamespaceTable to figure out the correct prefix for this namespace uri
              //      
              if (namespaceTable == null)
              {                                            
                Document document = parent.getNodeType() == Node.DOCUMENT_NODE ? (Document)parent : parent.getOwnerDocument();
                namespaceTable = new NamespaceTable(document);
                if (parent instanceof Element)
                {
                  namespaceTable.addElementLineage((Element)parent);
                }
              }
              prefix = namespaceTable.getPrefixForURI(namespaceURI);
            }
          }
        }
        if (prefix != null && prefix.length() > 0)
        {
          result = prefix + ":" + result; //$NON-NLS-1$
        }
      }    
    }
    return result;
  }  
  

  public static String[] getURIQualifiedNameComponents(String uriQualifiedName)
  {
    String[] result = new String[2];
    int firstIndex = uriQualifiedName.indexOf("["); //$NON-NLS-1$
    int lastIndex = uriQualifiedName.indexOf("]"); //$NON-NLS-1$
    if (firstIndex != -1 && lastIndex > firstIndex)
    {
      result[0] = uriQualifiedName.substring(firstIndex + 1, lastIndex);
      result[1] = uriQualifiedName.substring(lastIndex + 1);
    }  
    else
    {
      result[1] = uriQualifiedName;
    }                              
    return result;
  }   
}

Back to the top