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 File file = getRuntimeConfigurationFile();
108 XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
109 reader.setContentHandler(getRuntimeConfigurationContentHandler());
110 reader.parse(new InputSource(getRuntimeConfigurationReader(file)));
112 } catch(Exception e) {
113 PHPLaunchingPlugin.log(e);
117 protected Reader getRuntimeConfigurationReader(File file) {
119 return new FileReader(file);
120 } catch(FileNotFoundException e) {}
121 return new StringReader("");
124 protected void writeXML(Writer writer) {
126 writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><runtimeconfig>");
127 Iterator interpretersIterator = installedInterpreters.iterator();
128 while (interpretersIterator.hasNext()) {
129 writer.write("<interpreter name=\"");
131 PHPInterpreter entry = (PHPInterpreter) interpretersIterator.next();
132 writer.write(entry.getName());
133 writer.write("\" path=\"");
134 writer.write(entry.getInstallLocation().toString());
136 if (entry.equals(selectedInterpreter))
137 writer.write(" selected=\"true\"");
141 writer.write("</runtimeconfig>");
143 } catch(IOException e) {
144 PHPLaunchingPlugin.log(e);
148 protected ContentHandler getRuntimeConfigurationContentHandler() {
149 return new ContentHandler() {
150 public void setDocumentLocator(Locator locator) {}
151 public void startDocument() throws SAXException {}
152 public void endDocument() throws SAXException {}
153 public void startPrefixMapping(String prefix, String uri) throws SAXException {}
154 public void endPrefixMapping(String prefix) throws SAXException {}
155 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
156 if ("interpreter".equals(qName)) {
157 String interpreterName = atts.getValue("name");
158 IPath installLocation = new Path(atts.getValue("path"));
159 PHPInterpreter interpreter = new PHPInterpreter(interpreterName, installLocation);
160 installedInterpreters.add(interpreter);
161 if (atts.getValue("selected") != null)
162 selectedInterpreter = interpreter;
165 public void endElement(String namespaceURI, String localName, String qName) throws SAXException {}
166 public void characters(char[] ch, int start, int length) throws SAXException {}
167 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {}
168 public void processingInstruction(String target, String data) throws SAXException {}
169 public void skippedEntity(String name) throws SAXException {}
173 protected File getRuntimeConfigurationFile() {
174 IPath stateLocation = PHPLaunchingPlugin.getDefault().getStateLocation();
175 IPath fileLocation = stateLocation.append("runtimeConfiguration.xml");
176 return new File(fileLocation.toOSString());