Skip to main content
summaryrefslogtreecommitdiffstats
blob: 25d9a633afd8c1c008e5185a271253cfb17b2d8f (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
404
405
/*******************************************************************************
 * 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.wsdl.ui.internal.util;

import org.eclipse.wst.common.ui.internal.UIPlugin;
import org.eclipse.wst.wsdl.ui.internal.Messages;

public class ValidateHelper
{
  // XML Lang can have many different valid formats
  // 1) xx      ie. en, fr, de
  // 2) xx-xx   ie. en-US, en-FR
  // 3) I-xx    ie. I-en
  // 4) X-xx    ie. X-en
  public static String checkXMLLang(String lang)
  {
    if (lang.length() == 0) 
      return null;
    
    if (lang.length() == 1) 
    {
      char ch0 = lang.charAt(0);
      if ((ch0 >= 'a' && ch0 <= 'z') || (ch0 >= 'A' && ch0 <= 'Z'))
        return UIPlugin.getResourceString("_WARN_LANG_TOO_SHORT");
      else
        return UIPlugin.getResourceString("_WARN_NAME_INVALID_CHAR") + ch0 + 
        UIPlugin.getResourceString("_UI_NAME_INVALID_CHAR_END");          
    }
    
    int offset;

    char ch0 = lang.charAt(0);
    if (lang.charAt(1) == '-') 
    {
      if (ch0 == 'i' || ch0 == 'I' || ch0 == 'x' || ch0 == 'X') 
        offset = 1;
      else
        return UIPlugin.getResourceString("_WARN_NAME_INVALID_CHAR") + ch0 + 
        UIPlugin.getResourceString("_UI_NAME_INVALID_CHAR_END");
    }
    else
    {
      char ch1 = lang.charAt(1);
      if ((ch0 >= 'a' && ch0 <= 'z') || (ch0 >= 'A' && ch0 <= 'Z'))
        if ((ch1 >= 'a' && ch1 <= 'z') || (ch1 >= 'A' && ch1 <= 'Z'))
          offset = 2;
        else
          return UIPlugin.getResourceString("_WARN_NAME_INVALID_CHAR") + ch1 + 
          UIPlugin.getResourceString("_UI_NAME_INVALID_CHAR_END");
      else
        return UIPlugin.getResourceString("_WARN_NAME_INVALID_CHAR") + ch0 + 
        UIPlugin.getResourceString("_UI_NAME_INVALID_CHAR_END");
    }

    if (lang.length() > offset) 
    {
      char ch = lang.charAt(offset++);
      if (ch != '-') 
        return UIPlugin.getResourceString("_WARN_HYPHEN_NEEDED") + Integer.toString(offset-1);        
      else 
      {
        while (true) 
        {
          if (ch == '-') 
          {
            if (lang.length() == offset) 
              return UIPlugin.getResourceString("_WARN_HYPHEN_ENDING");
            
            ch = lang.charAt(offset++);
            if ((ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z')) 
              return UIPlugin.getResourceString("_WARN_NAME_INVALID_CHAR") + ch + 
              UIPlugin.getResourceString("_UI_NAME_INVALID_CHAR_END");
            
            if (lang.length() == offset)
              return null;
          } 
          else if ((ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z')) 
            return UIPlugin.getResourceString("_WARN_NAME_INVALID_CHAR") + ch + 
            UIPlugin.getResourceString("_UI_NAME_INVALID_CHAR_END");             
          else if (lang.length() == offset)
            return null;
          ch = lang.charAt(offset++);
        }
      }
    }

    return null;
  }
  
  /**
   * Check to see if the min value is correct.
   * A minimum value must be non-negative and < maxValue
   */
  public static String isValidMinValue(String minValue, String maxValue)
  {
    int min;

    if (minValue == null || minValue.equals("")) //$NON-NLS-1$
    {
      // Nothing to check
      return null;
    }

    try
    {
      min = Integer.parseInt(minValue);
    }
    catch (NumberFormatException ex)
    {
      return UIPlugin.getResourceString("_ERROR_MIN_NOT_POSITIVE");
    }

    if (min < 0) 
    {
      return UIPlugin.getResourceString("_ERROR_MIN_NOT_POSITIVE");
    }

    try
    {
      int max = Integer.parseInt(maxValue);
      if (min > max) 
      {
        return UIPlugin.getResourceString("_ERROR_MIN_VALUE");
      }
    }
    catch (NumberFormatException ex)
    {
      // Max is not accurate. Don't compare it.
    }
    return null;
  }

  /**
   * Check to see if the max value is correct.
   * A maximum value must be non-negative and > minValue
   *
   * It can also be set the string "unbounded"
   */
  public static String isValidMaxValue(String maxValue, String minValue)
  {
    int max;

    if (maxValue == null || maxValue.equals(""))  //$NON-NLS-1$
    {
      // Nothing to check
      return null;
    }

    if (maxValue.equals("unbounded")) //$NON-NLS-1$
    {
      return null;
    }

    try
    {
      max = Integer.parseInt(maxValue);
    }
    catch (NumberFormatException ex)
    {
      return UIPlugin.getResourceString("_ERROR_MAX_NOT_POSITIVE");
    }

    if (max < 0) 
    {
      return UIPlugin.getResourceString("_ERROR_MAX_NOT_POSITIVE");
    }

    try
    {
      int min = Integer.parseInt(minValue);
      if (max < min) 
      {
        return UIPlugin.getResourceString("_ERROR_MAX_VALUE");
      }
    }
    catch (NumberFormatException ex)
    {
      // Min is not accurate. Don't compare it.
    }
    return null;
  }

  /**
   * Validate the name conforms to the XML spec
   */
  public static String checkXMLName(String name, boolean allowEntityRef)
  {
    int length = name.length();
    char character;

    if (length == 0) 
    {
      return Messages.getString("_WARN_NAME_MUST_CONTAIN_AT_LEAST_ONE_CHAR"); //$NON-NLS-1$
    }
    
    if (name.indexOf(" ") >= 0) //$NON-NLS-1$
    {
      return(Messages.getString("_WARN_NAME_HAS_SPACE")); //$NON-NLS-1$
    }

    int index = 0;
    if (length > 0 &&
        name.charAt(0) == '%')
    {
      if (allowEntityRef) 
      {
        // skip over the first character 
        index++;
      } // end of if ()
      else
      {
        return Messages.getString("_WARN_NAME_INVALID_FIRST"); //$NON-NLS-1$
      } // end of else
    }
    
    for(; index < length; index++)
    {
      character = name.charAt(index);

      if(index == 0)
      {
        if( !isXMLNameStart(character) )
        {
          return Messages.getString("_WARN_NAME_INVALID_FIRST"); //$NON-NLS-1$
        }
      }
      else
      {
        if(!isXMLNameChar(character))
        {
          if ((index == length - 1) && //check if the last character is a ';'
              allowEntityRef &&
              character == ';')
          {
            // we're still ok then
            continue;
          } // end of if ()
          else 
          {
            return Messages.getString("_WARN_NAME_INVALID_CHAR") + character +  //$NON-NLS-1$
            Messages.getString("_UI_NAME_INVALID_CHAR_END"); //$NON-NLS-1$
          } // end of else
        }
      }
    }
    return null;
  }

  /**
   * Validate the name conforms to the XML spec
   */
  public static String checkXMLName(String name)
  {
    return checkXMLName(name, false);
  }

  /**
   * isXMLNameStart
   **/
  private static boolean isXMLNameStart(char ch)
  {
    return (ch == '_' || ch == ':' || Character.isLetter(ch) );
  }

  /**
   * isXMLNameChar
   **/
  private static boolean isXMLNameChar(char ch)
  {
    return (Character.isLetterOrDigit(ch) || ch == '.' || ch == '-' || ch == '_' || ch == ':');
  }

  /**
   * isXMLPrefixStart
   **/
//  private static boolean isXMLPrefixStart(char ch)
//  {
//    return (ch == '_' || Character.isLetter(ch) );
//  }

  /**
   * isXMLPrefixChar
   **/
  private static boolean isXMLPrefixChar(char ch)
  {
    return (Character.isLetterOrDigit(ch) || ch == '.' || ch == '-' || ch == '_');
  }

  /**
   * parseElementText
   */
  public static String parseElementText(String text)
  {
    if (text.indexOf('<') != -1 || text.indexOf('>') != -1)
    {
      return UIPlugin.getResourceString("_WARN_ELEMENT_INVALID_CHAR");
    }
    return null;
  }

  /**
   * parseAttributeValue
   */
  public static String parseAttributeValue(String value)
  {
    if (value.indexOf('"') != -1 || value.indexOf('<') != -1 || value.indexOf('>') != -1)
    {
      return UIPlugin.getResourceString("_WARN_ATTRIBUTE_INVALID_CHAR");

    }
    return null;
  }

  /**
   * parseADATASection
   */
  public static String parseCDATASection(String section)
  {
    if (section.indexOf("]]>") != -1) //$NON-NLS-1$
    {
      return UIPlugin.getResourceString("_WARN_CDATA_INVALID_STRING");

    }
    return null;
  }

  /**
   * parseProcessiingInstruction
   */
  public static String parseProcessingInstructionData(String data)
  {
    if (data.indexOf("?>") != -1) //$NON-NLS-1$
    {
      return UIPlugin.getResourceString("_WARN_PROCESSING_INVALID_STRING");

    }
    return null;
  }

  /**
   * parseProcessingInstructionTarget
   */
  public static String parseProcessingInstructionTarget(String target)
  {
    if (target.length() == 3 && (target.indexOf("XML") != -1 || target.indexOf("XMl") != -1 || target.indexOf("XmL") != -1 || //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        target.indexOf("xML") != -1 || target.indexOf("Xml") != -1 || target.indexOf("xMl") != -1 || //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        target.indexOf("xmL") != -1 || target.indexOf("xml") != -1)) //$NON-NLS-1$ //$NON-NLS-2$
    {
      return UIPlugin.getResourceString("_WARN_PROCESSING_TARGET_INVALID_STRING");

    }
    return checkXMLName(target);
  }

  /**
   * parsComment
   */
  public static String parseComment(String comment)
  {
    if (comment.indexOf("--") != -1) //$NON-NLS-1$
    {
      return UIPlugin.getResourceString("_WARN_COMMENT_INVALID_STRING");
    }
    return null;
  }
  
  /**
   * Validate the prefix conforms to the XML spec
   */
  public static String checkXMLPrefix(String prefix)
  {
    int length = prefix.length();
    if (length == 0)
    {
      return null;
    }
    char character;
    
    if (prefix.indexOf(" ") >= 0) //$NON-NLS-1$
    {
      return(UIPlugin.getResourceString("_WARN_PREFIX_HAS_SPACE"));
    }
    
    for(int index = 0; index < length; index++)
    {
      character = prefix.charAt(index);

      if(!isXMLPrefixChar(character))
      {
        return UIPlugin.getString("_WARN_PREFIX_INVALID_CHAR", String.valueOf(character));
      }
    }
    return null;
  }

}

Back to the top