KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > xmi > impl > XMLParserPoolImpl


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: XMLParserPoolImpl.java,v 1.3 2005/06/08 06:16:07 nickb Exp $
16  */

17
18 package org.eclipse.emf.ecore.xmi.impl;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import javax.xml.parsers.ParserConfigurationException JavaDoc;
25 import javax.xml.parsers.SAXParser JavaDoc;
26 import javax.xml.parsers.SAXParserFactory JavaDoc;
27 import org.eclipse.emf.ecore.xmi.XMLParserPool;
28 import org.eclipse.emf.ecore.xmi.XMLResource;
29 import org.xml.sax.SAXException JavaDoc;
30
31 /**
32  * This is the default implementation of XMLParserPool. Note: this
33  * implementation is not thread safe.
34  */

35 public class XMLParserPoolImpl implements XMLParserPool
36 {
37
38   private final HashMap JavaDoc parserCache = new HashMap JavaDoc();
39
40   /**
41    * @see XMLParserPool#get(Map, Map, boolean)
42    */

43   public synchronized SAXParser JavaDoc get(Map JavaDoc features, Map JavaDoc properties, boolean useLexicalHandler)
44       throws ParserConfigurationException JavaDoc, SAXException JavaDoc
45   {
46     Map JavaDoc map = new HashMap JavaDoc();
47     map.putAll(features);
48     map.putAll(properties);
49     map.put(XMLResource.OPTION_USE_LEXICAL_HANDLER, useLexicalHandler ? Boolean.TRUE : Boolean.FALSE);
50     Object JavaDoc o = parserCache.get(map);
51     if (o != null)
52     {
53       ArrayList JavaDoc list = (ArrayList JavaDoc) o;
54       int size = list.size();
55       if (size > 0)
56       {
57         return (SAXParser JavaDoc) list.remove(size - 1);
58       }
59       else
60       {
61         return makeParser(features, properties);
62       }
63     }
64     else
65     {
66       parserCache.put(map, new ArrayList JavaDoc());
67       return makeParser(features, properties);
68     }
69   }
70
71   /**
72    * @see XMLParserPool#release(SAXParser, Map, Map, boolean)
73    */

74   public synchronized void release(SAXParser JavaDoc parser, Map JavaDoc features, Map JavaDoc properties, boolean useLexicalHandler)
75   {
76     Map JavaDoc map = new HashMap JavaDoc();
77     map.putAll(features);
78     map.putAll(properties);
79     map.put(XMLResource.OPTION_USE_LEXICAL_HANDLER, useLexicalHandler ? Boolean.TRUE : Boolean.FALSE);
80     ArrayList JavaDoc list = (ArrayList JavaDoc) parserCache.get(map);
81     list.add(parser);
82   }
83
84   private SAXParser JavaDoc makeParser(Map JavaDoc features, Map JavaDoc properties)
85       throws ParserConfigurationException JavaDoc, SAXException JavaDoc
86   {
87     SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
88     SAXParser JavaDoc parser = factory.newSAXParser();
89     // set parser features and properties
90
if (features != null)
91     {
92       for (Iterator JavaDoc i = features.keySet().iterator(); i.hasNext();)
93       {
94         String JavaDoc feature = (String JavaDoc) i.next();
95         parser.getXMLReader().setFeature(feature,
96             ((Boolean JavaDoc) features.get(feature)).booleanValue());
97       }
98     }
99     if (properties != null)
100     {
101       for (Iterator JavaDoc i = properties.keySet().iterator(); i.hasNext();)
102       {
103         String JavaDoc property = (String JavaDoc) i.next();
104         parser.getXMLReader().setProperty(property, properties.get(property));
105       }
106     }
107     return parser;
108   }
109 }
110
Popular Tags