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 whiteSpace and pattern facets. 21 * 22 * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a> 23 * @version $Revision: 6678 $ $Date: 2005-03-05 06:42:06 -0700 (Sat, 05 Mar 2005) $ 24 * @since 1.1 25 */ 26 public abstract class AbstractWhiteSpaceFacet extends AbstractPatternFacet { 27 28 /** 29 * If set to true only 'collapse' is allowed for the whiteSpace facet. If set to false 'collaps', 30 * 'replace' and 'preserve' values are allowed for whiteSpace facet. 31 */ 32 private final boolean _whiteSpaceCollapseOnly; 33 34 /** The whiteSpace facet. */ 35 private String _whiteSpace = Facet.WHITESPACE_COLLAPSE; 36 37 /** 38 * No-arg constructor. By default only 'collapse' is supported for the whiteSpace facet. 39 */ 40 protected AbstractWhiteSpaceFacet() { 41 this(true); 42 } 43 44 /** 45 * Construct a new AbstractXSPatternFacet optionally allowing the whiteSpace facet to be set to 46 * 'replace' and 'preserve' values in addition to 'collaps'. 47 * 48 * @param whiteSpaceCollapseOnly If set to true only 'collapse' is allowed for the whiteSpace 49 * facet. If set to false 'collaps', 'replace' and 'preserve' values are allowed for 50 * whiteSpace facet. 51 */ 52 protected AbstractWhiteSpaceFacet(final boolean whiteSpaceCollapseOnly) { 53 _whiteSpaceCollapseOnly = whiteSpaceCollapseOnly; 54 } 55 56 /** 57 * Returns true if the whiteSpace facet is used. 58 * 59 * @return True if the whiteSpace facet is used. 60 */ 61 public final boolean hasWhiteSpace() { 62 return (_whiteSpace != null); 63 } 64 65 /** 66 * Returns the whiteSpace facet of this type. 67 * 68 * @return The whiteSpace facet of this type. 69 */ 70 public final String getWhiteSpace() { 71 return _whiteSpace; 72 } 73 74 /** 75 * Sets the whiteSpace facet of this XSType. The value of the whiteSpace facet must be one of the 76 * following: 77 * <ul> 78 * <li>preserve</li> 79 * <li>replace</li> 80 * <li>collapse</li> 81 * </ul> 82 * Any other value will generate a warning and the whiteSpace facet keeps unchanged. 83 * 84 * @param value The value for the whiteSpace facet. 85 */ 86 public final void setWhiteSpace(final String value) { 87 if (Facet.WHITESPACE_COLLAPSE.equals(value)) { 88 _whiteSpace = value; 89 } else if (!_whiteSpaceCollapseOnly) { 90 if (Facet.WHITESPACE_REPLACE.equals(value)) { 91 _whiteSpace = value; 92 } else if (Facet.WHITESPACE_PRESERVE.equals(value)) { 93 _whiteSpace = value; 94 } else { 95 throw new IllegalArgumentException( 96 getName() + ": bad entry for whiteSpace facet: " + value); 97 } 98 } else { 99 throw new IllegalArgumentException( 100 getName() + ": only 'collapse' allowed for whiteSpace facet: " + value); 101 } 102 } 103 104 /** 105 * {@inheritDoc} 106 */ 107 protected void setFacet(final Facet facet) { 108 super.setFacet(facet); 109 setWhiteSpaceFacet(facet); 110 } 111 112 /** 113 * Transfer given facet if it is a whiteSpace. 114 * 115 * @param facet The facet to transfer. 116 */ 117 protected final void setWhiteSpaceFacet(final Facet facet) { 118 if (Facet.WHITESPACE.equals(facet.getName())) { 119 setWhiteSpace(facet.getValue()); 120 } 121 } 122 123 /** 124 * Generate the source code for pattern facet validation. 125 * 126 * @param jsc The JSourceCode to fill in. 127 * @param validatorName The name of the TypeValidator that the whiteSpace should be added to. 128 */ 129 protected final void codeWhiteSpaceFacet(final JSourceCode jsc, final String validatorName) { 130 if (!_whiteSpaceCollapseOnly && hasWhiteSpace()) { 131 jsc.add("{0}.setWhiteSpace(\"{1}\");", validatorName, getWhiteSpace()); 132 } 133 } 134 135 }