859733adf9945da3fe53979e25c74359248567e5
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / resourcesview / PHPProject.java
1 package net.sourceforge.phpeclipse.resourcesview;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.InputStream;
5 import java.util.ArrayList;
6 import java.util.Iterator;
7 import java.util.List;
8
9 import javax.xml.parsers.SAXParserFactory;
10
11 import org.eclipse.core.resources.IFile;
12 import org.eclipse.core.resources.IProject;
13 import org.eclipse.core.resources.IProjectNature;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.core.runtime.Status;
20 import net.sourceforge.phpeclipse.LoadPathEntry;
21 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
22
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;
29
30 public class PHPProject implements IProjectNature, PHPElement {
31         protected IProject project;
32         protected List loadPathEntries;
33         protected boolean scratched;
34
35         public PHPProject() {}
36
37         public void configure() throws CoreException {}
38
39         public void deconfigure() throws CoreException {}
40
41         public IProject getProject() {
42                 return project;
43         }
44
45         protected IProject getProject(String name) {
46                 return PHPeclipsePlugin.getWorkspace().getRoot().getProject(name);
47         }
48
49         public void setProject(IProject aProject) {
50                 project = aProject;
51         }
52
53         public void addLoadPathEntry(IProject anotherPHPProject) {
54                 scratched = true;
55
56                 LoadPathEntry newEntry = new LoadPathEntry(anotherPHPProject);
57                 getLoadPathEntries().add(newEntry);
58         }
59
60         public void removeLoadPathEntry(IProject anotherPHPProject) {
61                 Iterator entries = getLoadPathEntries().iterator();
62                 while (entries.hasNext()) {
63                         LoadPathEntry entry = (LoadPathEntry) entries.next();
64                         if (entry.getType() == LoadPathEntry.TYPE_PROJECT && entry.getProject().getName().equals(anotherPHPProject.getName())) {
65                                 getLoadPathEntries().remove(entry);
66                                 scratched = true;
67                                 break;
68                         }
69                 }
70         }
71
72         public List getLoadPathEntries() {
73                 if (loadPathEntries == null) {
74                         loadLoadPathEntries();
75                 }
76
77                 return loadPathEntries;
78         }
79
80         public List getReferencedProjects() {
81                 List referencedProjects = new ArrayList();
82
83                 Iterator iterator = getLoadPathEntries().iterator();
84                 while (iterator.hasNext()) {
85                         LoadPathEntry pathEntry = (LoadPathEntry) iterator.next();
86                         if (pathEntry.getType() == LoadPathEntry.TYPE_PROJECT)
87                                 referencedProjects.add(pathEntry.getProject());
88                 }
89
90                 return referencedProjects;
91         }
92
93         protected void loadLoadPathEntries() {
94                 loadPathEntries = new ArrayList();
95
96                 IFile loadPathsFile = getLoadPathEntriesFile();
97
98                 XMLReader reader = null;
99                 try {
100                         reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
101                         reader.setContentHandler(getLoadPathEntriesContentHandler());
102                         reader.parse(new InputSource(loadPathsFile.getContents()));
103                 } catch (Exception e) {
104                         //the file is nonextant or unreadable
105                 }
106         }
107
108         protected ContentHandler getLoadPathEntriesContentHandler() {
109                 return new ContentHandler() {
110                         public void characters(char[] arg0, int arg1, int arg2) throws SAXException {}
111
112                         public void endDocument() throws SAXException {}
113
114                         public void endElement(String arg0, String arg1, String arg2) throws SAXException {}
115
116                         public void endPrefixMapping(String arg0) throws SAXException {}
117
118                         public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {}
119
120                         public void processingInstruction(String arg0, String arg1) throws SAXException {}
121
122                         public void setDocumentLocator(Locator arg0) {}
123
124                         public void skippedEntity(String arg0) throws SAXException {}
125
126                         public void startDocument() throws SAXException {}
127
128                         public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
129                                 if ("pathentry".equals(qName))
130                                         if ("project".equals(atts.getValue("type"))) {
131                                                 IPath referencedProjectPath = new Path(atts.getValue("path"));
132                                                 IProject referencedProject = getProject(referencedProjectPath.lastSegment());
133                                                 loadPathEntries.add(new LoadPathEntry(referencedProject));
134                                         }
135                         }
136
137                         public void startPrefixMapping(String arg0, String arg1) throws SAXException {}
138                 };
139         }
140
141         protected IFile getLoadPathEntriesFile() {
142                 return project.getFile(".loadpath");
143         }
144
145         public void save() throws CoreException {
146                 if (scratched) {
147                         InputStream xmlPath = new ByteArrayInputStream(getLoadPathXML().getBytes());
148                         IFile loadPathsFile = getLoadPathEntriesFile();
149                         if (!loadPathsFile.exists())
150                                 loadPathsFile.create(xmlPath, true, null);
151                         else
152                                 loadPathsFile.setContents(xmlPath, true, false, null);
153
154                         scratched = false;
155                 }
156         }
157
158         protected String getLoadPathXML() {
159                 StringBuffer buffer = new StringBuffer();
160                 buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><loadpath>");
161
162                 Iterator pathEntriesIterator = loadPathEntries.iterator();
163
164                 while (pathEntriesIterator.hasNext()) {
165                         LoadPathEntry entry = (LoadPathEntry) pathEntriesIterator.next();
166                         buffer.append(entry.toXML());
167                 }
168
169                 buffer.append("</loadpath>");
170                 return buffer.toString();
171         }
172         public IResource getUnderlyingResource() {
173                 return project;
174         }
175
176 }