avoid OutOfBoundsException and break of format action, when marker positions are...
[phpeclipse.git] / net.sourceforge.phpeclipse.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
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;
28
29 public class PHPRuntime {
30         protected static PHPRuntime runtime;
31         
32         protected List installedInterpreters;
33         protected PHPInterpreter selectedInterpreter;
34         protected PHPRuntime() {
35                 super();
36         }
37
38         public static PHPRuntime getDefault() {
39                 if (runtime == null) {
40                         runtime = new PHPRuntime();
41                 }
42                 return runtime;
43         }
44         
45         public PHPInterpreter getSelectedInterpreter() {
46                 if (selectedInterpreter == null) {
47                         loadRuntimeConfiguration();
48                 }
49                 return selectedInterpreter;
50         }
51
52         public PHPInterpreter getInterpreter(String installLocation) {
53                 Iterator interpreters = getInstalledInterpreters().iterator();
54                 while(interpreters.hasNext()) {
55                         PHPInterpreter each = (PHPInterpreter) interpreters.next();
56                         if (each.getInstallLocation().toString().equals(installLocation))
57                                 return each;
58                 }
59                 
60                 return getSelectedInterpreter();
61         }
62
63         public void setSelectedInterpreter(PHPInterpreter anInterpreter) {
64                 selectedInterpreter = anInterpreter;
65                 saveRuntimeConfiguration();
66         }
67
68         public void addInstalledInterpreter(PHPInterpreter anInterpreter) {
69                 getInstalledInterpreters().add(anInterpreter);
70                 if (getInstalledInterpreters().size() == 1)
71                         setSelectedInterpreter((PHPInterpreter) getInstalledInterpreters().get(0));
72
73                 saveRuntimeConfiguration();
74         }
75
76         public List getInstalledInterpreters() {
77                 if (installedInterpreters == null)
78                         loadRuntimeConfiguration();
79                 return installedInterpreters;
80         }
81         
82         public void setInstalledInterpreters(List newInstalledInterpreters) {
83                 installedInterpreters = newInstalledInterpreters;
84                 if (installedInterpreters.size() > 0)
85                         setSelectedInterpreter((PHPInterpreter)installedInterpreters.get(0));
86                 else
87                         setSelectedInterpreter(null);
88         }
89         
90         protected void saveRuntimeConfiguration() {
91                 writeXML(getRuntimeConfigurationWriter());
92         }
93
94         protected Writer getRuntimeConfigurationWriter() {
95                 try {
96                         OutputStream stream = new BufferedOutputStream(new FileOutputStream(getRuntimeConfigurationFile()));
97                         return new OutputStreamWriter(stream);
98                 } catch (FileNotFoundException e) {}
99
100                 return null;
101         }
102         
103         protected void loadRuntimeConfiguration() {
104                 installedInterpreters = new ArrayList();
105                 try {
106                         File file = getRuntimeConfigurationFile();
107                         if (file.exists()) {
108                             XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
109                             reader.setContentHandler(getRuntimeConfigurationContentHandler());
110                             reader.parse(new InputSource(getRuntimeConfigurationReader(file)));
111                         }
112                 } catch(Exception e) {
113                         PHPLaunchingPlugin.log(e);
114                 }
115         }
116
117         protected Reader getRuntimeConfigurationReader(File file) {
118                 try {
119                         return new FileReader(file);
120                 } catch(FileNotFoundException e) {}
121                 return new StringReader("");
122         }
123         
124         protected void writeXML(Writer writer) {
125                 try {
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=\"");
130                                 
131                                 PHPInterpreter entry = (PHPInterpreter) interpretersIterator.next();
132 //                              writer.write(entry.getName());
133                                 writer.write("\" path=\"");
134                                 writer.write(entry.getInstallLocation().toString());
135                                 writer.write("\"");
136                                 if (entry.equals(selectedInterpreter))
137                                         writer.write(" selected=\"true\"");
138                                         
139                                 writer.write("/>");
140                         }
141                         writer.write("</runtimeconfig>");
142                         writer.flush();
143                 } catch(IOException e) {
144                         PHPLaunchingPlugin.log(e);
145                 }
146         }
147
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                                         java.io.File installLocation;
159                                         if (interpreterName!=null) {
160                                           installLocation = new File(atts.getValue("path")+File.separatorChar+interpreterName);
161                                         } else {
162                                           installLocation = new File(atts.getValue("path"));
163                                         }
164                                         PHPInterpreter interpreter = new PHPInterpreter(installLocation);
165                                         installedInterpreters.add(interpreter);
166                                         if (atts.getValue("selected") != null)
167                                                 selectedInterpreter = interpreter;
168                                 }
169                         }
170                         public void endElement(String namespaceURI, String localName, String qName) throws SAXException {}
171                         public void characters(char[] ch, int start, int length) throws SAXException {}
172                         public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {}
173                         public void processingInstruction(String target, String data) throws SAXException {}
174                         public void skippedEntity(String name) throws SAXException {}
175                 };
176         }
177         
178         protected File getRuntimeConfigurationFile() {
179                 IPath stateLocation = PHPLaunchingPlugin.getDefault().getStateLocation();
180                 IPath fileLocation = stateLocation.append("runtimeConfiguration.xml");
181                 return new File(fileLocation.toOSString());
182         }
183 }