1 /* 2 * Copyright 2007 Ralf Joachim 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package org.exolab.castor.builder.types; 15 16 import org.exolab.castor.xml.schema.Facet; 17 import org.exolab.javasource.JSourceCode; 18 19 /** 20 * A base class for types which support the digits, range, whiteSpace and pattern facets. 21 * 22 * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a> 23 * @version $Revision: 6662 $ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $ 24 * @since 1.1 25 */ 26 public abstract class AbstractDigitsFacet extends AbstractRangeFacet { 27 // -------------------------------------------------------------------------- 28 29 /** 30 * If set to true only '0' is allowed for the fractionDigits facet. If set to false all positive 31 * values are allowed for fractionDigits facet. 32 */ 33 private final boolean _fractionDigitsZeroOnly; 34 35 /** Total number of digits. */ 36 private int _totalDigits = -1; 37 38 /** Total number of fraction digits. */ 39 private int _fractionDigits = -1; 40 41 // -------------------------------------------------------------------------- 42 43 /** 44 * No-arg constructor. By default only '0' is supported for the fractionDigits facet. 45 */ 46 protected AbstractDigitsFacet() { 47 this(true); 48 } 49 50 /** 51 * Construct a new AbstractDigitsFacet optionally allowing the fractionDigits facet to be set to 52 * all positive values. 53 * 54 * @param fractionDigitsZeroOnly If set to true only '0' is allowed for the fractionDigits facet. 55 * If set to false all positive values are allowed for fractionDigits facet. 56 */ 57 protected AbstractDigitsFacet(final boolean fractionDigitsZeroOnly) { 58 _fractionDigitsZeroOnly = fractionDigitsZeroOnly; 59 } 60 61 // -------------------------------------------------------------------------- 62 63 /** 64 * Returns the totalDigits facet value of this XSType. 65 * 66 * @return The totalDigits facet value of this XSType. 67 */ 68 public final int getTotalDigits() { 69 return _totalDigits; 70 } 71 72 /** 73 * Sets the totalDigits facet for this XSType. 74 * 75 * @param totalDigits The value of totalDigits (must be >0). 76 */ 77 public final void setTotalDigits(final int totalDigits) { 78 if (totalDigits <= 0) { 79 throw new IllegalArgumentException( 80 getName() + ": the totalDigits facet must be positive: " + totalDigits); 81 } 82 _totalDigits = totalDigits; 83 } 84 85 /** 86 * Returns the fractionDigits facet value of this XSType. 87 * 88 * @return The fractionDigits facet value of this XSType. 89 */ 90 public final int getFractionDigits() { 91 return _fractionDigits; 92 } 93 94 /** 95 * Sets the fractionDigits facet for this XSType. 96 * 97 * @param fractionDigits The value of fractionDigits (must be >=0). 98 */ 99 public final void setFractionDigits(final int fractionDigits) { 100 if (fractionDigits < 0) { 101 throw new IllegalArgumentException( 102 getName() + ": the fractionDigits facet must be positive: " + fractionDigits); 103 } 104 if (_fractionDigitsZeroOnly && (fractionDigits > 0)) { 105 throw new IllegalArgumentException( 106 getName() + ": only '0' allowed for fractionDigits facet: " + fractionDigits); 107 } 108 _fractionDigits = fractionDigits; 109 } 110 111 // -------------------------------------------------------------------------- 112 113 /** 114 * {@inheritDoc} 115 */ 116 protected final void setFacet(final Facet facet) { 117 super.setFacet(facet); 118 setDigitsFacet(facet); 119 } 120 121 /** 122 * Transfer given facet if it is a digits facet. 123 * 124 * @param facet The facet to transfer. 125 */ 126 protected final void setDigitsFacet(final Facet facet) { 127 String name = facet.getName(); 128 if (Facet.TOTALDIGITS.equals(name)) { 129 setTotalDigits(facet.toInt()); 130 } else if (Facet.FRACTIONDIGITS.equals(name)) { 131 setFractionDigits(facet.toInt()); 132 } 133 } 134 135 /** 136 * Generate the source code for digits facet validation. 137 * 138 * @param jsc The JSourceCode to fill in. 139 * @param validatorName The name of the TypeValidator that the digits should be added to. 140 */ 141 protected final void codeDigitsFacet(final JSourceCode jsc, final String validatorName) { 142 // -- totalDigits 143 if (getTotalDigits() != -1) { 144 jsc.add("{0}.setTotalDigits({1});", validatorName, Integer.toString(getTotalDigits())); 145 } 146 147 // -- fractionDigits 148 if (!_fractionDigitsZeroOnly && (getFractionDigits() != -1)) { 149 jsc.add("{0}.setFractionDigits({1});", validatorName, Integer.toString(getFractionDigits())); 150 } 151 } 152 153 // -------------------------------------------------------------------------- 154 }