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.eclipse.core.runtime.Path;
22 import org.xml.sax.Attributes;
23 import org.xml.sax.ContentHandler;
24 import org.xml.sax.InputSource;
25 import org.xml.sax.Locator;
26 import org.xml.sax.SAXException;
27 import org.xml.sax.XMLReader;
29 public class PHPRuntime {
30 protected static PHPRuntime runtime;
32 protected List installedInterpreters;
33 protected PHPInterpreter selectedInterpreter;
34 protected PHPRuntime() {
38 public static PHPRuntime getDefault() {
39 if (runtime == null) {
40 runtime = new PHPRuntime();
45 public PHPInterpreter getSelectedInterpreter() {
46 if (selectedInterpreter == null) {
47 loadRuntimeConfiguration();
49 return selectedInterpreter;
52 public PHPInterpreter getInterpreter(String name) {
53 Iterator interpreters = getInstalledInterpreters().iterator();
54 while(interpreters.hasNext()) {
55 PHPInterpreter each = (PHPInterpreter) interpreters.next();
56 if (each.getName().equals(name))
60 return getSelectedInterpreter();
63 public void setSelectedInterpreter(PHPInterpreter anInterpreter) {
64 selectedInterpreter = anInterpreter;
65 saveRuntimeConfiguration();
68 public void addInstalledInterpreter(PHPInterpreter anInterpreter) {
69 getInstalledInterpreters().add(anInterpreter);
70 if (getInstalledInterpreters().size() == 1)
71 setSelectedInterpreter((PHPInterpreter) getInstalledInterpreters().get(0));
73 saveRuntimeConfiguration();
76 public List getInstalledInterpreters() {
77 if (installedInterpreters == null)
78 loadRuntimeConfiguration();
79 return installedInterpreters;
82 public void setInstalledInterpreters(List newInstalledInterpreters) {
83 installedInterpreters = newInstalledInterpreters;
84 if (installedInterpreters.size() > 0)
85 setSelectedInterpreter((PHPInterpreter)installedInterpreters.get(0));
87 setSelectedInterpreter(null);
90 protected void saveRuntimeConfiguration() {
91 writeXML(getRuntimeConfigurationWriter());
94 protected Writer getRuntimeConfigurationWriter() {
96 OutputStream stream = new BufferedOutputStream(new FileOutputStream(getRuntimeConfigurationFile()));
97 return new OutputStreamWriter(stream);
98 } catch (FileNotFoundException e) {}
103 protected void loadRuntimeConfiguration() {
104 installedInterpreters = new ArrayList();
106 XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
107 reader.setContentHandler(getRuntimeConfigurationContentHandler());
108 reader.parse(new InputSource(getRuntimeConfigurationReader()));
109 } catch(Exception e) {
110 PHPLaunchingPlugin.log(e);
114 protected Reader getRuntimeConfigurationReader() {
116 return new FileReader(getRuntimeConfigurationFile());
117 } catch(FileNotFoundException e) {}
118 return new StringReader("");
121 protected void writeXML(Writer writer) {
123 writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><runtimeconfig>");
124 Iterator interpretersIterator = installedInterpreters.iterator();
125 while (interpretersIterator.hasNext()) {
126 writer.write("<interpreter name=\"");
128 PHPInterpreter entry = (PHPInterpreter) interpretersIterator.next();
129 writer.write(entry.getName());
130 writer.write("\" path=\"");
131 writer.write(entry.getInstallLocation().toString());
133 if (entry.equals(selectedInterpreter))
134 writer.write(" selected=\"true\"");
138 writer.write("</runtimeconfig>");
140 } catch(IOException e) {
141 PHPLaunchingPlugin.log(e);
145 protected ContentHandler getRuntimeConfigurationContentHandler() {
146 return new ContentHandler() {
147 public void setDocumentLocator(Locator locator) {}
148 public void startDocument() throws SAXException {}
149 public void endDocument() throws SAXException {}
150 public void startPrefixMapping(String prefix, String uri) throws SAXException {}
151 public void endPrefixMapping(String prefix) throws SAXException {}
152 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
153 if ("interpreter".equals(qName)) {
154 String interpreterName = atts.getValue("name");
155 IPath installLocation = new Path(atts.getValue("path"));
156 PHPInterpreter interpreter = new PHPInterpreter(interpreterName, installLocation);
157 installedInterpreters.add(interpreter);
158 if (atts.getValue("selected") != null)
159 selectedInterpreter = interpreter;
162 public void endElement(String namespaceURI, String localName, String qName) throws SAXException {}
163 public void characters(char[] ch, int start, int length) throws SAXException {}
164 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {}
165 public void processingInstruction(String target, String data) throws SAXException {}
166 public void skippedEntity(String name) throws SAXException {}
170 protected File getRuntimeConfigurationFile() {
171 IPath stateLocation = PHPLaunchingPlugin.getDefault().getStateLocation();
172 IPath fileLocation = stateLocation.append("runtimeConfiguration.xml");
173 return new File(fileLocation.toOSString());