001 /*
002 * Copyright 2002 - 2008 JEuclid, http://jeuclid.sf.net
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 /* $Id: AbstractOperatorDictionary.java,v c75d8b379394 2009/09/25 20:03:08 max $ */
018
019 package net.sourceforge.jeuclid.elements.support.operatordict;
020
021 import java.io.IOException;
022 import java.io.InputStream;
023 import java.io.ObjectInput;
024 import java.io.ObjectInputStream;
025 import java.io.Serializable;
026 import java.util.EnumMap;
027 import java.util.Map;
028
029 /**
030 * Read default values of operators from xml file.
031 *
032 * @version $Revision: c75d8b379394 $
033 */
034 public abstract class AbstractOperatorDictionary implements OperatorDictionary,
035 Serializable {
036
037 /**
038 *
039 */
040 private static final long serialVersionUID = 1L;
041
042 private final Map<OperatorAttribute, Map<String, Map<OperatorForm, String>>> dict;
043
044 /**
045 * Default constructor.
046 */
047 protected AbstractOperatorDictionary() {
048 this.dict = new EnumMap<OperatorAttribute, Map<String, Map<OperatorForm, String>>>(
049 OperatorAttribute.class);
050 this.initializeFromXML(this.dict);
051 this.overrideStretchy();
052 }
053
054 private void overrideStretchy() {
055 final Map<String, Map<OperatorForm, String>> opmap = this.dict
056 .get(OperatorAttribute.STRETCHY);
057 for (final Map.Entry<String, Map<OperatorForm, String>> e : opmap
058 .entrySet()) {
059 final String override = StretchOverride.getStretchOverride(e
060 .getKey());
061 if (override != null) {
062 for (final Map.Entry<OperatorForm, String> e2 : e.getValue()
063 .entrySet()) {
064 if (Boolean.parseBoolean(e2.getValue())) {
065 e2.setValue(override);
066 }
067 }
068 }
069 }
070 }
071
072 /**
073 * Get the for singleton instance.
074 *
075 * @param path
076 * path for the serialized object.
077 * @return an instance of OperatorDictionary.
078 */
079 protected static OperatorDictionary deserialize(final String path) {
080 OperatorDictionary newDict = null;
081 try {
082 final InputStream is = AbstractOperatorDictionary.class
083 .getResourceAsStream(path);
084 final ObjectInput oi = new ObjectInputStream(is);
085 newDict = (AbstractOperatorDictionary) oi.readObject();
086 oi.close();
087 } catch (final ClassNotFoundException cnfe) {
088 newDict = null;
089 } catch (final IllegalArgumentException e) {
090 newDict = null;
091 } catch (final IOException e) {
092 newDict = null;
093 } catch (final NullPointerException e) {
094 newDict = null;
095 }
096 return newDict;
097 }
098
099 /**
100 * Initializes Dictionary.
101 *
102 * @param d
103 * the dictionary to initialize.
104 */
105 protected abstract void initializeFromXML(
106 Map<OperatorAttribute, Map<String, Map<OperatorForm, String>>> d);
107
108 /**
109 * Determines default value of the operator attribute.
110 *
111 * @param operator
112 * operator character
113 * @param form
114 * form string
115 * @param attributeName
116 * name of attribute
117 * @return VALUE_UNKOWN or value from dict.
118 * @throws UnknownAttributeException
119 * Raised, if wrong attributeName was provided.
120 */
121 public String getDefaultAttributeValue(final String operator,
122 final String form, final String attributeName)
123 throws UnknownAttributeException {
124 final OperatorForm intForm = OperatorForm.parseOperatorForm(form);
125 return this.getDefaultAttributeValue(operator, intForm,
126 OperatorAttribute.parseOperatorAttribute(attributeName));
127 }
128
129 /**
130 * Determines default value of the operator attribute.
131 *
132 * @param operator
133 * Operator character.
134 * @param form
135 * Form value
136 * @param attributeName
137 * Name of the attribute.
138 * @return Default value (VALUE_UNKNOWN, if default value has not been
139 * provided yet).
140 * @throws UnknownAttributeException
141 * Raised, if wrong attributeName was provided.
142 */
143 private String getDefaultAttributeValue(final String operator,
144 final OperatorForm form, final OperatorAttribute attribute) {
145
146 final Map<String, Map<OperatorForm, String>> opForAttr = this.dict
147 .get(attribute);
148 if (opForAttr == null) {
149 return attribute.getDefaultValue();
150 }
151 final Map<OperatorForm, String> valuesPerForm = opForAttr.get(operator);
152 String retVal;
153 if (valuesPerForm == null) {
154 retVal = attribute.getDefaultValue();
155 } else {
156 retVal = valuesPerForm.get(form);
157 if (retVal == null) {
158 retVal = valuesPerForm.get(OperatorForm.INFIX);
159 }
160 if (retVal == null) {
161 retVal = valuesPerForm.get(OperatorForm.POSTFIX);
162 }
163 if (retVal == null) {
164 retVal = valuesPerForm.get(OperatorForm.PREFIX);
165 }
166 if (retVal == null) {
167 retVal = attribute.getDefaultValue();
168 }
169 }
170 return retVal;
171 }
172
173 }