001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.util;
011:
012: import java.lang.ref.SoftReference;
013: import java.util.Collections;
014: import java.util.LinkedList;
015: import java.util.List;
016:
017: import javax.xml.parsers.DocumentBuilder;
018: import javax.xml.parsers.DocumentBuilderFactory;
019: import javax.xml.parsers.FactoryConfigurationError;
020: import javax.xml.parsers.ParserConfigurationException;
021: import javax.xml.parsers.SAXParser;
022: import javax.xml.parsers.SAXParserFactory;
023:
024: import org.xml.sax.SAXException;
025:
026: /**
027: * PDEXMLHelper
028: *
029: */
030: public class PDEXMLHelper {
031:
032: protected static SAXParserFactory fSAXFactory;
033: protected static PDEXMLHelper fPinstance;
034: protected static DocumentBuilderFactory fDOMFactory;
035: protected static List fSAXParserQueue;
036: protected static List fDOMParserQueue;
037: protected static int fSAXPoolLimit;
038: protected static int fDOMPoolLimit;
039: protected static final int FMAXPOOLLIMIT = 1;
040:
041: protected PDEXMLHelper() throws FactoryConfigurationError {
042: fSAXFactory = SAXParserFactory.newInstance();
043: fDOMFactory = DocumentBuilderFactory.newInstance();
044: fSAXParserQueue = Collections
045: .synchronizedList(new LinkedList());
046: fDOMParserQueue = Collections
047: .synchronizedList(new LinkedList());
048: fSAXPoolLimit = FMAXPOOLLIMIT;
049: fDOMPoolLimit = FMAXPOOLLIMIT;
050: }
051:
052: public synchronized SAXParser getDefaultSAXParser()
053: throws ParserConfigurationException, SAXException {
054:
055: SAXParser parser = null;
056: if (fSAXParserQueue.isEmpty()) {
057: parser = fSAXFactory.newSAXParser();
058: } else {
059: SoftReference reference = (SoftReference) fSAXParserQueue
060: .remove(0);
061: if (reference.get() != null) {
062: parser = (SAXParser) reference.get();
063: } else {
064: parser = fSAXFactory.newSAXParser();
065: }
066: }
067: return parser;
068: }
069:
070: public synchronized DocumentBuilder getDefaultDOMParser()
071: throws ParserConfigurationException {
072:
073: DocumentBuilder parser = null;
074: if (fDOMParserQueue.isEmpty()) {
075: parser = fDOMFactory.newDocumentBuilder();
076: } else {
077: SoftReference reference = (SoftReference) fDOMParserQueue
078: .remove(0);
079: if (reference.get() != null) {
080: parser = (DocumentBuilder) reference.get();
081: } else {
082: parser = fDOMFactory.newDocumentBuilder();
083: }
084: }
085: return parser;
086: }
087:
088: public static PDEXMLHelper Instance()
089: throws FactoryConfigurationError {
090: if (fPinstance == null) {
091: fPinstance = new PDEXMLHelper();
092: }
093: return fPinstance;
094: }
095:
096: public synchronized void recycleSAXParser(SAXParser parser) {
097: if (fSAXParserQueue.size() < fSAXPoolLimit) {
098: SoftReference reference = new SoftReference(parser);
099: fSAXParserQueue.add(reference);
100: }
101: }
102:
103: public synchronized void recycleDOMParser(DocumentBuilder parser) {
104: if (fDOMParserQueue.size() < fDOMPoolLimit) {
105: SoftReference reference = new SoftReference(parser);
106: fDOMParserQueue.add(reference);
107: }
108: }
109:
110: public static String getWritableString(String source) {
111: if (source == null)
112: return ""; //$NON-NLS-1$
113: StringBuffer buf = new StringBuffer();
114: for (int i = 0; i < source.length(); i++) {
115: char c = source.charAt(i);
116: switch (c) {
117: case '&':
118: buf.append("&"); //$NON-NLS-1$
119: break;
120: case '<':
121: buf.append("<"); //$NON-NLS-1$
122: break;
123: case '>':
124: buf.append(">"); //$NON-NLS-1$
125: break;
126: case '\'':
127: buf.append("'"); //$NON-NLS-1$
128: break;
129: case '\"':
130: buf.append("""); //$NON-NLS-1$
131: break;
132: default:
133: buf.append(c);
134: break;
135: }
136: }
137: return buf.toString();
138: }
139:
140: /**
141: * @param source
142: * @return
143: */
144: public static String getWritableAttributeString(String source) {
145: // Ensure source is defined
146: if (source == null) {
147: return ""; //$NON-NLS-1$
148: }
149: // Trim the leading and trailing whitespace if any
150: source = source.trim();
151: // Translate source using a buffer
152: StringBuffer buffer = new StringBuffer();
153: // Translate source character by character
154: for (int i = 0; i < source.length(); i++) {
155: char character = source.charAt(i);
156: switch (character) {
157: case '&':
158: buffer.append("&"); //$NON-NLS-1$
159: break;
160: case '<':
161: buffer.append("<"); //$NON-NLS-1$
162: break;
163: case '>':
164: buffer.append(">"); //$NON-NLS-1$
165: break;
166: case '\'':
167: buffer.append("'"); //$NON-NLS-1$
168: break;
169: case '\"':
170: buffer.append("""); //$NON-NLS-1$
171: break;
172: case '\r':
173: buffer.append("
"); //$NON-NLS-1$
174: break;
175: case '\n':
176: buffer.append("
"); //$NON-NLS-1$
177: break;
178: default:
179: buffer.append(character);
180: break;
181: }
182: }
183: return buffer.toString();
184: }
185:
186: public static int getSAXPoolLimit() {
187: return fSAXPoolLimit;
188: }
189:
190: public static void setSAXPoolLimit(int poolLimit) {
191: fSAXPoolLimit = poolLimit;
192: }
193:
194: public static int getDOMPoolLimit() {
195: return fDOMPoolLimit;
196: }
197:
198: public static void setDOMPoolLimit(int poolLimit) {
199: fDOMPoolLimit = poolLimit;
200: }
201:
202: }
|