KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > util > PDEXMLHelper


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.core.util;
13
14 import java.lang.ref.SoftReference JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.LinkedList JavaDoc;
17 import java.util.List JavaDoc;
18
19 import javax.xml.parsers.DocumentBuilder JavaDoc;
20 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
21 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
22 import javax.xml.parsers.ParserConfigurationException JavaDoc;
23 import javax.xml.parsers.SAXParser JavaDoc;
24 import javax.xml.parsers.SAXParserFactory JavaDoc;
25
26 import org.xml.sax.SAXException JavaDoc;
27
28
29 /**
30  * PDEXMLHelper
31  *
32  */

33 public class PDEXMLHelper {
34
35     protected static SAXParserFactory JavaDoc fSAXFactory;
36     protected static PDEXMLHelper fPinstance;
37     protected static DocumentBuilderFactory JavaDoc fDOMFactory;
38     protected static List JavaDoc fSAXParserQueue;
39     protected static List JavaDoc fDOMParserQueue;
40     protected static int fSAXPoolLimit;
41     protected static int fDOMPoolLimit;
42     protected static final int FMAXPOOLLIMIT = 1;
43     
44     protected PDEXMLHelper() throws FactoryConfigurationError JavaDoc {
45         fSAXFactory = SAXParserFactory.newInstance();
46         fDOMFactory = DocumentBuilderFactory.newInstance();
47         fSAXParserQueue = Collections.synchronizedList(new LinkedList JavaDoc());
48         fDOMParserQueue = Collections.synchronizedList(new LinkedList JavaDoc());
49         fSAXPoolLimit = FMAXPOOLLIMIT;
50         fDOMPoolLimit = FMAXPOOLLIMIT;
51     }
52     
53     public synchronized SAXParser JavaDoc getDefaultSAXParser() throws ParserConfigurationException JavaDoc, SAXException JavaDoc {
54
55         SAXParser JavaDoc parser = null;
56         if (fSAXParserQueue.isEmpty()) {
57             parser = fSAXFactory.newSAXParser();
58         } else {
59             SoftReference JavaDoc reference = (SoftReference JavaDoc)fSAXParserQueue.remove(0);
60             if (reference.get() != null) {
61                 parser = (SAXParser JavaDoc)reference.get();
62             } else {
63                 parser = fSAXFactory.newSAXParser();
64             }
65         }
66         return parser;
67     }
68
69     public synchronized DocumentBuilder JavaDoc getDefaultDOMParser() throws ParserConfigurationException JavaDoc {
70
71         DocumentBuilder JavaDoc parser = null;
72         if (fDOMParserQueue.isEmpty()) {
73             parser = fDOMFactory.newDocumentBuilder();
74         } else {
75             SoftReference JavaDoc reference = (SoftReference JavaDoc)fDOMParserQueue.remove(0);
76             if (reference.get() != null) {
77                 parser = (DocumentBuilder JavaDoc)reference.get();
78             } else {
79                 parser = fDOMFactory.newDocumentBuilder();
80             }
81         }
82         return parser;
83     }
84     
85     public static PDEXMLHelper Instance() throws FactoryConfigurationError JavaDoc {
86         if (fPinstance == null) {
87             fPinstance = new PDEXMLHelper();
88         }
89         return fPinstance;
90     }
91     
92     public synchronized void recycleSAXParser(SAXParser JavaDoc parser) {
93         if (fSAXParserQueue.size() < fSAXPoolLimit) {
94             SoftReference JavaDoc reference = new SoftReference JavaDoc(parser);
95             fSAXParserQueue.add(reference);
96         }
97     }
98
99     public synchronized void recycleDOMParser(DocumentBuilder JavaDoc parser) {
100         if (fDOMParserQueue.size() < fDOMPoolLimit) {
101             SoftReference JavaDoc reference = new SoftReference JavaDoc(parser);
102             fDOMParserQueue.add(reference);
103         }
104     }
105
106     public static String JavaDoc getWritableString(String JavaDoc source) {
107         if (source == null)
108             return ""; //$NON-NLS-1$
109
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
110         for (int i = 0; i < source.length(); i++) {
111             char c = source.charAt(i);
112             switch (c) {
113                 case '&' :
114                     buf.append("&amp;"); //$NON-NLS-1$
115
break;
116                 case '<' :
117                     buf.append("&lt;"); //$NON-NLS-1$
118
break;
119                 case '>' :
120                     buf.append("&gt;"); //$NON-NLS-1$
121
break;
122                 case '\'' :
123                     buf.append("&apos;"); //$NON-NLS-1$
124
break;
125                 case '\"' :
126                     buf.append("&quot;"); //$NON-NLS-1$
127
break;
128                 default :
129                     buf.append(c);
130                     break;
131             }
132         }
133         return buf.toString();
134     }
135
136     public static int getSAXPoolLimit() {
137         return fSAXPoolLimit;
138     }
139
140     public static void setSAXPoolLimit(int poolLimit) {
141         fSAXPoolLimit = poolLimit;
142     }
143
144     public static int getDOMPoolLimit() {
145         return fDOMPoolLimit;
146     }
147
148     public static void setDOMPoolLimit(int poolLimit) {
149         fDOMPoolLimit = poolLimit;
150     }
151
152
153 }
154
Popular Tags