1 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 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 calendar1, Object calendar2) 39 { 40 return XMLCalendar.compare((XMLCalendar)calendar1, (XMLCalendar)calendar2); 41 } 42 43 public static int compareDuration(Object duration1, Object 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 normalize(String value, boolean collapse) 54 { 55 if (value == null) 56 { 57 return null; 58 } 59 60 StringBuffer 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 (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 (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 pattern) 135 { 136 return new PatternMatcherImpl(pattern); 137 } 138 139 140 147 public static Object createQName(String namespaceUri, String localPart, String prefix) 148 { 149 return new QName(namespaceUri, localPart, prefix); 150 } 151 152 158 public static void setQNameValues(Object qname, String namespaceUri, String localPart, String prefix) 159 { 160 QName qn = (QName)qname; 161 qn.setLocalPart(localPart); 162 qn.setNamespaceURI(namespaceUri); 163 qn.setPrefix(prefix); 164 } 165 166 169 public static String getQNameNamespaceURI(Object qname) 170 { 171 return ((QName)qname).getNamespaceURI(); 172 } 173 176 public static String getQNameLocalPart(Object qname) 177 { 178 return ((QName)qname).getLocalPart(); 179 } 180 181 184 public static String getQNamePrefix(Object 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 pattern) 194 { 195 regularExpression = new RegEx.RegularExpression(pattern, "X"); 196 } 197 198 public boolean matches(String value) 199 { 200 return regularExpression.matches(value); 201 } 202 203 public String toString() 204 { 205 return regularExpression.getPattern(); 206 } 207 } 208 209 228 } 229 | Popular Tags |