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: /** A very easy ant import, just reads the some project paths
010: * TODO: import ext libs... classes...
011: */
012: public final class AntImport extends DefaultHandler {
013: public static String fileNameEnding = ".xml"; // normally build.xml
014: final File baseDir;
015:
016: final ProjectSettings proj;
017: final private List<File> libs = new ArrayList<File>();
018: final private Map<String, String> properties = new HashMap<String, String>();
019:
020: public AntImport(File propFile, ProjectSettings proj)
021: throws Exception {
022: super ();
023:
024: this .proj = proj;
025: this .baseDir = propFile.getParentFile();
026:
027: System.out
028: .println("Importing ant project " + baseDir.getName());
029: if (proj != null) {
030: proj.setProjectName(baseDir.getName());
031: }
032:
033: XMLReader xr = XMLReaderFactory.createXMLReader();
034: xr.setContentHandler(this );
035: xr.setErrorHandler(this );
036:
037: // Important: since 1.6, crashes if inputsource made with string path (unknown protocol "c") !!
038: xr.parse(new InputSource(new FileInputStream(propFile)));
039:
040: System.out.println("\nPROPS=" + properties);
041: System.out.println("\nKEYS=" + properties.keySet());
042:
043: if (proj != null) {
044: proj.setSources_Home(new File(this
045: .getResolvedPropValue("src")));
046: }
047: System.out.println("Src=" + this .getResolvedPropValue("src"));
048: System.out.println("out=" + this .getResolvedPropValue("out"));
049:
050: }
051:
052: @Override
053: public void startElement(String uri, String name, String qName,
054: Attributes atts) {
055:
056: if (qName.equals("property") && "".equals(uri)) {
057: //System.out.println("Start element: {" + uri + "} " + name);
058: // top level props
059: /*for(int i=0; i<atts.getLength(); i++)
060: {
061: System.out.println("PROPERTY "+atts.getLocalName(i)+" = "+atts.getValue(i));
062: }*/
063:
064: if (atts.getValue("name") != null) {
065: properties.put(atts.getValue("name"), atts
066: .getValue("value"));
067: } else {
068: System.out.println("### Start element: {" + uri + "} "
069: + name);
070: for (int i = 0; i < atts.getLength(); i++) {
071: System.out.println(" PROPERTY "
072: + atts.getLocalName(i) + " = "
073: + atts.getValue(i));
074: }
075: }
076: } else {
077: if (qName.equals("project")) {
078: if (proj != null)
079: proj.setProjectName(atts.getValue("name"));
080: System.out.println("ANT Project name = "
081: + atts.getValue("name"));
082: } else {
083: System.out.println("Start element: {" + uri + "} "
084: + name + " (" + qName + ")");
085: for (int i = 0; i < atts.getLength(); i++) {
086: System.out.println(" ATTRS: "
087: + atts.getLocalName(i) + " = "
088: + atts.getValue(i));
089: }
090: }
091: }
092:
093: /*if(qName.equals("classpathentry"))
094: {
095: if( atts.getValue("kind").equals("src"))
096: {
097: System.out.println("### Source path = "+getFile(atts.getValue("path")));
098: if(proj!=null)
099: {
100: proj.setSources_Home( getFile(atts.getValue("path")) );
101: }
102: }
103:
104: if( atts.getValue("kind").equals("lib"))
105: {
106: File path = getFile(atts.getValue("path"));
107: libs.add( path );
108: }
109:
110: if( atts.getValue("kind").equals("output"))
111: {
112: if(proj!=null)
113: {
114: proj.setClasses_Home( new File(atts.getValue("path")) );
115: }
116: }
117:
118: }*/
119:
120: }
121:
122: @Override
123: public void endElement(String uri, String name, String qName) {
124: /* if ("".equals (uri))
125: System.out.println("End element: " + qName);
126: else
127: System.out.println("End element: {" + uri + "}" + name);
128: */
129: }
130:
131: @Override
132: public void characters(char[] ch, int start, int length) {
133:
134: //System.out.println("chars: '"+new String(ch, start, length)+"'");
135: }
136:
137: @Override
138: public void endDocument() {
139: System.out.println("Classpath=" + libs);
140: if (proj != null) {
141: proj.setExternalJars(libs);
142: }
143: }
144:
145: private String getResolvedPropValue(String name) {
146: String raw = properties.get(name);
147: if (raw == null)
148: return null;
149:
150: raw = raw.replace("${basedir}", this .baseDir.getAbsolutePath());
151:
152: return raw;
153: }
154: /*
155: public static void main(String[] args) throws Exception
156: {
157: new AntImport(new File("C:/Java/asm/asm-cvs-2006-09-19/asm/build.xml"), null);
158: }
159: */
160: }
|