001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.axi.datatype;
043:
044: import java.math.BigDecimal;
045: import java.util.ArrayList;
046: import java.util.Collections;
047: import java.util.List;
048: import org.netbeans.modules.xml.axi.datatype.Datatype.Facet;
049: import org.netbeans.modules.xml.schema.model.Whitespace;
050:
051: /**
052: *
053: * @author Ayub Khan
054: */
055: public abstract class NumberBase extends Datatype {
056:
057: public final static Number UNBOUNDED_VALUE = Double.MAX_VALUE;
058:
059: public final static String UNBOUNDED_STRING = "unbounded"; //NoI18n
060:
061: static List<Facet> applicableFacets;
062:
063: private Datatype.Kind kind;
064:
065: private boolean hasFacets;
066:
067: private boolean isList;
068:
069: private List<Integer> totalDigits;
070:
071: private List<Integer> fractionDigits;
072:
073: private List<String> patterns;
074:
075: private List<Whitespace.Treatment> whitespaces;
076:
077: private List<Number> enumerations;
078:
079: private List<Number> maxInclusives;
080:
081: private List<Number> maxExclusives;
082:
083: private List<Number> minInclusives;
084:
085: private List<Number> minExclusives;
086:
087: /** Creates a new instance of NumberBase */
088: public NumberBase(Datatype.Kind kind) {
089: this .kind = kind;
090: }
091:
092: public Datatype.Kind getKind() {
093: return kind;
094: }
095:
096: public synchronized List<Facet> getApplicableFacets() {
097: if (applicableFacets == null) {
098: List<Facet> facets = new ArrayList<Facet>();
099: facets.add(Facet.TOTATDIGITS);
100: facets.add(Facet.FRACTIONDIGITS);
101: facets.add(Facet.PATTERN);
102: facets.add(Facet.WHITESPACE);
103: facets.add(Facet.ENUMERATION);
104: facets.add(Facet.MAXINCLUSIVE);
105: facets.add(Facet.MAXEXCLUSIVE);
106: facets.add(Facet.MININCLUSIVE);
107: facets.add(Facet.MINEXCLUSIVE);
108: applicableFacets = Collections.unmodifiableList(facets);
109: }
110: return applicableFacets;
111: }
112:
113: public boolean hasFacets() {
114: return hasFacets;
115: }
116:
117: public boolean isList() {
118: return isList;
119: }
120:
121: public void setIsList(boolean isList) {
122: this .isList = isList;
123: }
124:
125: /*
126: * returns pattern value (this corresponds to the value of pattern facet in schema)
127: *
128: * @return pattern
129: */
130: public List<String> getPatterns() {
131: return patterns;
132: }
133:
134: /*
135: * returns enumeration values (this corresponds to the values of enumeration facets in schema)
136: *
137: * @return enumeration
138: */
139: public List<Number> getEnumerations() {
140: return enumerations;
141: }
142:
143: /*
144: * returns whitespace value (this corresponds to the value of whitespace facet in schema)
145: *
146: * @return whitespaces
147: */
148: public List<Whitespace.Treatment> getWhiteSpaces() {
149: return whitespaces;
150: }
151:
152: /*
153: * returns total digits value (this corresponds to the value of totalDigits facet in schema)
154: *
155: * @return totalDigits
156: */
157: public List<Integer> getTotalDigits() {
158: return totalDigits;
159: }
160:
161: /*
162: * returns fraction digits value (this corresponds to the value of fractionDigits facet in schema)
163: *
164: * @return fractionDigits
165: */
166: public List<Integer> getFractionDigits() {
167: return fractionDigits;
168: }
169:
170: /*
171: * returns maximum Inclusive value (this corresponds to the value of maxInclusive facet in schema)
172: *
173: * @return maxInclusive
174: */
175: public List<Number> getMaxInclusives() {
176: return maxInclusives;
177: }
178:
179: /*
180: * returns maximum Exclusive value (this corresponds to the value of maxExclusive facet in schema)
181: *
182: * @return maxExclusive
183: */
184: public List<Number> getMaxExclusives() {
185: return maxExclusives;
186: }
187:
188: /*
189: * returns minimum Inclusive value (this corresponds to the value of minInclusive facet in schema)
190: *
191: * @return minInclusive
192: */
193: public List<Number> getMinInclusives() {
194: return minInclusives;
195: }
196:
197: /*
198: * returns minExclusive value (this corresponds to the value of minExclusive facet in schema)
199: *
200: * @return minExclusive
201: */
202: public List<Number> getMinExclusives() {
203: return minExclusives;
204: }
205:
206: /*
207: * set pattern value (this corresponds to the value of pattern facet in schema)
208: *
209: * @param pattern
210: */
211: public void addPattern(String pattern) {
212: if (patterns == null) {
213: patterns = new ArrayList<String>(1);
214: hasFacets = true;
215: }
216: this .patterns.add(pattern);
217: }
218:
219: /*
220: * set enumeration values (this corresponds to the values of enumeration facets in schema)
221: *
222: * @param enumeration
223: */
224: public void addEnumeration(Number enumeration) {
225: if (enumerations == null) {
226: enumerations = new ArrayList<Number>(1);
227: hasFacets = true;
228: }
229: this .enumerations.add(enumeration);
230: }
231:
232: /*
233: * set whitespace value (this corresponds to the value of whitespace facet in schema)
234: *
235: * @param whitespace
236: */
237: public void addWhitespace(Whitespace.Treatment whitespace) {
238: if (whitespaces == null) {
239: whitespaces = new ArrayList<Whitespace.Treatment>(1);
240: hasFacets = true;
241: }
242: this .whitespaces.add(whitespace);
243: }
244:
245: /*
246: * set total digits value (this corresponds to the value of totalDigits facet in schema)
247: *
248: * @param totalDigits
249: */
250: public void addTotalDigits(int totalDigits) {
251: if (this .totalDigits == null) {
252: this .totalDigits = new ArrayList<Integer>(1);
253: hasFacets = true;
254: }
255: this .totalDigits.add(new Integer(totalDigits));
256: }
257:
258: /*
259: * set fraction digits value (this corresponds to the value of fractionDigits facet in schema)
260: *
261: * @param fractionDigits
262: */
263: public void addFractionDigits(int fractionDigits) {
264: if (this .fractionDigits == null) {
265: this .fractionDigits = new ArrayList<Integer>(1);
266: hasFacets = true;
267: }
268: this .fractionDigits.add(new Integer(fractionDigits));
269: }
270:
271: /*
272: * set maximum Inclusive value (this corresponds to the value of maxInclusive facet in schema)
273: *
274: * @param maxInclusive
275: */
276: public void addMaxInclusive(Number maxInclusive) {
277: if (maxInclusives == null) {
278: maxInclusives = new ArrayList<Number>(1);
279: hasFacets = true;
280: }
281: this .maxInclusives.add(maxInclusive);
282: }
283:
284: /*
285: * set maximum Exclusive value (this corresponds to the value of maxExclusive facet in schema)
286: *
287: * @param maxExclusive
288: */
289: public void addMaxExclusive(Number maxExclusive) {
290: if (maxExclusives == null) {
291: maxExclusives = new ArrayList<Number>(1);
292: hasFacets = true;
293: }
294: this .maxExclusives.add(maxExclusive);
295: }
296:
297: /*
298: * set minimum Inclusive value (this corresponds to the value of minInclusive facet in schema)
299: *
300: * @param minInclusive
301: */
302: public void addMinInclusive(Number minInclusive) {
303: if (minInclusives == null) {
304: minInclusives = new ArrayList<Number>(1);
305: hasFacets = true;
306: }
307: this .minInclusives.add(minInclusive);
308: }
309:
310: /*
311: * set minExclusive value (this corresponds to the value of minExclusive facet in schema)
312: *
313: * @param minExclusive
314: */
315: public void addMinExclusive(Number minExclusive) {
316: if (minExclusives == null) {
317: minExclusives = new ArrayList<Number>(1);
318: hasFacets = true;
319: }
320: this .minExclusives.add(minExclusive);
321: }
322:
323: /*
324: * remove pattern value (this corresponds to the value of pattern facet in schema)
325: *
326: * @param pattern
327: */
328: public void removePattern(String pattern) {
329: if (patterns != null)
330: patterns.remove(pattern);
331: }
332:
333: /*
334: * set enumeration values (this corresponds to the values of enumeration facets in schema)
335: *
336: * @param enumeration
337: */
338: public void removeEnumeration(Number enumeration) {
339: if (enumerations != null)
340: enumerations.remove(enumeration);
341: }
342:
343: /*
344: * set whitespace value (this corresponds to the value of whitespace facet in schema)
345: *
346: * @param whitespace
347: */
348: public void removeWhitespace(Whitespace.Treatment whitespace) {
349: if (whitespaces != null)
350: whitespaces.remove(whitespace);
351: }
352:
353: /*
354: * set total digits value (this corresponds to the value of totalDigits facet in schema)
355: *
356: * @param totalDigits
357: */
358: public void removeTotalDigits(Number totalDigits) {
359: if (this .totalDigits != null)
360: this .totalDigits.remove(totalDigits);
361: }
362:
363: /*
364: * set fraction digits value (this corresponds to the value of fractionDigits facet in schema)
365: *
366: * @param fractionDigits
367: */
368: public void removeFractionDigits(Number fractionDigits) {
369: if (this .fractionDigits != null)
370: this .fractionDigits.remove(fractionDigits);
371: }
372:
373: /*
374: * set maximum Inclusive value (this corresponds to the value of maxInclusive facet in schema)
375: *
376: * @param maxInclusive
377: */
378: public void removeMaxInclusive(Number maxInclusive) {
379: if (maxInclusives != null)
380: maxInclusives.remove(maxInclusive);
381: }
382:
383: /*
384: * set maximum Exclusive value (this corresponds to the value of maxExclusive facet in schema)
385: *
386: * @param maxExclusive
387: */
388: public void removeMaxExclusive(Number maxExclusive) {
389: if (maxExclusives != null)
390: maxExclusives.remove(maxExclusive);
391: }
392:
393: /*
394: * set minimum Inclusive value (this corresponds to the value of minInclusive facet in schema)
395: *
396: * @param minInclusive
397: */
398: public void removeMinInclusive(Number minInclusive) {
399: if (minInclusives != null)
400: minInclusives.remove(minInclusive);
401: }
402:
403: /*
404: * set minExclusive value (this corresponds to the value of minExclusive facet in schema)
405: *
406: * @param minExclusive
407: */
408: public void removeMinExclusive(Number minExclusive) {
409: if (minExclusives != null)
410: minExclusives.remove(minExclusive);
411: }
412:
413: public static Number toNumber(String value) {
414: Number n = null;
415: if (value.equals(UNBOUNDED_STRING))
416: n = UNBOUNDED_VALUE;
417: else {
418: try {
419: n = new BigDecimal(value);
420: } catch (Throwable th) {
421: n = 0;
422: }
423: }
424: return n;
425: }
426:
427: public static String toXMLString(Number val) {
428: if (val == UNBOUNDED_VALUE)
429: return UNBOUNDED_STRING;
430: else
431: return String.valueOf(val);
432: }
433: }
|