1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements.  See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership.  The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License.  You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied.  See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21 
22 package org.apache.openoffice.ooxml.schema.model.simple;
23 
24 import java.util.Set;
25 import java.util.TreeSet;
26 
27 import org.apache.openoffice.ooxml.schema.model.base.INodeVisitor;
28 import org.apache.openoffice.ooxml.schema.model.base.Location;
29 import org.apache.openoffice.ooxml.schema.model.base.Node;
30 import org.apache.openoffice.ooxml.schema.model.base.NodeType;
31 import org.apache.openoffice.ooxml.schema.model.base.QualifiedName;
32 
33 /** Representation of the 'restriction' XML schema element.
34  *  It defines constraints on a another simple type.
35  *  Examples for such restrictions are minimum and maximum values
36  *  (inclusive or exclusive), patterns or length constraints of strings,
37  *  sets of valid values.
38  */
39 public class Restriction
40     extends Node
41 {
42     public final static int MinInclusiveBit = 0x0001;
43     public final static int MinExclusiveBit = 0x0002;
44     public final static int MaxInclusiveBit = 0x0004;
45     public final static int MaxExclusiveBit = 0x0008;
46     public final static int LengthBit = 0x0010;
47     public final static int MinLengthBit = 0x0020;
48     public final static int MaxLengthBit = 0x0040;
49     public final static int PatternBit = 0x0080;
50     public final static int EnumerationBit = 0x0100;
51 
Restriction( final Node aParent, final QualifiedName aBaseType, final Location aLocation)52     public Restriction (
53         final Node aParent,
54         final QualifiedName aBaseType,
55         final Location aLocation)
56     {
57         super(aParent, null, aLocation);
58         maBaseType = aBaseType;
59         maEnumerations = new TreeSet<>();
60         mnFeatures = 0;
61     }
62 
63 
64 
65 
66     @Override
AcceptVisitor(final INodeVisitor aVisitor)67     public void AcceptVisitor (final INodeVisitor aVisitor)
68     {
69         aVisitor.Visit(this);
70     }
71 
72 
73 
74 
75     @Override
GetNodeType()76     public NodeType GetNodeType()
77     {
78         return NodeType.Restriction;
79     }
80 
81 
82 
83 
GetBaseType()84     public QualifiedName GetBaseType ()
85     {
86         return maBaseType;
87     }
88 
89 
90 
91 
92     @Override
toString()93     public String toString ()
94     {
95         final StringBuffer aBuffer = new StringBuffer("restriction based on ");
96         aBuffer.append(maBaseType.GetDisplayName());
97 
98         if (msMinInclusive != null)
99         {
100             aBuffer.append(",minInclusive=");
101             aBuffer.append(msMinInclusive);
102         }
103         if (msMinExclusive != null)
104         {
105             aBuffer.append(",minExclusive=");
106             aBuffer.append(msMinExclusive);
107         }
108         if (msMaxInclusive != null)
109         {
110             aBuffer.append(",maxInclusive=");
111             aBuffer.append(msMaxInclusive);
112         }
113         if (msMaxExclusive != null)
114         {
115             aBuffer.append(",maxExclusive=");
116             aBuffer.append(msMaxExclusive);
117         }
118         if (HasFeature(LengthBit))
119         {
120             aBuffer.append(",length=");
121             aBuffer.append(mnLength);
122         }
123         if (HasFeature(MinLengthBit))
124         {
125             aBuffer.append(",minLength=");
126             aBuffer.append(mnMinLength);
127         }
128         if (HasFeature(MaxLengthBit))
129         {
130             aBuffer.append(",maxLength=");
131             aBuffer.append(mnMaxLength);
132         }
133         if (msPattern != null)
134         {
135             aBuffer.append(",pattern=\"");
136             aBuffer.append(msPattern);
137             aBuffer.append('"');
138         }
139         if ( ! maEnumerations.isEmpty())
140         {
141             aBuffer.append(",enumerations");
142             aBuffer.append(maEnumerations);
143         }
144         return aBuffer.toString();
145     }
146 
147 
148 
AddEnumeration(final String sValue)149     public void AddEnumeration (final String sValue)
150     {
151         maEnumerations.add(sValue);
152         mnFeatures |= EnumerationBit;
153     }
154 
155 
156 
157 
SetMinInclusive(final String sValue)158     public void SetMinInclusive (final String sValue)
159     {
160         msMinInclusive = sValue;
161         assert( ! HasFeature(MinExclusiveBit));
162         mnFeatures |= MinInclusiveBit;
163     }
164 
165 
166 
167 
SetMinExclusive(final String sValue)168     public void SetMinExclusive (final String sValue)
169     {
170         msMinExclusive = sValue;
171         assert( ! HasFeature(MinInclusiveBit));
172         mnFeatures |= MinExclusiveBit;
173     }
174 
175 
176 
177 
SetMaxInclusive(final String sValue)178     public void SetMaxInclusive (final String sValue)
179     {
180         msMaxInclusive = sValue;
181         assert( ! HasFeature(MaxExclusiveBit));
182         mnFeatures |= MaxInclusiveBit;
183     }
184 
185 
186 
187 
SetMaxExclusive(final String sValue)188     public void SetMaxExclusive (final String sValue)
189     {
190         msMaxExclusive = sValue;
191         assert( ! HasFeature(MaxInclusiveBit));
192         mnFeatures |= MaxExclusiveBit;
193     }
194 
195 
196 
197 
SetLength(final String sValue)198     public void SetLength (final String sValue)
199     {
200         mnLength = Integer.parseInt(sValue);
201         assert( ! HasFeature(MinLengthBit|MaxLengthBit));
202         mnFeatures |= LengthBit;
203     }
204 
205 
206 
207 
SetMinLength(final String sValue)208     public void SetMinLength (final String sValue)
209     {
210         mnMinLength = Integer.parseInt(sValue);
211         assert( ! HasFeature(LengthBit));
212         mnFeatures |= MinLengthBit;
213     }
214 
215 
216 
217 
SetMaxLength(final String sValue)218     public void SetMaxLength (final String sValue)
219     {
220         mnMaxLength = Integer.parseInt(sValue);
221         assert( ! HasFeature(LengthBit));
222         mnFeatures |= MaxLengthBit;
223     }
224 
225 
226 
227 
SetPattern(final String sValue)228     public void SetPattern (final String sValue)
229     {
230         msPattern = sValue;
231         mnFeatures |= PatternBit;
232     }
233 
234 
235 
236 
GetMinInclusive()237     public String GetMinInclusive ()
238     {
239         return msMinInclusive;
240     }
241 
242 
243 
244 
GetMinExclusive()245     public String GetMinExclusive ()
246     {
247         return msMinExclusive;
248     }
249 
250 
251 
252 
GetMaxInclusive()253     public String GetMaxInclusive ()
254     {
255         return msMaxInclusive;
256     }
257 
258 
259 
260 
GetMaxExclusive()261     public String GetMaxExclusive ()
262     {
263         return msMaxExclusive;
264     }
265 
266 
267 
268 
GetEnumeration()269     public Set<String> GetEnumeration()
270     {
271         return maEnumerations;
272     }
273 
274 
275 
276 
GetLength()277     public int GetLength()
278     {
279         return mnLength;
280     }
281 
282 
283 
284 
GetMinimumLength()285     public int GetMinimumLength()
286     {
287         return mnMinLength;
288     }
289 
290 
291 
292 
GetMaximumLength()293     public int GetMaximumLength()
294     {
295         return mnMaxLength;
296     }
297 
298 
299 
300 
301 
GetPattern()302     public String GetPattern()
303     {
304         return msPattern;
305     }
306 
307 
308 
309 
GetFeatureBits()310     public int GetFeatureBits ()
311     {
312         return mnFeatures;
313     }
314 
315 
316 
317 
HasFeature(final int nBitMask)318     public boolean HasFeature (final int nBitMask)
319     {
320         return (mnFeatures & nBitMask) != 0;
321     }
322 
323 
324 
325 
326     private final QualifiedName maBaseType;
327     private final Set<String> maEnumerations;
328     private String msMinInclusive;
329     private String msMinExclusive;
330     private String msMaxInclusive;
331     private String msMaxExclusive;
332     private int mnLength;
333     private int mnMinLength;
334     private int mnMaxLength;
335     private String msPattern;
336     private int mnFeatures;
337 }
338