1 package net.sourceforge.phpdt.internal.launching;
3 import java.io.BufferedOutputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.FileReader;
8 import java.io.IOException;
9 import java.io.OutputStream;
10 import java.io.OutputStreamWriter;
11 import java.io.Reader;
12 import java.io.StringReader;
13 import java.io.Writer;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
18 import javax.xml.parsers.SAXParserFactory;
20 import org.eclipse.core.runtime.IPath;
21 import org.xml.sax.Attributes;
22 import org.xml.sax.ContentHandler;
23 import org.xml.sax.InputSource;
24 import org.xml.sax.Locator;
25 import org.xml.sax.SAXException;
26 import org.xml.sax.XMLReader;
28 public class PHPRuntime {
29 protected static PHPRuntime runtime;
31 protected List installedInterpreters;
33 protected PHPInterpreter selectedInterpreter;
35 protected PHPRuntime() {
39 public static PHPRuntime getDefault() {
40 if (runtime == null) {
41 runtime = new PHPRuntime();
46 public PHPInterpreter getSelectedInterpreter() {
47 if (selectedInterpreter == null) {
48 loadRuntimeConfiguration();
50 return selectedInterpreter;
53 public PHPInterpreter getInterpreter(String installLocation) {
54 Iterator interpreters = getInstalledInterpreters().iterator();
55 while (interpreters.hasNext()) {
56 PHPInterpreter each = (PHPInterpreter) interpreters.next();
57 if (each.getInstallLocation().toString().equals(installLocation))
61 return getSelectedInterpreter();
64 public void setSelectedInterpreter(PHPInterpreter anInterpreter) {
65 selectedInterpreter = anInterpreter;
66 saveRuntimeConfiguration();
69 public void addInstalledInterpreter(PHPInterpreter anInterpreter) {
70 getInstalledInterpreters().add(anInterpreter);
71 if (getInstalledInterpreters().size() == 1)
72 setSelectedInterpreter((PHPInterpreter) getInstalledInterpreters()
75 saveRuntimeConfiguration();
78 public List getInstalledInterpreters() {
79 if (installedInterpreters == null)
80 loadRuntimeConfiguration();
81 return installedInterpreters;
84 public void setInstalledInterpreters(List newInstalledInterpreters) {
85 installedInterpreters = newInstalledInterpreters;
86 if (installedInterpreters.size() > 0)
87 setSelectedInterpreter((PHPInterpreter) installedInterpreters
90 setSelectedInterpreter(null);
93 protected void saveRuntimeConfiguration() {
94 writeXML(getRuntimeConfigurationWriter());
97 protected Writer getRuntimeConfigurationWriter() {
99 OutputStream stream = new BufferedOutputStream(
100 new FileOutputStream(getRuntimeConfigurationFile()));
101 return new OutputStreamWriter(stream);
102 } catch (FileNotFoundException e) {
108 protected void loadRuntimeConfiguration() {
109 installedInterpreters = new ArrayList();
111 File file = getRuntimeConfigurationFile();
113 XMLReader reader = SAXParserFactory.newInstance()
114 .newSAXParser().getXMLReader();
116 .setContentHandler(getRuntimeConfigurationContentHandler());
117 reader.parse(new InputSource(
118 getRuntimeConfigurationReader(file)));
120 } catch (Exception e) {
121 PHPLaunchingPlugin.log(e);
125 protected Reader getRuntimeConfigurationReader(File file) {
127 return new FileReader(file);
128 } catch (FileNotFoundException e) {
130 return new StringReader("");
133 protected void writeXML(Writer writer) {
136 .write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><runtimeconfig>");
137 Iterator interpretersIterator = installedInterpreters.iterator();
138 while (interpretersIterator.hasNext()) {
139 writer.write("<interpreter name=\"");
141 PHPInterpreter entry = (PHPInterpreter) interpretersIterator
143 // writer.write(entry.getName());
144 writer.write("\" path=\"");
145 writer.write(entry.getInstallLocation().toString());
147 if (entry.equals(selectedInterpreter))
148 writer.write(" selected=\"true\"");
152 writer.write("</runtimeconfig>");
154 } catch (IOException e) {
155 PHPLaunchingPlugin.log(e);
159 protected ContentHandler getRuntimeConfigurationContentHandler() {
160 return new ContentHandler() {
161 public void setDocumentLocator(Locator locator) {
164 public void startDocument() throws SAXException {
167 public void endDocument() throws SAXException {
170 public void startPrefixMapping(String prefix, String uri)
171 throws SAXException {
174 public void endPrefixMapping(String prefix) throws SAXException {
177 public void startElement(String namespaceURI, String localName,
178 String qName, Attributes atts) throws SAXException {
179 if ("interpreter".equals(qName)) {
180 String interpreterName = atts.getValue("name");
181 java.io.File installLocation;
182 if (interpreterName != null) {
183 installLocation = new File(atts.getValue("path")
184 + File.separatorChar + interpreterName);
186 installLocation = new File(atts.getValue("path"));
188 PHPInterpreter interpreter = new PHPInterpreter(
190 installedInterpreters.add(interpreter);
191 if (atts.getValue("selected") != null)
192 selectedInterpreter = interpreter;
196 public void endElement(String namespaceURI, String localName,
197 String qName) throws SAXException {
200 public void characters(char[] ch, int start, int length)
201 throws SAXException {
204 public void ignorableWhitespace(char[] ch, int start, int length)
205 throws SAXException {
208 public void processingInstruction(String target, String data)
209 throws SAXException {
212 public void skippedEntity(String name) throws SAXException {
217 protected File getRuntimeConfigurationFile() {
218 IPath stateLocation = PHPLaunchingPlugin.getDefault()
220 IPath fileLocation = stateLocation.append("runtimeConfiguration.xml");
221 return new File(fileLocation.toOSString());