1 17 18 package org.eclipse.emf.ecore.xmi.impl; 19 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import javax.xml.parsers.ParserConfigurationException ; 25 import javax.xml.parsers.SAXParser ; 26 import javax.xml.parsers.SAXParserFactory ; 27 import org.eclipse.emf.ecore.xmi.XMLParserPool; 28 import org.eclipse.emf.ecore.xmi.XMLResource; 29 import org.xml.sax.SAXException ; 30 31 35 public class XMLParserPoolImpl implements XMLParserPool 36 { 37 38 private final HashMap parserCache = new HashMap (); 39 40 43 public synchronized SAXParser get(Map features, Map properties, boolean useLexicalHandler) 44 throws ParserConfigurationException , SAXException 45 { 46 Map map = new HashMap (); 47 map.putAll(features); 48 map.putAll(properties); 49 map.put(XMLResource.OPTION_USE_LEXICAL_HANDLER, useLexicalHandler ? Boolean.TRUE : Boolean.FALSE); 50 Object o = parserCache.get(map); 51 if (o != null) 52 { 53 ArrayList list = (ArrayList ) o; 54 int size = list.size(); 55 if (size > 0) 56 { 57 return (SAXParser ) list.remove(size - 1); 58 } 59 else 60 { 61 return makeParser(features, properties); 62 } 63 } 64 else 65 { 66 parserCache.put(map, new ArrayList ()); 67 return makeParser(features, properties); 68 } 69 } 70 71 74 public synchronized void release(SAXParser parser, Map features, Map properties, boolean useLexicalHandler) 75 { 76 Map map = new HashMap (); 77 map.putAll(features); 78 map.putAll(properties); 79 map.put(XMLResource.OPTION_USE_LEXICAL_HANDLER, useLexicalHandler ? Boolean.TRUE : Boolean.FALSE); 80 ArrayList list = (ArrayList ) parserCache.get(map); 81 list.add(parser); 82 } 83 84 private SAXParser makeParser(Map features, Map properties) 85 throws ParserConfigurationException , SAXException 86 { 87 SAXParserFactory factory = SAXParserFactory.newInstance(); 88 SAXParser parser = factory.newSAXParser(); 89 if (features != null) 91 { 92 for (Iterator i = features.keySet().iterator(); i.hasNext();) 93 { 94 String feature = (String ) i.next(); 95 parser.getXMLReader().setFeature(feature, 96 ((Boolean ) features.get(feature)).booleanValue()); 97 } 98 } 99 if (properties != null) 100 { 101 for (Iterator i = properties.keySet().iterator(); i.hasNext();) 102 { 103 String property = (String ) i.next(); 104 parser.getXMLReader().setProperty(property, properties.get(property)); 105 } 106 } 107 return parser; 108 } 109 } 110 | Popular Tags |