This commit was generated by cvs2svn to compensate for changes in r50,
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.launching / src / net / sourceforge / phpdt / internal / launching / PHPRuntime.java
1 package net.sourceforge.phpdt.internal.launching;
2
3 import java.io.BufferedOutputStream;
4 import java.io.File;
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;
17
18 import javax.xml.parsers.SAXParserFactory;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.Path;
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;
27
28 public class PHPRuntime {
29         protected static PHPRuntime runtime;
30         
31         protected List installedInterpreters;
32         protected PHPInterpreter selectedInterpreter;
33         protected PHPRuntime() {
34                 super();
35         }
36
37         public static PHPRuntime getDefault() {
38                 if (runtime == null) {
39                         runtime = new PHPRuntime();
40                 }
41                 return runtime;
42         }
43         
44         public PHPInterpreter getSelectedInterpreter() {
45                 if (selectedInterpreter == null) {
46                         loadRuntimeConfiguration();
47                 }
48                 return selectedInterpreter;
49         }
50
51         public PHPInterpreter getInterpreter(String name) {
52                 Iterator interpreters = getInstalledInterpreters().iterator();
53                 while(interpreters.hasNext()) {
54                         PHPInterpreter each = (PHPInterpreter) interpreters.next();
55                         if (each.getName().equals(name))
56                                 return each;
57                 }
58                 
59                 return getSelectedInterpreter();
60         }
61
62         public void setSelectedInterpreter(PHPInterpreter anInterpreter) {
63                 selectedInterpreter = anInterpreter;
64                 saveRuntimeConfiguration();
65         }
66
67         public void addInstalledInterpreter(PHPInterpreter anInterpreter) {
68                 getInstalledInterpreters().add(anInterpreter);
69                 if (getInstalledInterpreters().size() == 1)
70                         setSelectedInterpreter((PHPInterpreter) getInstalledInterpreters().get(0));
71
72                 saveRuntimeConfiguration();
73         }
74
75         public List getInstalledInterpreters() {
76                 if (installedInterpreters == null)
77                         loadRuntimeConfiguration();
78                 return installedInterpreters;
79         }
80         
81         public void setInstalledInterpreters(List newInstalledInterpreters) {
82                 installedInterpreters = newInstalledInterpreters;
83                 if (installedInterpreters.size() > 0)
84                         setSelectedInterpreter((PHPInterpreter)installedInterpreters.get(0));
85                 else
86                         setSelectedInterpreter(null);
87         }
88         
89         protected void saveRuntimeConfiguration() {
90                 writeXML(getRuntimeConfigurationWriter());
91         }
92
93         protected Writer getRuntimeConfigurationWriter() {
94                 try {
95                         OutputStream stream = new BufferedOutputStream(new FileOutputStream(getRuntimeConfigurationFile()));
96                         return new OutputStreamWriter(stream);
97                 } catch (FileNotFoundException e) {}
98
99                 return null;
100         }
101         
102         protected void loadRuntimeConfiguration() {
103                 installedInterpreters = new ArrayList();
104                 try {
105                         XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
106                         reader.setContentHandler(getRuntimeConfigurationContentHandler());
107                         reader.parse(new InputSource(getRuntimeConfigurationReader()));
108                 } catch(Exception e) {
109                         PHPLaunchingPlugin.getDefault().log(e);
110                 }
111         }
112
113         protected Reader getRuntimeConfigurationReader() {
114                 try {
115                         return new FileReader(getRuntimeConfigurationFile());
116                 } catch(FileNotFoundException e) {}
117                 return new StringReader("");
118         }
119         
120         protected void writeXML(Writer writer) {
121                 try {
122                         writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><runtimeconfig>");
123                         Iterator interpretersIterator = installedInterpreters.iterator();
124                         while (interpretersIterator.hasNext()) {
125                                 writer.write("<interpreter name=\"");
126                                 
127                                 PHPInterpreter entry = (PHPInterpreter) interpretersIterator.next();
128                                 writer.write(entry.getName());
129                                 writer.write("\" path=\"");
130                                 writer.write(entry.getInstallLocation().toString());
131                                 writer.write("\"");
132                                 if (entry.equals(selectedInterpreter))
133                                         writer.write(" selected=\"true\"");
134                                         
135                                 writer.write("/>");
136                         }
137                         writer.write("</runtimeconfig>");
138                         writer.flush();
139                 } catch(IOException e) {
140                         PHPLaunchingPlugin.log(e);
141                 }
142         }
143
144         protected ContentHandler getRuntimeConfigurationContentHandler() {
145                 return new ContentHandler() {
146                         public void setDocumentLocator(Locator locator) {}
147                         public void startDocument() throws SAXException {}
148                         public void endDocument() throws SAXException {}
149                         public void startPrefixMapping(String prefix, String uri) throws SAXException {}
150                         public void endPrefixMapping(String prefix) throws SAXException {}
151                         public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
152                                 if ("interpreter".equals(qName)) {
153                                         String interpreterName = atts.getValue("name");
154                                         IPath installLocation = new Path(atts.getValue("path"));
155                                         PHPInterpreter interpreter = new PHPInterpreter(interpreterName, installLocation);
156                                         installedInterpreters.add(interpreter);
157                                         if (atts.getValue("selected") != null)
158                                                 selectedInterpreter = interpreter;
159                                 }
160                         }
161                         public void endElement(String namespaceURI, String localName, String qName) throws SAXException {}
162                         public void characters(char[] ch, int start, int length) throws SAXException {}
163                         public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {}
164                         public void processingInstruction(String target, String data) throws SAXException {}
165                         public void skippedEntity(String name) throws SAXException {}
166                 };
167         }
168         
169         protected File getRuntimeConfigurationFile() {
170                 IPath stateLocation = PHPLaunchingPlugin.getDefault().getStateLocation();
171                 IPath fileLocation = stateLocation.append("runtimeConfiguration.xml");
172                 return new File(fileLocation.toOSString());
173         }
174 }