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.parser.type;
23 
24 import java.util.HashSet;
25 import java.util.Set;
26 import java.util.regex.Pattern;
27 
28 import org.apache.openoffice.ooxml.parser.NameMap;
29 
30 public class StringParser
31     implements ISimpleTypeParser
32 {
33 
StringParser(final String[] aLine)34     public StringParser (final String[] aLine)
35     {
36         switch(aLine[5])
37         {
38             case "E":
39                 meRestrictionType = RestrictionType.Enumeration;
40                 maEnumeration = new HashSet<>();
41                 for (int nIndex=6; nIndex<aLine.length; ++nIndex)
42                     maEnumeration.add(Integer.parseInt(aLine[nIndex]));
43                 break;
44 
45             case "P":
46                 meRestrictionType = RestrictionType.Pattern;
47                 maPattern = Pattern.compile(aLine[6].replace("\\p{Is", "\\p{In"));
48                 break;
49 
50             case "L":
51                 meRestrictionType = RestrictionType.Length;
52                 mnMinimumLength = Integer.parseInt(aLine[6]);
53                 mnMaximumLength = Integer.parseInt(aLine[7]);
54                 break;
55 
56             case "N":
57                 meRestrictionType = RestrictionType.None;
58                 break;
59 
60             default:
61                 throw new RuntimeException();
62         }
63     }
64 
65 
66 
67 
68     @Override
Parse( final String sRawValue, final NameMap aAttributeValueMap)69     public Object Parse (
70         final String sRawValue,
71         final NameMap aAttributeValueMap)
72     {
73         switch(meRestrictionType)
74         {
75             case Enumeration:
76                 final int nId = aAttributeValueMap.GetIdForOptionalName(sRawValue);
77                 if ( ! maEnumeration.contains(nId))
78                     return null;//throw new RuntimeException("value is not part of enumeration");
79                 else
80                     return nId;
81 
82             case Pattern:
83                 if ( ! maPattern.matcher(sRawValue).matches())
84                     return null;//throw new RuntimeException("value does not match pattern");
85                 else
86                     return sRawValue;
87 
88             case Length:
89                 if (sRawValue.length()<mnMinimumLength || sRawValue.length()>mnMaximumLength)
90                     return null;/*throw new RuntimeException(
91                         String.format("value violates string length restriction: %s is not inside [%d,%d]",
92                             sRawValue.length(),
93                             mnMinimumLength,
94                             mnMaximumLength));
95                             */
96                 else
97                     return sRawValue;
98 
99             case None:
100                 return sRawValue;
101 
102             default:
103                 throw new RuntimeException();
104         }
105     }
106 
107 
108 
109 
110     enum RestrictionType
111     {
112         Enumeration,
113         Pattern,
114         Length,
115         None
116     }
117     private final RestrictionType meRestrictionType;
118     private Set<Integer> maEnumeration;
119     private Pattern maPattern;
120     private int mnMinimumLength;
121     private int mnMaximumLength;
122 }
123