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: 40c1bef077ef1da6ed0fbde36bb44bc8fcac6bdb (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
/*******************************************************************************
 * Copyright (c) 2001, 2008 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.wizards;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wst.common.ui.internal.viewers.SelectSingleFilePage;
import org.eclipse.wst.xsd.ui.internal.editor.XSDEditorPlugin;
import org.eclipse.wst.xsd.ui.internal.util.ViewUtility;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.util.XSDParser;


/**
 * Extend the base wizard to select a file from the project or outside the workbench
 * and add error handling
 */
public class XSDSelectIncludeFileWizard extends Wizard implements INewWizard
{
  boolean isInclude;
  XSDSchema mainSchema;
  XSDSchema externalSchema;

  XSDLocationChoicePage choicePage;
  XSDSelectSingleFilePage filePage;
  XSDURLPage urlPage;

  IFile resultFile;
  String resultURL;
  String namespace = "";

  public XSDSelectIncludeFileWizard(XSDSchema mainSchema, boolean isInclude,
                                    String title, String desc, 
                                    ViewerFilter filter,
                                    IStructuredSelection selection)
  {
    super();
    setWindowTitle(title);
    setDefaultPageImageDescriptor(ImageDescriptor.createFromFile(XSDEditorPlugin.class, "icons/NewXSD.png"));

    setNeedsProgressMonitor(true);

    // Choice Page
    choicePage = new XSDLocationChoicePage();

    // Select File Page
    filePage = new XSDSelectSingleFilePage(PlatformUI.getWorkbench(),  selection, true);
    filePage.setTitle(title);
    filePage.setDescription(desc);
    filePage.addFilter(filter);

    // URL Page
    urlPage = new XSDURLPage();
    urlPage.setTitle(title);
    urlPage.setDescription(XSDEditorPlugin.getXSDString("_UI_FILEDIALOG_SELECT_XML_URL"));
    
    this.mainSchema = mainSchema;
    this.isInclude = isInclude;
  }

  public void init(IWorkbench aWorkbench, IStructuredSelection aSelection)
  { 
  }

  public void addPages()
  {
    addPage(choicePage);
    addPage(filePage);
    addPage(urlPage);
  }

  public IWizardPage getNextPage(IWizardPage currentPage)
  {
    WizardPage nextPage = null;

    if (currentPage == choicePage)
    {
      if (choicePage.isURL()) 
      {
        nextPage = urlPage;
      }
      else
      {
        nextPage = filePage;
      }
    }
    return nextPage;
  }

  public boolean canFinish()
  {
    if (!choicePage.isURL())
    {
      return filePage.isPageComplete(); 
    }
    return true;
  }

  public boolean performFinish()
  { 
    if (choicePage.isURL())
    {
      try 
      {
        getContainer().run(false, true, urlPage.getRunnable());
        resultURL = urlPage.getURL();
      }
      catch (Exception e)
      {
        return false;
      }
      return true;
    }
    else
    {  
      resultFile = filePage.getFile();
    }
    return true;
  }

  /**
   * Get the MOF object that represents the external file
   */
  public XSDSchema getExternalSchema()
  {
    return externalSchema;
  }

  public IFile getResultFile()
  {
    return resultFile;
  }

  public String getURL()
  {
    return resultURL;
  }
  
  public String getNamespace()
  {
  	return namespace;
  }

  /**
   * Create a MOF model for the imported file
   */
  protected String doLoadExternalModel(IProgressMonitor monitor, String xsdModelFile, String xsdFileName)
  { 
    String errorMessage = null;
    String currentNameSpace = mainSchema.getTargetNamespace();

    monitor.beginTask("Loading XML Schema", 100);
    monitor.worked(50);

    XSDParser parser = new XSDParser();
    parser.parse(xsdModelFile);

    externalSchema = parser.getSchema();
    if (externalSchema != null)
    {
      String extNamespace = externalSchema.getTargetNamespace();
      namespace = extNamespace;
     
      if (externalSchema.getDiagnostics() != null &&
          externalSchema.getDiagnostics().size() > 0)
      {
        errorMessage = XSDEditorPlugin.getPlugin().getString("_UI_INCORRECT_XML_SCHEMA", xsdFileName);
      }  
      else
      {
        if (isInclude) 
        {  
          // Check the namespace to make sure they are the same as current file
          if (extNamespace != null)
          {
            if (currentNameSpace != null && !extNamespace.equals(currentNameSpace))
            {
              errorMessage = XSDEditorPlugin.getPlugin().getString("_UI_DIFFERENT_NAME_SPACE", xsdFileName);
            }
          }
        }
        else
        {  
          // Check the namespace to make sure they are different from the current file
          if (extNamespace != null)
          {
            if (currentNameSpace != null && extNamespace.equals(currentNameSpace))
            {
              errorMessage = XSDEditorPlugin.getPlugin().getString("_UI_SAME_NAME_SPACE", xsdFileName);
            }
          }
        }
      }
    }
    else
    {
      errorMessage = XSDEditorPlugin.getPlugin().getString("_UI_INCORRECT_XML_SCHEMA", xsdFileName);
    }

    monitor.subTask("Finish Loading");
    monitor.worked(80);

    return errorMessage;
  }

 
  /**
   * URL page
   */
  class XSDURLPage extends WizardPage
  { 
    Text urlField;
    String saveString;

    public XSDURLPage()
    {
      super("URLPage");
    }

    public void createControl(Composite parent)
    {
      Composite client = ViewUtility.createComposite(parent,2);
      ViewUtility.setComposite(client);

      ViewUtility.createLabel(client, XSDEditorPlugin.getXSDString("_UI_LABEL_URL"));
      ViewUtility.createLabel(client, "");

      urlField = ViewUtility.createTextField(client, 50);
      saveString = "http://";
      urlField.setText(saveString);

      setControl(client);
    }

    public String getURL()
    {
      return urlField.getText();
    }

    private boolean openExternalSchema(IProgressMonitor monitor)
    {
      String text = urlField.getText();
//      if (text.equals(saveString)) 
//      {
//        return false;
//      }
//      saveString = text;

      if (text.equals(""))
      {
        setErrorMessage(XSDEditorPlugin.getXSDString("_UI_SPECIFY_URL"));
        return false;
      }

      if ( !text.startsWith("http://") )
      {
        setErrorMessage(XSDEditorPlugin.getXSDString("_UI_URL_START_WITH"));
        return false;
      }

      setErrorMessage(null);
      String errorMessage = doLoadExternalModel(monitor, text, text);
      if (errorMessage != null) 
      {
        setErrorMessage(errorMessage); 
        return false;
      }
      else
      {
        return true;
      }
    }

    public IRunnableWithProgress getRunnable()
    {
      return new IRunnableWithProgress()
      {
        public void run(IProgressMonitor monitor)   
          throws InvocationTargetException, InterruptedException
        {
          if (monitor == null)
          {
            monitor= new NullProgressMonitor();
          }
          monitor.beginTask("", 6);
        
          boolean ok = openExternalSchema(monitor);

          if (!ok) 
          { 
            throw new InvocationTargetException(new java.lang.Error());
          }

          monitor.done();
        }
      };
    }
  }

  /**
   * Select XML Schema File
   */
  class XSDSelectSingleFilePage extends SelectSingleFilePage
  {
    public XSDSelectSingleFilePage(IWorkbench workbench, IStructuredSelection selection, boolean isFileMandatory)
    {          
      super(workbench,selection,isFileMandatory);
    }

    private boolean openExternalSchema()
    {
      // Get the fully-qualified file name
      IFile iFile = getFile();
      if (iFile == null) 
        return false;

      setErrorMessage(null);

      String xsdModelFile = iFile.getLocationURI().toString();
      String xsdFileName = iFile.getName();
      String errorMessage = doLoadExternalModel(new NullProgressMonitor(), xsdModelFile, xsdFileName);

      if (errorMessage != null) 
      {
        setErrorMessage(errorMessage);
        return false;
      }
      else
      {
        return true;
      }
    }

    public boolean isPageComplete()
    {  
      if (choicePage.isURL()) 
      {
        return true;
      }

      if (super.isPageComplete()) 
      {
        return openExternalSchema();
      }
      return super.isPageComplete();
    }
  }
}

Back to the top