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.HashMap;
25 import java.util.Map;
26 import java.util.Vector;
27 
28 import org.apache.openoffice.ooxml.parser.NameMap;
29 import org.apache.openoffice.ooxml.parser.attribute.AttributeDescriptor;
30 
31 public class SimpleTypeManager
32 {
SimpleTypeManager( final Vector<String[]> aData, final NameMap aAttributeValueMap)33     public SimpleTypeManager(
34         final Vector<String[]> aData,
35         final NameMap aAttributeValueMap)
36     {
37         maAttributeValueMap = aAttributeValueMap;
38         maSimpleTypeToParsersMap = new HashMap<>();
39         ParseData(aData);
40     }
41 
42 
43 
44 
ParseData(final Vector<String[]> aData)45     private void ParseData (final Vector<String[]> aData)
46     {
47         for (final String[] aLine : aData)
48         {
49             final int nSimpleTypeId = Integer.parseInt(aLine[1]);
50 //            final int nVariant = Integer.parseInt(aLine[2]);
51             final boolean bIsList = aLine[3].equals("L");
52             final ISimpleTypeParser aVariantParser;
53             switch (aLine[4])
54             {
55                 case "S":
56                     aVariantParser = new StringParser(aLine);
57                     break;
58                 case "N":
59                     aVariantParser = new NumberParser(aLine);
60                     break;
61                 case "D":
62                     aVariantParser = new DateTimeParser(aLine);
63                     break;
64                 case "B":
65                     aVariantParser = new BlobParser(aLine);
66                     break;
67                 default:
68                     throw new RuntimeException("unexpected parser type: "+aLine[4]);
69             }
70 
71             Vector<ISimpleTypeParser> aVariants = maSimpleTypeToParsersMap.get(nSimpleTypeId);
72             if (aVariants == null)
73             {
74                 aVariants = new Vector<>();
75                 maSimpleTypeToParsersMap.put(nSimpleTypeId, aVariants);
76             }
77             if (bIsList)
78                 aVariants.add(new ListParser(aVariantParser));
79             else
80                 aVariants.add(aVariantParser);
81         }
82     }
83 
84 
85 
86 
PreprocessValue( final String sRawValue, final AttributeDescriptor aAttributeDescriptor)87     public Object PreprocessValue (
88         final String sRawValue,
89         final AttributeDescriptor aAttributeDescriptor)
90     {
91         final Vector<ISimpleTypeParser> aTypeParsers = maSimpleTypeToParsersMap.get(aAttributeDescriptor.GetTypeId());
92         if (aTypeParsers == null)
93             throw new RuntimeException("type "+aAttributeDescriptor.GetTypeId()+" is not supported");
94 
95         for (final ISimpleTypeParser aParser : aTypeParsers)
96         {
97             try
98             {
99                 final Object aProcessedValue = aParser.Parse(
100                     sRawValue,
101                     maAttributeValueMap);
102                 if (aProcessedValue != null)
103                     return aProcessedValue;
104             }
105             catch(final Exception aException)
106             {
107                 return "error";
108             }
109         }
110         return null;
111     }
112 
113 
114 
115 
116     private final NameMap maAttributeValueMap;
117     private Map<Integer,Vector<ISimpleTypeParser>> maSimpleTypeToParsersMap;
118 }
119