KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > xml > type > util > XMLTypeUtil


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: XMLTypeUtil.java,v 1.7 2005/06/27 13:34:27 emerks Exp $
16  */

17 package org.eclipse.emf.ecore.xml.type.util;
18
19
20 import org.eclipse.emf.ecore.EValidator;
21 import org.eclipse.emf.ecore.xml.type.internal.DataValue;
22 import org.eclipse.emf.ecore.xml.type.internal.QName;
23 import org.eclipse.emf.ecore.xml.type.internal.RegEx;
24 import org.eclipse.emf.ecore.xml.type.internal.XMLCalendar;
25 import org.eclipse.emf.ecore.xml.type.internal.XMLDuration;
26
27
28 /**
29  * This class contains convenient static methods for working with XML-related information.
30  */

31 public final class XMLTypeUtil
32 {
33   public static final int EQUALS = 0;
34   public static final int LESS_THAN = -1;
35   public static final int GREATER_THAN = 1;
36   public static final int INDETERMINATE = 2;
37
38   public static int compareCalendar(Object JavaDoc calendar1, Object JavaDoc calendar2)
39   {
40     return XMLCalendar.compare((XMLCalendar)calendar1, (XMLCalendar)calendar2);
41   }
42
43   public static int compareDuration(Object JavaDoc duration1, Object JavaDoc duration2)
44   {
45     return XMLDuration.compare((XMLDuration)duration1, (XMLDuration)duration2);
46   }
47
48   public static boolean isSpace(char value)
49   {
50     return DataValue.XMLChar.isSpace(value);
51   }
52
53   public static String JavaDoc normalize(String JavaDoc value, boolean collapse)
54   {
55     if (value == null)
56     {
57       return null;
58     }
59
60     StringBuffer JavaDoc buffer = null;
61     boolean skipSpace = collapse;
62     for (int i = 0, size = value.length(), offset = 0; i < size; i++)
63     {
64       char c = value.charAt(i);
65       if (isSpace(c))
66       {
67         if (skipSpace)
68         {
69           if (buffer == null)
70           {
71             buffer = new StringBuffer JavaDoc(value);
72           }
73           buffer.deleteCharAt(i - offset++);
74         }
75         else
76         {
77           skipSpace = collapse;
78           if (c != ' ')
79           {
80             if (buffer == null)
81             {
82               buffer = new StringBuffer JavaDoc(value);
83             }
84             buffer.setCharAt(i - offset, ' ');
85           }
86         }
87       }
88       else
89       {
90         skipSpace = false;
91       }
92     }
93
94     if (skipSpace)
95     {
96       if (buffer == null)
97       {
98         int length = value.length();
99         if (length > 0)
100         {
101           return value.substring(0, length - 1);
102         }
103         else
104         {
105           return value;
106         }
107       }
108       else
109       {
110         int length = buffer.length();
111         if (length > 0)
112         {
113           return buffer.substring(0, length - 1);
114         }
115         else
116         {
117           return buffer.toString();
118         }
119       }
120     }
121     else
122     {
123       if (buffer == null)
124       {
125         return value;
126       }
127       else
128       {
129         return buffer.toString();
130       }
131     }
132   }
133
134   public static EValidator.PatternMatcher createPatternMatcher(String JavaDoc pattern)
135   {
136     return new PatternMatcherImpl(pattern);
137   }
138   
139   
140   /**
141    * Creates a new QName object with the specified values
142    * @param namespaceUri namespace uri value or null
143    * @param localPart localPart (not null)
144    * @param prefix prefix value or null
145    * @return The newly created QName object
146    */

147   public static Object JavaDoc createQName(String JavaDoc namespaceUri, String JavaDoc localPart, String JavaDoc prefix)
148   {
149     return new QName(namespaceUri, localPart, prefix);
150   }
151   
152   /**
153    * Sets the QName object values to the specified onces
154    * @param namespaceUri namespace uri value or null
155    * @param localPart localPart (not null)
156    * @param prefix prefix value or null
157    */

158   public static void setQNameValues(Object JavaDoc qname, String JavaDoc namespaceUri, String JavaDoc localPart, String JavaDoc prefix)
159   {
160       QName qn = (QName)qname;
161       qn.setLocalPart(localPart);
162       qn.setNamespaceURI(namespaceUri);
163       qn.setPrefix(prefix);
164   }
165   
166   /**
167    * Returns the namespaceURI of a QName.
168    */

169   public static String JavaDoc getQNameNamespaceURI(Object JavaDoc qname)
170   {
171     return ((QName)qname).getNamespaceURI();
172   }
173   /**
174    * Returns the localPart of a QName.
175    */

176   public static String JavaDoc getQNameLocalPart(Object JavaDoc qname)
177   {
178     return ((QName)qname).getLocalPart();
179   }
180   
181   /**
182    * Returns the prefix of a QName.
183    */

184   public static String JavaDoc getQNamePrefix(Object JavaDoc qname)
185   {
186     return ((QName)qname).getPrefix();
187   }
188
189   private static class PatternMatcherImpl implements EValidator.PatternMatcher
190   {
191     protected RegEx.RegularExpression regularExpression;
192
193     public PatternMatcherImpl(String JavaDoc pattern)
194     {
195       regularExpression = new RegEx.RegularExpression(pattern, "X");
196     }
197
198     public boolean matches(String JavaDoc value)
199     {
200       return regularExpression.matches(value);
201     }
202
203     public String JavaDoc toString()
204     {
205       return regularExpression.getPattern();
206     }
207   }
208
209 /*
210   public static void main(String args[])
211   {
212     System.err.println("###YES");
213     String x1 = normalize(" x ", true);
214     String x2 = normalize(" x ", false);
215     String x3 = normalize(" x x ", true);
216     String x4 = normalize(" x x ", false);
217     String x5 = normalize(" x x ", true);
218     String x6 = normalize(" x x ", false);
219     String x7 = normalize(" x x", true);
220     String x8 = normalize(" x x", false);
221     String x9 = normalize(" x \n x", true);
222     String x10 = normalize(" x \n x", false);
223     System.err.println(XMLTypeFactory.eINSTANCE.createFromString(XMLTypePackage.eINSTANCE.getInt(), " 1 "));
224     System.err.println(XMLTypeFactory.eINSTANCE.createFromString(XMLTypePackage.eINSTANCE.getInt(), " 1 \n "));
225     System.err.println("###YES");
226   }
227 */

228 }
229
Popular Tags