| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package net.sourceforge.jeuclid.elements.support.operatordict; |
| 20 | |
|
| 21 | |
import java.io.IOException; |
| 22 | |
import java.io.InputStream; |
| 23 | |
import java.io.Serializable; |
| 24 | |
import java.util.EnumMap; |
| 25 | |
import java.util.Map; |
| 26 | |
import java.util.TreeMap; |
| 27 | |
|
| 28 | |
import javax.xml.parsers.ParserConfigurationException; |
| 29 | |
import javax.xml.parsers.SAXParserFactory; |
| 30 | |
|
| 31 | |
import net.sourceforge.jeuclid.elements.presentation.token.Mo; |
| 32 | |
|
| 33 | |
import org.apache.commons.logging.Log; |
| 34 | |
import org.apache.commons.logging.LogFactory; |
| 35 | |
import org.xml.sax.Attributes; |
| 36 | |
import org.xml.sax.InputSource; |
| 37 | |
import org.xml.sax.SAXException; |
| 38 | |
import org.xml.sax.XMLReader; |
| 39 | |
import org.xml.sax.helpers.DefaultHandler; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 0 | public final class OperatorDictionary2 extends AbstractOperatorDictionary |
| 47 | |
implements Serializable { |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | 209 | private static final Log LOGGER = LogFactory |
| 53 | |
.getLog(OperatorDictionary2.class); |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
private static final long serialVersionUID = 1L; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
private static final String DICTIONARY_FILE = "/net/sourceforge/jeuclid/moDictionary.xml"; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
private static final String DICTIONARY_SERIALIZED = "/net/sourceforge/jeuclid/moDictionary.ser"; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
private static OperatorDictionary instance; |
| 74 | |
|
| 75 | 0 | private OperatorDictionary2() { |
| 76 | |
|
| 77 | 0 | } |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
public static OperatorDictionary getInstance() { |
| 85 | 42700 | synchronized (OperatorDictionary2.class) { |
| 86 | 42700 | if (OperatorDictionary2.instance == null) { |
| 87 | 209 | final OperatorDictionary newDict = AbstractOperatorDictionary |
| 88 | |
.deserialize(OperatorDictionary2.DICTIONARY_SERIALIZED); |
| 89 | 209 | if (newDict == null) { |
| 90 | 0 | OperatorDictionary2.instance = new OperatorDictionary2(); |
| 91 | |
} else { |
| 92 | 209 | OperatorDictionary2.instance = newDict; |
| 93 | |
} |
| 94 | |
} |
| 95 | 42700 | } |
| 96 | 42700 | return OperatorDictionary2.instance; |
| 97 | |
} |
| 98 | |
|
| 99 | |
|
| 100 | |
@Override |
| 101 | |
protected void initializeFromXML( |
| 102 | |
final Map<OperatorAttribute, Map<String, Map<OperatorForm, String>>> dict) { |
| 103 | 0 | InputStream dictInput = null; |
| 104 | |
try { |
| 105 | 0 | dictInput = OperatorDictionary2.class |
| 106 | |
.getResourceAsStream(OperatorDictionary2.DICTIONARY_FILE); |
| 107 | 0 | final SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 108 | 0 | final XMLReader reader = factory.newSAXParser().getXMLReader(); |
| 109 | 0 | reader.setContentHandler(new DictionaryReader(dict)); |
| 110 | 0 | reader.parse(new InputSource(dictInput)); |
| 111 | 0 | } catch (final ParserConfigurationException e) { |
| 112 | 0 | OperatorDictionary2.LOGGER.warn("Cannot get SAXParser:" |
| 113 | |
+ e.getMessage()); |
| 114 | 0 | } catch (final SAXException e) { |
| 115 | 0 | OperatorDictionary2.LOGGER |
| 116 | |
.warn("SAXException while parsing dictionary:" |
| 117 | |
+ e.getMessage()); |
| 118 | 0 | } catch (final IOException e) { |
| 119 | 0 | OperatorDictionary2.LOGGER.warn( |
| 120 | |
"Read error while accessing XML dictionary", e); |
| 121 | |
} finally { |
| 122 | 0 | if (dictInput != null) { |
| 123 | |
try { |
| 124 | 0 | dictInput.close(); |
| 125 | 0 | } catch (final IOException io) { |
| 126 | 0 | OperatorDictionary2.LOGGER.warn( |
| 127 | |
"Error closing XML dictionary", io); |
| 128 | 0 | } |
| 129 | |
} |
| 130 | |
} |
| 131 | 0 | } |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
private class DictionaryReader extends DefaultHandler { |
| 138 | |
private static final String ELEMENT_ELEMENT = "element"; |
| 139 | |
|
| 140 | |
private String currentOperator; |
| 141 | |
|
| 142 | |
private OperatorForm currentFormIndex; |
| 143 | |
|
| 144 | |
private Map<OperatorAttribute, String> currentEntry; |
| 145 | |
private final Map<OperatorAttribute, Map<String, Map<OperatorForm, String>>> dict; |
| 146 | |
|
| 147 | |
public DictionaryReader( |
| 148 | 0 | final Map<OperatorAttribute, Map<String, Map<OperatorForm, String>>> d) { |
| 149 | |
|
| 150 | 0 | this.dict = d; |
| 151 | 0 | this.currentEntry = null; |
| 152 | 0 | } |
| 153 | |
|
| 154 | |
@Override |
| 155 | |
public void startDocument() throws SAXException { |
| 156 | |
|
| 157 | 0 | } |
| 158 | |
|
| 159 | |
@Override |
| 160 | |
public void endDocument() throws SAXException { |
| 161 | |
|
| 162 | 0 | } |
| 163 | |
|
| 164 | |
@Override |
| 165 | |
public void startElement(final String uri, final String localName, |
| 166 | |
final String rawName, final Attributes attlist) |
| 167 | |
throws SAXException { |
| 168 | |
|
| 169 | 0 | if (rawName |
| 170 | |
.equals(OperatorDictionary2.DictionaryReader.ELEMENT_ELEMENT)) { |
| 171 | 0 | this.currentEntry = new TreeMap<OperatorAttribute, String>(); |
| 172 | 0 | final String form = attlist.getValue(Mo.ATTR_FORM); |
| 173 | 0 | if (form == null) { |
| 174 | |
|
| 175 | |
|
| 176 | 0 | OperatorDictionary2.LOGGER |
| 177 | |
.fatal("Error in dictionary, attribute 'form' is required attribute for the dictionary"); |
| 178 | 0 | this.currentFormIndex = OperatorForm.INFIX; |
| 179 | |
} else { |
| 180 | 0 | this.currentFormIndex = OperatorForm |
| 181 | |
.parseOperatorForm(form); |
| 182 | |
} |
| 183 | 0 | for (int i = 0; i < attlist.getLength(); i++) { |
| 184 | 0 | final String attName = attlist.getQName(i); |
| 185 | 0 | final String attValue = attlist.getValue(i); |
| 186 | 0 | if (!attName.equals(Mo.ATTR_FORM)) { |
| 187 | |
try { |
| 188 | 0 | this.currentEntry.put(OperatorAttribute |
| 189 | |
.parseOperatorAttribute(attName), attValue); |
| 190 | 0 | } catch (final UnknownAttributeException e) { |
| 191 | 0 | OperatorDictionary2.LOGGER.fatal(e.getMessage()); |
| 192 | 0 | } |
| 193 | |
} |
| 194 | |
} |
| 195 | |
} |
| 196 | 0 | } |
| 197 | |
|
| 198 | |
@Override |
| 199 | |
public void endElement(final String uri, final String localName, |
| 200 | |
final String rawName) throws SAXException { |
| 201 | 0 | if (rawName |
| 202 | |
.equals(OperatorDictionary2.DictionaryReader.ELEMENT_ELEMENT)) { |
| 203 | |
|
| 204 | 0 | for (final Map.Entry<OperatorAttribute, String> attributeValues : this.currentEntry |
| 205 | |
.entrySet()) { |
| 206 | 0 | final OperatorAttribute attribute = attributeValues |
| 207 | |
.getKey(); |
| 208 | 0 | final String value = attributeValues.getValue(); |
| 209 | 0 | Map<String, Map<OperatorForm, String>> mapForAttr = this.dict |
| 210 | |
.get(attribute); |
| 211 | 0 | if (mapForAttr == null) { |
| 212 | 0 | mapForAttr = new TreeMap<String, Map<OperatorForm, String>>(); |
| 213 | 0 | this.dict.put(attribute, mapForAttr); |
| 214 | |
} |
| 215 | 0 | Map<OperatorForm, String> valueForForm = mapForAttr |
| 216 | |
.get(this.currentOperator); |
| 217 | 0 | if (valueForForm == null) { |
| 218 | 0 | valueForForm = new EnumMap<OperatorForm, String>( |
| 219 | |
OperatorForm.class); |
| 220 | 0 | mapForAttr.put(this.currentOperator, valueForForm); |
| 221 | |
} |
| 222 | 0 | valueForForm.put(this.currentFormIndex, value); |
| 223 | 0 | } |
| 224 | |
} |
| 225 | 0 | this.currentEntry = null; |
| 226 | 0 | this.currentOperator = null; |
| 227 | 0 | } |
| 228 | |
|
| 229 | |
@Override |
| 230 | |
public void characters(final char[] data, final int start, |
| 231 | |
final int length) throws SAXException { |
| 232 | 0 | if (this.currentEntry != null) { |
| 233 | 0 | final char[] temp = new char[length]; |
| 234 | 0 | System.arraycopy(data, start, temp, 0, length); |
| 235 | 0 | if (this.currentOperator == null) { |
| 236 | 0 | this.currentOperator = new String(temp); |
| 237 | |
} else { |
| 238 | 0 | this.currentOperator += new String(temp); |
| 239 | |
} |
| 240 | 0 | this.currentOperator = this.currentOperator.trim(); |
| 241 | |
} |
| 242 | 0 | } |
| 243 | |
} |
| 244 | |
|
| 245 | |
} |