001: package tide.importtools;
002:
003: import java.io.*;
004: import java.util.*;
005: import tide.project.ProjectSettings;
006: import org.xml.sax.helpers.*;
007: import org.xml.sax.*;
008:
009: public final class EclipseImport extends DefaultHandler {
010: public static String fileNameEnding = ".classpath";
011: final File base;
012:
013: final private Set<String> usedKeys = new HashSet<String>();
014: final ProjectSettings proj;
015: final private List<File> libs = new ArrayList<File>();
016:
017: public EclipseImport(File propFile, ProjectSettings proj)
018: throws Exception {
019: super ();
020:
021: this .proj = proj;
022: this .base = propFile.getParentFile();
023:
024: System.out.println("Importing eclipse project "
025: + base.getName());
026: if (proj != null) {
027: proj.setProjectName(base.getName());
028: }
029:
030: XMLReader xr = XMLReaderFactory.createXMLReader();
031: xr.setContentHandler(this );
032: xr.setErrorHandler(this );
033:
034: // Important: since 1.6, crashes if inputsource made with string path (unknown protocol "c") !!
035: xr.parse(new InputSource(new FileInputStream(propFile)));
036:
037: }
038:
039: @Override
040: public void startElement(String uri, String name, String qName,
041: Attributes atts) {
042: if ("".equals(uri))
043: System.out.println("Start element: " + qName);
044: else
045: System.out.println("Start element: {" + uri + "}" + name);
046:
047: if (qName.equals("classpathentry")) {
048: if (atts.getValue("kind").equals("src")) {
049: System.out.println("### Source path = "
050: + getFile(atts.getValue("path")));
051: if (proj != null) {
052: proj
053: .setSources_Home(getFile(atts
054: .getValue("path")));
055: }
056: }
057:
058: if (atts.getValue("kind").equals("lib")) {
059: File path = getFile(atts.getValue("path"));
060: libs.add(path);
061: }
062:
063: if (atts.getValue("kind").equals("output")) {
064: if (proj != null) {
065: proj
066: .setClasses_Home(new File(atts
067: .getValue("path")));
068: }
069: }
070:
071: }
072:
073: for (int i = 0; i < atts.getLength(); i++) {
074: System.out.println(" " + atts.getLocalName(i) + " = "
075: + atts.getValue(i));
076: }
077: }
078:
079: @Override
080: public void endElement(String uri, String name, String qName) {
081: if ("".equals(uri))
082: System.out.println("End element: " + qName);
083: else
084: System.out.println("End element: {" + uri + "}" + name);
085:
086: }
087:
088: @Override
089: public void characters(char[] ch, int start, int length) {
090:
091: //System.out.println("chars: '"+new String(ch, start, length)+"'");
092: }
093:
094: @Override
095: public void endDocument() {
096: System.out.println("Classpath=" + libs);
097: if (proj != null) {
098: proj.setExternalJars(libs);
099: }
100: }
101:
102: /** files are relative to the base => this gives the absolute file path
103: * if not existing, try directly the file without base
104: */
105: private File getFile(String name) {
106: File fn = new File(name);
107: if (fn.isAbsolute() && fn.exists())
108: return fn;
109:
110: File f = new File(base, name);
111: try {
112: return f.getCanonicalFile();
113: } catch (Exception e) {
114: return f;
115: }
116: }
117:
118: }
|