1 11 12 package org.eclipse.pde.internal.core.util; 13 14 import java.lang.ref.SoftReference ; 15 import java.util.Collections ; 16 import java.util.LinkedList ; 17 import java.util.List ; 18 19 import javax.xml.parsers.DocumentBuilder ; 20 import javax.xml.parsers.DocumentBuilderFactory ; 21 import javax.xml.parsers.FactoryConfigurationError ; 22 import javax.xml.parsers.ParserConfigurationException ; 23 import javax.xml.parsers.SAXParser ; 24 import javax.xml.parsers.SAXParserFactory ; 25 26 import org.xml.sax.SAXException ; 27 28 29 33 public class PDEXMLHelper { 34 35 protected static SAXParserFactory fSAXFactory; 36 protected static PDEXMLHelper fPinstance; 37 protected static DocumentBuilderFactory fDOMFactory; 38 protected static List fSAXParserQueue; 39 protected static List fDOMParserQueue; 40 protected static int fSAXPoolLimit; 41 protected static int fDOMPoolLimit; 42 protected static final int FMAXPOOLLIMIT = 1; 43 44 protected PDEXMLHelper() throws FactoryConfigurationError { 45 fSAXFactory = SAXParserFactory.newInstance(); 46 fDOMFactory = DocumentBuilderFactory.newInstance(); 47 fSAXParserQueue = Collections.synchronizedList(new LinkedList ()); 48 fDOMParserQueue = Collections.synchronizedList(new LinkedList ()); 49 fSAXPoolLimit = FMAXPOOLLIMIT; 50 fDOMPoolLimit = FMAXPOOLLIMIT; 51 } 52 53 public synchronized SAXParser getDefaultSAXParser() throws ParserConfigurationException , SAXException { 54 55 SAXParser parser = null; 56 if (fSAXParserQueue.isEmpty()) { 57 parser = fSAXFactory.newSAXParser(); 58 } else { 59 SoftReference reference = (SoftReference )fSAXParserQueue.remove(0); 60 if (reference.get() != null) { 61 parser = (SAXParser )reference.get(); 62 } else { 63 parser = fSAXFactory.newSAXParser(); 64 } 65 } 66 return parser; 67 } 68 69 public synchronized DocumentBuilder getDefaultDOMParser() throws ParserConfigurationException { 70 71 DocumentBuilder parser = null; 72 if (fDOMParserQueue.isEmpty()) { 73 parser = fDOMFactory.newDocumentBuilder(); 74 } else { 75 SoftReference reference = (SoftReference )fDOMParserQueue.remove(0); 76 if (reference.get() != null) { 77 parser = (DocumentBuilder )reference.get(); 78 } else { 79 parser = fDOMFactory.newDocumentBuilder(); 80 } 81 } 82 return parser; 83 } 84 85 public static PDEXMLHelper Instance() throws FactoryConfigurationError { 86 if (fPinstance == null) { 87 fPinstance = new PDEXMLHelper(); 88 } 89 return fPinstance; 90 } 91 92 public synchronized void recycleSAXParser(SAXParser parser) { 93 if (fSAXParserQueue.size() < fSAXPoolLimit) { 94 SoftReference reference = new SoftReference (parser); 95 fSAXParserQueue.add(reference); 96 } 97 } 98 99 public synchronized void recycleDOMParser(DocumentBuilder parser) { 100 if (fDOMParserQueue.size() < fDOMPoolLimit) { 101 SoftReference reference = new SoftReference (parser); 102 fDOMParserQueue.add(reference); 103 } 104 } 105 106 public static String getWritableString(String source) { 107 if (source == null) 108 return ""; StringBuffer buf = new StringBuffer (); 110 for (int i = 0; i < source.length(); i++) { 111 char c = source.charAt(i); 112 switch (c) { 113 case '&' : 114 buf.append("&"); break; 116 case '<' : 117 buf.append("<"); break; 119 case '>' : 120 buf.append(">"); break; 122 case '\'' : 123 buf.append("'"); break; 125 case '\"' : 126 buf.append("""); 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 |