1 package net.sourceforge.phpeclipse.resourcesview;
3 import java.io.ByteArrayInputStream;
4 import java.io.InputStream;
5 import java.util.ArrayList;
6 import java.util.Iterator;
9 import javax.xml.parsers.SAXParserFactory;
11 import net.sourceforge.phpeclipse.LoadPathEntry;
12 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
14 import org.eclipse.core.resources.ICommand;
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IProjectDescription;
18 import org.eclipse.core.resources.IProjectNature;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.Path;
23 import org.xml.sax.Attributes;
24 import org.xml.sax.ContentHandler;
25 import org.xml.sax.InputSource;
26 import org.xml.sax.Locator;
27 import org.xml.sax.SAXException;
28 import org.xml.sax.XMLReader;
30 public class PHPProject_DeleteIt implements IProjectNature, PHPElement {
31 protected IProject fProject;
32 // protected IndexFileReader fIndexManager;
33 protected List fLoadPathEntries;
34 protected boolean fScratched;
36 public PHPProject_DeleteIt() {
39 public void addLoadPathEntry(IProject anotherPHPProject) {
42 LoadPathEntry newEntry = new LoadPathEntry(anotherPHPProject);
43 getLoadPathEntries().add(newEntry);
46 public void configure() throws CoreException {
47 // get project description and then the associated build commands
48 IProjectDescription desc = fProject.getDescription();
49 ICommand[] commands = desc.getBuildSpec();
51 // determine if builder already associated
52 boolean found = false;
53 for (int i = 0; i < commands.length; ++i) {
54 if (commands[i].getBuilderName().equals(PHPeclipsePlugin.BUILDER_PARSER_ID)) {
60 // add builder if not already in project
62 ICommand command = desc.newCommand();
63 command.setBuilderName(PHPeclipsePlugin.BUILDER_PARSER_ID);
64 ICommand[] newCommands = new ICommand[commands.length + 1];
66 // Add it before other builders.
67 System.arraycopy(commands, 0, newCommands, 1, commands.length);
68 newCommands[0] = command;
69 desc.setBuildSpec(newCommands);
70 fProject.setDescription(desc, null);
74 public void deconfigure() throws CoreException {
77 public List getLoadPathEntries() {
78 if (fLoadPathEntries == null) {
79 loadLoadPathEntries();
82 return fLoadPathEntries;
85 protected ContentHandler getLoadPathEntriesContentHandler() {
86 return new ContentHandler() {
87 public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
90 public void endDocument() throws SAXException {
93 public void endElement(String arg0, String arg1, String arg2) throws SAXException {
96 public void endPrefixMapping(String arg0) throws SAXException {
99 public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {
102 public void processingInstruction(String arg0, String arg1) throws SAXException {
105 public void setDocumentLocator(Locator arg0) {
108 public void skippedEntity(String arg0) throws SAXException {
111 public void startDocument() throws SAXException {
114 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
115 if ("pathentry".equals(qName))
116 if ("project".equals(atts.getValue("type"))) {
117 IPath referencedProjectPath = new Path(atts.getValue("path"));
118 IProject referencedProject = getProject(referencedProjectPath.lastSegment());
119 fLoadPathEntries.add(new LoadPathEntry(referencedProject));
123 public void startPrefixMapping(String arg0, String arg1) throws SAXException {
128 protected IFile getLoadPathEntriesFile() {
129 return fProject.getFile(".loadpath");
132 protected String getLoadPathXML() {
133 StringBuffer buffer = new StringBuffer();
134 buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><loadpath>");
136 Iterator pathEntriesIterator = fLoadPathEntries.iterator();
138 while (pathEntriesIterator.hasNext()) {
139 LoadPathEntry entry = (LoadPathEntry) pathEntriesIterator.next();
140 buffer.append(entry.toXML());
143 buffer.append("</loadpath>");
144 return buffer.toString();
147 public IProject getProject() {
151 protected IProject getProject(String name) {
152 return PHPeclipsePlugin.getWorkspace().getRoot().getProject(name);
155 public List getReferencedProjects() {
156 List referencedProjects = new ArrayList();
158 Iterator iterator = getLoadPathEntries().iterator();
159 while (iterator.hasNext()) {
160 LoadPathEntry pathEntry = (LoadPathEntry) iterator.next();
161 if (pathEntry.getType() == LoadPathEntry.TYPE_PROJECT)
162 referencedProjects.add(pathEntry.getProject());
165 return referencedProjects;
168 public IResource getUnderlyingResource() {
172 protected void loadLoadPathEntries() {
173 fLoadPathEntries = new ArrayList();
175 IFile loadPathsFile = getLoadPathEntriesFile();
177 XMLReader reader = null;
179 reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
180 reader.setContentHandler(getLoadPathEntriesContentHandler());
181 reader.parse(new InputSource(loadPathsFile.getContents()));
182 } catch (Exception e) {
183 //the file is nonextant or unreadable
187 public void removeLoadPathEntry(IProject anotherPHPProject) {
188 Iterator entries = getLoadPathEntries().iterator();
189 while (entries.hasNext()) {
190 LoadPathEntry entry = (LoadPathEntry) entries.next();
191 if (entry.getType() == LoadPathEntry.TYPE_PROJECT && entry.getProject().getName().equals(anotherPHPProject.getName())) {
192 getLoadPathEntries().remove(entry);
199 public void save() throws CoreException {
201 InputStream xmlPath = new ByteArrayInputStream(getLoadPathXML().getBytes());
202 IFile loadPathsFile = getLoadPathEntriesFile();
203 if (!loadPathsFile.exists())
204 loadPathsFile.create(xmlPath, true, null);
206 loadPathsFile.setContents(xmlPath, true, false, null);
212 public void setProject(IProject aProject) {