import org.xml.sax.Attributes; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.Locator; import org.xml.sax.SAXParseException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; // import java.nio.charset; import java.io.File; import java.io.StringReader; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; class MyAnt { static public void main(String[] args) throws Exception { System.err.println( Charset.forName( System.getProperty( "file.encoding" ) ).name() ); int n = args.length; String filename = null; boolean trace = false; boolean help = false; boolean error = (n == 0); for (int i = 0; i < n; i++) { if (args[i].equals("-v") || args[i].equals("-verbatim")) { trace = true; } else if (args[i].equals("-h") || args[i].equals("-help")) { help = true; } else if ((i == (n - 1)) && (args[i].length() > 0) && (args[i].charAt(0) != '-')) { filename = args[i]; } else { error = true; } } if ((filename == null) && !help) { error = true; } if (error) { System.err.print("--- Error --- Illigal command line: "); trace = true; } if (trace) { if (!error) { System.err.print(" [trace]"); } System.err.print(" java MyAnt"); for (int i = 0; i < n; i++) { System.err.print(" " + args[i]); } System.err.println(); } if (error || help) { System.err.println( "\n Usage: java MyAnt [-v|-verbatim] [-h|-help] filename\n" + "\n filename shouldn't start with a hyphen" + "\n filename is optional when help is on"); if (error) { System.exit(1); } } if (filename == null) { System.exit(0); } SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler(new AntContentHandler(trace)); xmlReader.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException e) { showSpecifics("Warning", e); } public void error(SAXParseException e) { showSpecifics("Error", e); System.exit(4); } public void fatalError(SAXParseException e) { showSpecifics("Fatal error", e); System.exit(5); } public void showSpecifics(String s, SAXParseException e) { System.out.println(s + ": " + e.getSystemId() + " line " + e.getLineNumber() + " col " + e.getColumnNumber() + "\n " + e.getMessage()); } }); xmlReader.parse(new File(filename).toURL().toString()); } } class AntContentHandler extends DefaultHandler { String mkdir_element = "mkdir"; String move_element = "move"; String fileset_element = "fileset"; String echo_element = "echo"; String project_element = "project"; String message_attribute = "message"; String basedir; String basedir_attribute = "basedir"; String start_msg = "Start a project"; String end_msg = "End the project"; String mkdir_command = "mkdir"; String dir_attribute = "dir"; String separator = "/"; String todir; String move_command = "mv"; String todir_attribute = "todir"; String file_attribute = "file"; String dict; String charset; String usrDir = System.getProperty("user.dir", null) + "/"; private boolean trace; Locator locator; public AntContentHandler(boolean trace) { this.trace = trace; if (usrDir.charAt(0) != '/') { mkdir_command = "mkdir"; move_command = "move"; separator = "\\"; } } public void startElement(String uri, String localName, String qName, Attributes atts) { if (qName.equals(project_element)) { basedir = atts.getValue(basedir_attribute); if (trace) { System.err.println(" [" + locator.getLineNumber() + ". trace] " + start_msg); } } else if (qName.equals(mkdir_element)) { String dir = atts.getValue(dir_attribute); dir = basedir + "/" + dir; String command = mkdir_command + " " + dir; if (!separator.equals("/")) { command = command.replaceAll("/", separator + separator); command = command.replaceAll(" .\\", usrDir); } try { if (trace) { System.err.println(" [" + locator.getLineNumber() + ". trace] " + command); } Runtime.getRuntime().exec(command); } catch (Exception e) { System.err.println("[" + locator.getLineNumber() + ". --- error ---] Couldn't make directory: " + command); System.exit(2); } } else if (qName.equals(move_element)) { todir = atts.getValue(todir_attribute); todir = basedir + "/" + todir; } else if (qName.equals(fileset_element)) { String file = atts.getValue(file_attribute); file = basedir + "/" + file; String command = move_command + " " + file + " " + todir + "/."; if (!separator.equals("/")) { command = command.replaceAll("/", separator + separator); command = command.replaceAll(" .\\", usrDir); } try { if (trace) { System.err.println(" [" + locator.getLineNumber() + ". trace] " + command); } Runtime.getRuntime().exec(command); } catch (Exception e) { System.err.println("[" + locator.getLineNumber() + ". --- error ---] Couldn't move file: " + command); System.exit(3); } } else if (qName.equals(echo_element)) { String mssg = atts.getValue(message_attribute); String command = echo_element + " \"" + mssg + "\""; if (trace) { System.err.println(" [" + locator.getLineNumber() + ". trace] " + command); } System.out.println(mssg); } } public void endElement(String uri, String localName, String qName) { if (qName.equals(project_element)) { if (trace) { System.err.println(" [" + locator.getLineNumber() + ". trace] " + end_msg); } } } public void setDocumentLocator(Locator locator) { this.locator = locator; } public void processingInstruction(String target, String attrs) { if (target.equals("lang")) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler(new DefaultHandler() { public void startElement(String uri, String localName, String qName, Attributes atts) { dict = atts.getValue("dict"); charset = atts.getValue("charset"); } }); String str = ""; StringReader reader = new StringReader(str); InputSource in = new InputSource(reader); xmlReader.parse(in); xmlReader.setContentHandler(new DefaultHandler() { public void startElement(String uri, String localName, String qName, Attributes atts) { String name = atts.getValue("name"); if (qName.equals("project")) { project_element = name; } else if (qName.equals("mkdir")) { mkdir_element = name; } else if (qName.equals("move")) { move_element = name; } else if (qName.equals("echo")) { echo_element = name; } else if (qName.equals("basedir")) { basedir_attribute = name; } else if (qName.equals("dir")) { dir_attribute = name; } else if (qName.equals("file")) { file_attribute = name; } else if (qName.equals("fileset")) { fileset_element = name; } else if (qName.equals("message")) { message_attribute = name; } else if (qName.equals("todir")) { todir_attribute = name; } else if (qName.equals("start")) { start_msg = name; } else if (qName.equals("end")) { end_msg = name; } } }); xmlReader.parse(new File(dict).toURL().toString()); } catch (Exception pie) { System.err.println("--- Error ---" + pie); } } } }