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

import java.util.Map;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.emf.common.util.URI;
import org.eclipse.gef.commands.Command;
import org.eclipse.wst.sse.core.internal.encoding.CommonEncodingPreferenceNames;
import org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor;
import org.eclipse.wst.xml.core.internal.XMLCorePlugin;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.eclipse.wst.xml.core.internal.provisional.format.FormatProcessorXML;
import org.eclipse.wst.xsd.ui.internal.editor.XSDEditorPlugin;
import org.eclipse.xsd.XSDConcreteComponent;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.util.XSDConstants;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;

public class BaseCommand extends Command
{
  private static final String XML = "xml"; //$NON-NLS-1$

  XSDConcreteComponent addedXSDConcreteComponent;

  public BaseCommand()
  {
    super();
  }

  public BaseCommand(String label)
  {
    super(label);
  }
  
  public XSDConcreteComponent getAddedComponent()
  {
    return addedXSDConcreteComponent;
  }
  
  IDOMNode domNode;
  protected void beginRecording(Object element) {
    if (element instanceof IDOMNode)
    {
      domNode = (IDOMNode) element; 
      domNode.getModel().beginRecording(this, getUndoDescription());
    }
  }
  
  protected void endRecording()
  {
    if (domNode != null)
      domNode.getModel().endRecording(this);
  }
  
  protected String getUndoDescription() {
    return getLabel();
  }

  protected void formatChild(Element child)
  {
    if (child instanceof IDOMNode)
    {
      IDOMModel model = ((IDOMNode)child).getModel();
      try
      {
        // tell the model that we are about to make a big model change
        model.aboutToChangeModel();
        
        IStructuredFormatProcessor formatProcessor = new FormatProcessorXML();
        formatProcessor.formatNode(child);
      }
      finally
      {
        // tell the model that we are done with the big model change
        model.changedModel(); 
      }
    }
  }
 
  protected static void ensureSchemaElement(XSDSchema schema)
  {
    Document document = schema.getDocument();
    
    Element schemaElement = document.getDocumentElement();

    if (schemaElement == null)
    {
      String targetNamespace = getDefaultNamespace(schema);
      schema.setTargetNamespace(targetNamespace);
      Map qNamePrefixToNamespaceMap = schema.getQNamePrefixToNamespaceMap();
      qNamePrefixToNamespaceMap.put("tns", targetNamespace);      
      if (XSDEditorPlugin.getDefault().isQualifyXMLSchemaLanguage())
      {
        String prefix = XSDEditorPlugin.getDefault().getXMLSchemaPrefix();
        schema.setSchemaForSchemaQNamePrefix(prefix);
        qNamePrefixToNamespaceMap.put(prefix, XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);      
      }
      else
      {
        qNamePrefixToNamespaceMap.put(null, XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);      
      }
      
      schema.updateElement();
      ensureXMLDirective(document);
    }
  }

  private static void ensureXMLDirective(Document document)
  {
    if (hasXMLDirective(document))
    {
      return;
    }
    
    Node firstChild = document.getFirstChild();
    ProcessingInstruction xmlDeclaration = getXMLDeclaration(document);
    document.insertBefore(xmlDeclaration, firstChild);
    Text textNode = document.createTextNode(System.getProperty("line.separator"));
    document.insertBefore(textNode, firstChild);
  }
  
  private static boolean hasXMLDirective(Document document)
  {
    Node firstChild = document.getFirstChild();
   
    if (firstChild == null)
    {
      return false;
    }
    
    if (firstChild.getNodeType() != Node.PROCESSING_INSTRUCTION_NODE)
    {
      return false;
    }
    
    ProcessingInstruction processingInstruction  = (ProcessingInstruction)firstChild;
    
    if (!XML.equals(processingInstruction.getTarget())) 
    {
      return false;
    }
    
    return true;
  }

  private static ProcessingInstruction getXMLDeclaration(Document document)
  {
    Preferences preference = XMLCorePlugin.getDefault().getPluginPreferences();
    String charSet = preference.getString(CommonEncodingPreferenceNames.OUTPUT_CODESET);
    if (charSet == null || charSet.trim().equals(""))
    {
      charSet = "UTF-8";
    }
    ProcessingInstruction xmlDeclaration = document.createProcessingInstruction(XML, "version=\"1.0\" encoding=\"" + charSet + "\"");
    return xmlDeclaration;
  }
  
  private static String getDefaultNamespace(XSDSchema schema)
  {
    String namespace = XSDEditorPlugin.getDefault().getXMLSchemaTargetNamespace();

    if (!namespace.endsWith("/"))
    {
      namespace = namespace.concat("/");
    }

    namespace += getFileName(schema) + "/";

    return namespace;

  }
  
  private static String getFileName(XSDSchema schema)
  {
    URI schemaURI = schema.eResource().getURI();
    IPath filePath = new Path(schemaURI.toString());
    return filePath.removeFileExtension().lastSegment().toString();
  }  
}

Back to the top