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: 561a2184fba779523438d22ef991128d68d45efc (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
/*******************************************************************************
 * Copyright (c) 2001, 2007 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
 *******************************************************************************/
package org.eclipse.wst.xsd.ui.internal.common.commands;

import java.util.List;
import java.util.Map;
import org.eclipse.wst.xml.core.internal.contentmodel.util.NamespaceTable;
import org.eclipse.wst.xsd.ui.internal.common.properties.sections.appinfo.SpecificationForExtensionsSchema;
import org.eclipse.wst.xsd.ui.internal.common.util.XSDCommonUIUtils;
import org.eclipse.xsd.XSDAnnotation;
import org.eclipse.xsd.XSDConcreteComponent;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDSchema;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class AddExtensionElementCommand extends AddExtensionCommand
{
  XSDElementDeclaration element;
  Element appInfo;
  Element newElement;

  public AddExtensionElementCommand(String label, XSDConcreteComponent input, XSDElementDeclaration element)
  {
    super(label);
    this.component = input;
    this.element = element;
  }

  public void execute()
  {
    try
    {
      beginRecording(component.getElement());
      super.execute();
      addAnnotationSet(component.getSchema(), extensionsSchemaSpec);
      formatChild(component.getElement());
    }
    finally
    {
      endRecording();
    }
  }

  public void undo()
  {
    super.undo();
    XSDAnnotation xsdAnnotation = XSDCommonUIUtils.getInputXSDAnnotation(component, false);
    xsdAnnotation.getElement().removeChild(appInfo);
    List appInfos = xsdAnnotation.getApplicationInformation();
    appInfos.remove(appInfo);
    xsdAnnotation.updateElement();

  }

  public void setSchemaProperties(SpecificationForExtensionsSchema spec)
  {
    this.extensionsSchemaSpec = spec;
  }

  public void addAnnotationSet(XSDSchema xsdSchema, SpecificationForExtensionsSchema spec)
  {
    XSDAnnotation xsdAnnotation = XSDCommonUIUtils.getInputXSDAnnotation(component, true);
    addAnnotationSet(spec, xsdAnnotation);
  }

  private void addAnnotationSet(SpecificationForExtensionsSchema spec, XSDAnnotation xsdAnnotation)
  {     
    XSDSchema schema= xsdAnnotation.getSchema();
    Element schemaElement = schema.getElement();

    if (xsdAnnotation.getApplicationInformation().size() == 0)
    {
      appInfo = xsdAnnotation.createApplicationInformation(null);
      xsdAnnotation.getElement().appendChild(appInfo);
      List appInfos = xsdAnnotation.getApplicationInformation();
      appInfos.add(appInfo);
    }
    else
    {
      // use the first appInfo
      appInfo = (Element)xsdAnnotation.getApplicationInformation().get(0);
    }

    String prefix = addNamespaceDeclarationIfRequired(schemaElement, "p", spec.getNamespaceURI());

    if (appInfo != null)
    {
      Document doc = appInfo.getOwnerDocument();
     
      newElement = doc.createElementNS(spec.getNamespaceURI(), element.getName());
      newElement.setPrefix(prefix);   
      appInfo.appendChild(newElement);

      xsdAnnotation.updateElement();
    }
  }

  public Object getNewObject()
  {
    return newElement;
  }
  
  /**
   * @deprecated
   */
  protected String createUniquePrefix(XSDConcreteComponent component)
  {
    String prefix = "p"; //$NON-NLS-1$
    Map prefMapper = component.getSchema().getQNamePrefixToNamespaceMap();
    if ( prefMapper.get(prefix) != null){
      int i = 1;
      while ( prefMapper.get(prefix + i) != null)
        i++;
      prefix += i;
    }
    return prefix;
  }  
  
  // TODO... common this up with wsdl.ui
  private String addNamespaceDeclarationIfRequired(Element schemaElement, String prefixHint, String namespace)
  {
    String prefix = null;      
    NamespaceTable namespaceTable = new NamespaceTable(schemaElement.getOwnerDocument());
    namespaceTable.addElement(schemaElement);
    prefix = namespaceTable.getPrefixForURI(namespace);
    if (prefix == null)
    { 
      String basePrefix = prefixHint;
      prefix = basePrefix;
      String xmlnsColon = "xmlns:"; //$NON-NLS-1$
      String attributeName = xmlnsColon + prefix;
      int count = 0;
      while (schemaElement.getAttribute(attributeName) != null)
      {
        count++;
        prefix = basePrefix + count;
        attributeName = xmlnsColon + prefix;
      }      
      schemaElement.setAttribute(attributeName, namespace);  
    }    
    return prefix;
  }
}

Back to the top