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.complex;
23 
24 import org.apache.openoffice.ooxml.schema.model.base.INodeVisitor;
25 import org.apache.openoffice.ooxml.schema.model.base.Location;
26 import org.apache.openoffice.ooxml.schema.model.base.Node;
27 import org.apache.openoffice.ooxml.schema.model.base.NodeType;
28 
29 /** An occurrence indicator is based on a minimum and a maximum value.
30  *  The minimum value is 0 or larger.  If it is 0 then the child node is optional.
31  *  The maximum value is at least as large as the minimum value.
32  *  It can be 'unbounded' in which case the child node can appear any number of times.
33  *  'Unbounded' is internally stored as -1.
34  *
35  *  An OccurrenceIndicator represents the minOccur and maxOccur attributes of
36  *  XML schema elements.
37  *
38  *  There is usually exactly one child.
39  */
40 public class OccurrenceIndicator
41     extends Node
42 {
43     public static int unbounded = -1;
44 
OccurrenceIndicator( final Node aParent, final String sMinimum, final String sMaximum, final Location aLocation)45     public OccurrenceIndicator (
46         final Node aParent,
47         final String sMinimum,
48         final String sMaximum,
49         final Location aLocation)
50     {
51         super(aParent, null, aLocation);
52 
53         mnMinimum = ParseValue(sMinimum);
54         mnMaximum = ParseValue(sMaximum);
55 
56         assert(mnMinimum>=0);
57         assert(mnMaximum==unbounded || mnMaximum>=mnMinimum);
58     }
59 
60 
61 
62 
63     @Override
GetNodeType()64     public NodeType GetNodeType ()
65     {
66         return NodeType.OccurrenceIndicator;
67     }
68 
69 
70 
71 
72     /** Return a string version of the minimum value for textual display.
73      */
GetDisplayMinimum()74     public String GetDisplayMinimum ()
75     {
76         return Integer.toString(mnMinimum);
77     }
78 
79 
80 
81 
82     /** Return a string version of the maximum value for textual display.
83      */
GetDisplayMaximum()84     public String GetDisplayMaximum ()
85     {
86         if (mnMaximum == unbounded)
87             return "unbounded";
88         else
89             return Integer.toString(mnMaximum);
90     }
91 
92 
93 
94 
GetMinimum()95     public int GetMinimum ()
96     {
97         return mnMinimum;
98     }
99 
100 
101 
102 
GetMaximum()103     public int GetMaximum ()
104     {
105         return mnMaximum;
106     }
107 
108 
109 
110 
111     @Override
AcceptVisitor(final INodeVisitor aVisitor)112     public void AcceptVisitor (final INodeVisitor aVisitor)
113     {
114         aVisitor.Visit(this);
115     }
116 
117 
118 
119 
120     @Override
toString()121     public String toString ()
122     {
123         return String.format("occurrence %s to %s",
124             GetDisplayMinimum(),
125             GetDisplayMaximum());
126     }
127 
128 
129 
130 
ParseValue(final String sValue)131     private static int ParseValue (final String sValue)
132     {
133         if (sValue == null)
134         {
135             // Missing values default to 1.
136             return 1;
137         }
138         else
139             switch (sValue)
140             {
141                 case "0" : return 0;
142                 case "1" : return 1;
143                 case "unbounded" : return unbounded;
144                 default: return Integer.parseInt(sValue);
145             }
146     }
147 
148 
149 
150     private final int mnMinimum;
151     private final int mnMaximum;
152 }
153