List of usage examples for java.lang String matches
public boolean matches(String regex)
From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step7CollectMTurkResults.java
public static void main(String[] args) throws Exception { // input dir - list of xml query containers // /home/user-ukp/research/data/dip/wp1-documents/step4-boiler-plate/ File inputDir = new File(args[0] + "/"); // MTurk result file // output dir File outputDir = new File(args[2]); if (!outputDir.exists()) { outputDir.mkdirs();// w ww. ja v a 2s.c om } // Folder with success files File mturkSuccessDir = new File(args[1]); Collection<File> files = FileUtils.listFiles(mturkSuccessDir, new String[] { "result" }, false); if (files.isEmpty()) { throw new IllegalArgumentException("Input folder is empty. " + mturkSuccessDir); } HashMap<String, List<MTurkAnnotation>> mturkAnnotations = new HashMap<>(); // parsing all CSV files for (File mturkCSVResultFile : files) { System.out.println("Parsing " + mturkCSVResultFile.getName()); MTurkOutputReader outputReader = new MTurkOutputReader( new HashSet<>(Arrays.asList("annotation", "workerid")), mturkCSVResultFile); // for fixing broken data input: for each hit, collect all sentence IDs Map<String, SortedSet<String>> hitSentences = new HashMap<>(); // first iteration: collect the sentences for (Map<String, String> record : outputReader) { String hitID = record.get("hitid"); if (!hitSentences.containsKey(hitID)) { hitSentences.put(hitID, new TreeSet<>()); } String relevantSentences = record.get("Answer.relevant_sentences"); String irrelevantSentences = record.get("Answer.irrelevant_sentences"); if (relevantSentences != null) { hitSentences.get(hitID).addAll(Arrays.asList(relevantSentences.split(","))); } if (irrelevantSentences != null) { hitSentences.get(hitID).addAll(Arrays.asList(irrelevantSentences.split(","))); } } // and now second iteration for (Map<String, String> record : outputReader) { String hitID = record.get("hitid"); String annotatorID = record.get("workerid"); String acceptTime = record.get("assignmentaccepttime"); String submitTime = record.get("assignmentsubmittime"); String relevantSentences = record.get("Answer.relevant_sentences"); String irrelevantSentences = record.get("Answer.irrelevant_sentences"); String reject = record.get("reject"); String filename[]; String comment; String clueWeb; String[] relevant = {}; String[] irrelevant = {}; filename = record.get("annotation").split("_"); String fileXml = filename[0]; clueWeb = filename[1].trim(); comment = record.get("Answer.comment"); if (relevantSentences != null) { relevant = relevantSentences.split(","); } if (irrelevantSentences != null) { irrelevant = irrelevantSentences.split(","); } // sanitizing data: if both relevant and irrelevant are empty, that's a bug // we're gonna look up all sentences from this HIT and treat this assignment // as if there were only irrelevant ones if (relevant.length == 0 && irrelevant.length == 0) { SortedSet<String> strings = hitSentences.get(hitID); irrelevant = new String[strings.size()]; strings.toArray(irrelevant); } if (reject != null) { System.out.println(" HIT " + hitID + " annotated by " + annotatorID + " was rejected "); } else { /* // relevant sentences is a comma-delimited string, // this regular expression is rather strange // it must contain digits, it might be that there is only one space or a comma or some other char // digits are the sentence ids. if relevant sentences do not contain digits then it is wrong if (relevantSentences.matches("^\\D*$") && irrelevantSentences.matches("^\\D*$")) { try { throw new IllegalStateException( "No annotations found for HIT " + hitID + " in " + fileXml + " for document " + clueWeb); } catch (IllegalStateException ex) { ex.printStackTrace(); } } */ MTurkAnnotation mturkAnnotation; try { mturkAnnotation = new MTurkAnnotation(hitID, annotatorID, acceptTime, submitTime, comment, clueWeb, relevant, irrelevant); } catch (IllegalArgumentException ex) { throw new IllegalArgumentException("Record: " + record, ex); } List<MTurkAnnotation> listOfAnnotations = mturkAnnotations.get(fileXml); if (listOfAnnotations == null) { listOfAnnotations = new ArrayList<>(); } listOfAnnotations.add(mturkAnnotation); mturkAnnotations.put(fileXml, listOfAnnotations); } } // parser.close(); } // Debugging: output number of HITs of a query System.out.println("Accepted HITs for a query:"); for (Map.Entry e : mturkAnnotations.entrySet()) { ArrayList<MTurkAnnotation> a = (ArrayList<MTurkAnnotation>) e.getValue(); System.out.println(e.getKey() + " " + a.size()); } for (File f : FileUtils.listFiles(inputDir, new String[] { "xml" }, false)) { QueryResultContainer queryResultContainer = QueryResultContainer .fromXML(FileUtils.readFileToString(f, "utf-8")); String fileName = f.getName(); List<MTurkAnnotation> listOfAnnotations = mturkAnnotations.get(fileName); if (listOfAnnotations == null || listOfAnnotations.isEmpty()) { throw new IllegalStateException("No annotations for " + f.getName()); } for (QueryResultContainer.SingleRankedResult rankedResults : queryResultContainer.rankedResults) { for (MTurkAnnotation mtAnnotation : listOfAnnotations) { String clueWeb = mtAnnotation.clueWeb; if (rankedResults.clueWebID.equals(clueWeb)) { List<QueryResultContainer.MTurkRelevanceVote> mTurkRelevanceVotes = rankedResults.mTurkRelevanceVotes; QueryResultContainer.MTurkRelevanceVote relevanceVote = new QueryResultContainer.MTurkRelevanceVote(); String annotatorID = mtAnnotation.annotatorID; String hitID = mtAnnotation.hitID; String acceptTime = mtAnnotation.acceptTime; String submitTime = mtAnnotation.submitTime; String comment = mtAnnotation.comment; String[] relevant = mtAnnotation.relevant; String[] irrelevant = mtAnnotation.irrelevant; relevanceVote.turkID = annotatorID.trim(); relevanceVote.hitID = hitID.trim(); relevanceVote.acceptTime = acceptTime.trim(); relevanceVote.submitTime = submitTime.trim(); relevanceVote.comment = comment != null ? comment.trim() : null; if (relevant.length == 0 && irrelevant.length == 0) { try { throw new IllegalStateException("the length of the annotations is 0" + rankedResults.clueWebID + " for HIT " + relevanceVote.hitID); } catch (IllegalStateException e) { e.printStackTrace(); } } for (String r : relevant) { String sentenceId = r.trim(); if (!sentenceId.isEmpty() && sentenceId.matches("\\d+")) { QueryResultContainer.SingleSentenceRelevanceVote singleSentenceVote = new QueryResultContainer.SingleSentenceRelevanceVote(); singleSentenceVote.sentenceID = sentenceId; singleSentenceVote.relevant = "true"; relevanceVote.singleSentenceRelevanceVotes.add(singleSentenceVote); } } for (String r : irrelevant) { String sentenceId = r.trim(); if (!sentenceId.isEmpty() && sentenceId.matches("\\d+")) { QueryResultContainer.SingleSentenceRelevanceVote singleSentenceVote = new QueryResultContainer.SingleSentenceRelevanceVote(); singleSentenceVote.sentenceID = sentenceId; singleSentenceVote.relevant = "false"; relevanceVote.singleSentenceRelevanceVotes.add(singleSentenceVote); } } mTurkRelevanceVotes.add(relevanceVote); } } } File outputFile = new File(outputDir, f.getName()); FileUtils.writeStringToFile(outputFile, queryResultContainer.toXML(), "utf-8"); System.out.println("Finished " + outputFile); } }
From source file:edu.usc.ir.geo.gazetteer.GeoNameResolver.java
public static void main(String[] args) throws Exception { Option buildOpt = OptionBuilder.withArgName("gazetteer file").hasArg().withLongOpt("build") .withDescription("The Path to the Geonames allCountries.txt").create('b'); Option searchOpt = OptionBuilder.withArgName("set of location names").withLongOpt("search").hasArgs() .withDescription("Location names to search the Gazetteer for").create('s'); Option indexOpt = OptionBuilder.withArgName("directoryPath").withLongOpt("index").hasArgs() .withDescription("The path to the Lucene index directory to either create or read").create('i'); Option helpOpt = OptionBuilder.withLongOpt("help").withDescription("Print this message.").create('h'); Option resultCountOpt = OptionBuilder.withArgName("number of results").withLongOpt("count").hasArgs() .withDescription("Number of best results to be returned for one location").withType(Integer.class) .create('c'); Option serverOption = OptionBuilder.withArgName("Launch Server").withLongOpt("server") .withDescription("Launches Geo Gazetteer Service").create("server"); Option jsonOption = OptionBuilder.withArgName("outputs json").withLongOpt(JSON_OPT) .withDescription("Formats output in well defined json structure").create(JSON_OPT); String indexPath = null;//from ww w . j a v a2 s. c o m String gazetteerPath = null; Options options = new Options(); options.addOption(buildOpt); options.addOption(searchOpt); options.addOption(indexOpt); options.addOption(helpOpt); options.addOption(resultCountOpt); options.addOption(serverOption); options.addOption(jsonOption); // create the parser CommandLineParser parser = new DefaultParser(); GeoNameResolver resolver = new GeoNameResolver(); try { // parse the command line arguments CommandLine line = parser.parse(options, args); if (line.hasOption("index")) { indexPath = line.getOptionValue("index"); } if (line.hasOption("build")) { gazetteerPath = line.getOptionValue("build"); } if (line.hasOption("help")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("lucene-geo-gazetteer", options); System.exit(1); } if (indexPath != null && gazetteerPath != null) { LOG.info("Building Lucene index at path: [" + indexPath + "] with geoNames.org file: [" + gazetteerPath + "]"); resolver.buildIndex(gazetteerPath, indexPath); } if (line.hasOption("search")) { List<String> geoTerms = new ArrayList<String>(Arrays.asList(line.getOptionValues("search"))); String countStr = line.getOptionValue("count", "1"); int count = 1; if (countStr.matches("\\d+")) count = Integer.parseInt(countStr); Map<String, List<Location>> resolved = resolver.searchGeoName(indexPath, geoTerms, count); if (line.hasOption(JSON_OPT)) { writeResultJson(resolved, System.out); } else { writeResult(resolved, System.out); } } else if (line.hasOption("server")) { if (indexPath == null) { System.err.println("Index path is required"); System.exit(-2); } //TODO: get port from CLI args int port = 8765; Launcher.launchService(port, indexPath); } else { System.err.println("Sub command not recognised"); System.exit(-1); } } catch (ParseException exp) { // oops, something went wrong System.err.println("Parsing failed. Reason: " + exp.getMessage()); } }
From source file:com.zimbra.cs.mailbox.util.MetadataDump.java
public static void main(String[] args) { try {/* ww w .j a v a2 s. c o m*/ CliUtil.toolSetup("WARN"); int mboxId = 0; int itemId = 0; PrintStream out = new PrintStream(System.out, true, "utf-8"); CommandLine cl = parseArgs(args); if (cl.hasOption(OPT_HELP)) { usage(null); System.exit(0); } // Get data from file. String infileName = cl.getOptionValue(OPT_FILE); if (infileName != null) { File file = new File(infileName); if (file.exists()) { String encoded = loadFromFile(file); Metadata md = new Metadata(encoded); String pretty = md.prettyPrint(); out.println(pretty); return; } else { System.err.println("File " + infileName + " does not exist"); System.exit(1); } } // Get data from input String. String encoded = cl.getOptionValue(OPT_STR); if (!StringUtil.isNullOrEmpty(encoded)) { Metadata md = new Metadata(encoded); String pretty = md.prettyPrint(); out.println(pretty); return; } // Get data from db. DbPool.startup(); DbConnection conn = null; try { boolean fromDumpster = cl.hasOption(OPT_DUMPSTER); String mboxIdStr = cl.getOptionValue(OPT_MAILBOX_ID); String itemIdStr = cl.getOptionValue(OPT_ITEM_ID); if (mboxIdStr == null || itemIdStr == null) { usage(null); System.exit(1); return; } if (mboxIdStr.matches("\\d+")) { try { mboxId = Integer.parseInt(mboxIdStr); } catch (NumberFormatException e) { System.err.println("Invalid mailbox id " + mboxIdStr); System.exit(1); } } else { conn = DbPool.getConnection(); mboxId = lookupMailboxIdFromEmail(conn, mboxIdStr); } try { itemId = Integer.parseInt(itemIdStr); } catch (NumberFormatException e) { usage(null); System.exit(1); } if (conn == null) conn = DbPool.getConnection(); doDump(conn, mboxId, itemId, fromDumpster, out); } finally { DbPool.quietClose(conn); } } catch (Exception e) { System.err.println(e.getMessage()); System.err.println(); System.err.println(); e.printStackTrace(); System.exit(1); } }
From source file:de.prozesskraft.pkraft.Startinstance.java
public static void main(String[] args) throws org.apache.commons.cli.ParseException, IOException { /*---------------------------- get options from ini-file/*ww w. j a va 2 s. c om*/ ----------------------------*/ java.io.File inifile = new java.io.File(WhereAmI.getInstallDirectoryAbsolutePath(Startinstance.class) + "/" + "../etc/pkraft-startinstance.ini"); if (inifile.exists()) { try { ini = new Ini(inifile); } catch (InvalidFileFormatException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { System.err.println("ini file does not exist: " + inifile.getAbsolutePath()); System.exit(1); } /*---------------------------- create boolean options ----------------------------*/ Option ohelp = new Option("help", "print this message"); Option ov = new Option("v", "prints version and build-date"); /*---------------------------- create argument options ----------------------------*/ Option obasedir = OptionBuilder.withArgName("DIR").hasArg() .withDescription("[optional, default .] base directory where instance shourd run.") // .isRequired() .create("basedir"); Option odefinition = OptionBuilder.withArgName("FILE").hasArg() .withDescription("[optional] definition file of the process you want to start an instance from.") // .isRequired() .create("definition"); Option onostart = OptionBuilder.withArgName("") // .hasArg() .withDescription( "[optional] oppresses the start of the instance. (only create the process-instance)") // .isRequired() .create("nostart"); Option opdomain = OptionBuilder.withArgName("STRING").hasArg() .withDescription("[optional] domain of the process (mandatory if you omit -definition)") // .isRequired() .create("pdomain"); Option opname = OptionBuilder.withArgName("STRING").hasArg().withDescription( "[optional] name of the process you want to start an instance from (mandatory if you omit -definition)") // .isRequired() .create("pname"); Option opversion = OptionBuilder.withArgName("STRING").hasArg().withDescription( "[optional] version of the process you want to start an instance from (mandatory if you omit -definition)") // .isRequired() .create("pversion"); Option ocommitfile = OptionBuilder.withArgName("KEY=FILE; FILE").hasArg() .withDescription("[optional] this file will be committed as file. omit KEY= if KEY==FILENAME.") // .isRequired() .create("commitfile"); Option ocommitvariable = OptionBuilder.withArgName("KEY=VALUE; VALUE").hasArg() .withDescription("[optional] this string will be committed as a variable. omit KEY= if KEY==VALUE") // .isRequired() .create("commitvariable"); Option ocommitfiledummy = OptionBuilder.withArgName("KEY=FILE; FILE").hasArg().withDescription( "[optional] use this parameter like --commitfile. the file will not be checked against the process interface and therefore allows to commit files which are not expected by the process definition. use this parameter only for test purposes e.g. to commit dummy output files for accelerated tests of complex processes or the like.") // .isRequired() .create("commitfiledummy"); Option ocommitvariabledummy = OptionBuilder.withArgName("KEY=VALUE; VALUE").hasArg().withDescription( "[optional] use this parameter like --commitvariable. the variable will not be checked against the process interface and therefore allows to commit variables which are not expected by the process definition. use this parameter only for test purposes e.g. to commit dummy output variables for accelerated tests of complex processes or the like.") // .isRequired() .create("commitvariabledummy"); /*---------------------------- create options object ----------------------------*/ Options options = new Options(); options.addOption(ohelp); options.addOption(ov); options.addOption(obasedir); options.addOption(odefinition); options.addOption(onostart); options.addOption(opname); options.addOption(opversion); options.addOption(ocommitfile); options.addOption(ocommitvariable); options.addOption(ocommitfiledummy); options.addOption(ocommitvariabledummy); /*---------------------------- create the parser ----------------------------*/ CommandLineParser parser = new GnuParser(); // parse the command line arguments commandline = parser.parse(options, args); /*---------------------------- usage/help ----------------------------*/ if (commandline.hasOption("help")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("startinstance", options); System.exit(0); } if (commandline.hasOption("v")) { System.out.println("author: alexander.vogel@caegroup.de"); System.out.println("version: [% version %]"); System.out.println("date: [% date %]"); System.exit(0); } /*---------------------------- ueberpruefen ob eine schlechte kombination von parametern angegeben wurde ----------------------------*/ if (!(commandline.hasOption("definition")) && (!(commandline.hasOption("pname")) || !(commandline.hasOption("pversion")) || !(commandline.hasOption("pdomain")))) { System.err.println("option -definition or the options -pname & -pversion & -pdomain are mandatory"); exiter(); } if ((commandline.hasOption("definition") && ((commandline.hasOption("pversion")) || (commandline.hasOption("pname")) || (commandline.hasOption("pdomain"))))) { System.err.println("you must not use option -definition with -pversion or -pname or -pdomain"); exiter(); } /*---------------------------- die lizenz ueberpruefen und ggf abbrechen ----------------------------*/ // check for valid license ArrayList<String> allPortAtHost = new ArrayList<String>(); allPortAtHost.add(ini.get("license-server", "license-server-1")); allPortAtHost.add(ini.get("license-server", "license-server-2")); allPortAtHost.add(ini.get("license-server", "license-server-3")); MyLicense lic = new MyLicense(allPortAtHost, "1", "user-edition", "0.1"); // lizenz-logging ausgeben for (String actLine : (ArrayList<String>) lic.getLog()) { System.err.println(actLine); } // abbruch, wenn lizenz nicht valide if (!lic.isValid()) { System.exit(1); } /*---------------------------- die eigentliche business logic ----------------------------*/ Process p1 = new Process(); String pathToDefinition = ""; if (commandline.hasOption("definition")) { pathToDefinition = commandline.getOptionValue("definition"); } else if (commandline.hasOption("pname") && commandline.hasOption("pversion") && commandline.hasOption("pdomain")) { pathToDefinition.replaceAll("/+$", ""); pathToDefinition = ini.get("process", "domain-installation-directory") + "/" + commandline.getOptionValue("pdomain") + "/" + commandline.getOptionValue("pname") + "/" + commandline.getOptionValue("pversion") + "/process.xml"; } else { System.err.println("option -definition or the options -pname & -pversion & -pdomain are mandatory"); exiter(); } // check ob das ermittelte oder uebergebene xml-file ueberhaupt existiert java.io.File xmlDefinition = new java.io.File(pathToDefinition); if (!(xmlDefinition.exists()) || !(xmlDefinition.isFile())) { System.err.println("process definition does not exist: " + pathToDefinition); exiter(); } p1.setInfilexml(xmlDefinition.getCanonicalPath()); Process p2 = null; try { p2 = p1.readXml(); } catch (JAXBException e1) { System.err.println(e1.getMessage()); } // das processBinary vorneweg schon mal erstellen, da es sein kann das das committen laenger dauert und ein pradar-attend den neuen prozess schon mal aufnehmen will // root-verzeichnis erstellen if (commandline.hasOption("basedir")) { p2.setBaseDir(commandline.getOptionValue("basedir")); } p2.makeRootdir(); // den pfad fuers binary setzen p2.setOutfilebinary(p2.getRootdir() + "/process.pmb"); System.err.println("info: writing process instance " + p2.getOutfilebinary()); // binary schreiben p2.writeBinary(); // step, an den die commits gehen, soll 'root' sein. Step stepRoot = p2.getRootStep(); // committen von files (ueber einen glob) if (commandline.hasOption("commitfile")) { for (String actOptionCommitfile : commandline.getOptionValues("commitfile")) { String[] parts = actOptionCommitfile.split("="); File userFile = new File(); if (parts.length == 1) { userFile.setKey(new java.io.File(parts[0]).getName()); userFile.setGlob(parts[0]); } else if (parts.length == 2) { userFile.setKey(parts[0]); userFile.setGlob(parts[1]); } else { System.err.println("error in option -commitfile " + actOptionCommitfile); exiter(); } // die auf der kommandozeile uebergebenen Informationen sollen in die vorhandenen commits im rootStep gemappt werden // alle vorhandenen commits in step root durchgehen und dem passenden file zuordnen for (Commit actCommit : stepRoot.getCommit()) { // alle files des aktuellen commits for (File actFile : actCommit.getFile()) { if (actFile.getKey().equals(userFile.getKey())) { // wenn actFile schon ein valider eintrag ist, dann soll ein klon befuellt werden if (actFile.getGlob() != null) { // wenn die maximale erlaubte anzahl noch nicht erreicht ist if (actCommit.getFile(actFile.getKey()).size() < actFile.getMaxoccur()) { File newFile = actFile.clone(); newFile.setGlob(userFile.getGlob()); System.err.println("entering file into commit '" + actCommit.getName() + "' (" + newFile.getKey() + "=" + newFile.getGlob() + ")"); actCommit.addFile(newFile); break; } else { System.err.println("fatal: you only may commit " + actFile.getMaxoccur() + " " + actFile.getKey() + "-files into commit " + actCommit.getName()); exiter(); } } // ansonsten das bereits vorhandene file im commit mit den daten befuellen else { actFile.setGlob(userFile.getGlob()); actFile.setGlobdir(p2.getBaseDir()); System.err.println("entering file into commit '" + actCommit.getName() + "' (" + actFile.getKey() + "=" + actFile.getGlob() + ")"); break; } } } } } } // committen von files (ueber einen glob) if (commandline.hasOption("commitfiledummy")) { // diese files werden nicht in bestehende commits der prozessdefinition eingetragen, sondern in ein spezielles commit Commit commitFiledummy = new Commit(); commitFiledummy.setName("fileDummy"); stepRoot.addCommit(commitFiledummy); for (String actOptionCommitfiledummy : commandline.getOptionValues("commitfiledummy")) { String[] parts = actOptionCommitfiledummy.split("="); File userFile = new File(); commitFiledummy.addFile(userFile); if (parts.length == 1) { userFile.setKey(new java.io.File(parts[0]).getName()); userFile.setGlob(parts[0]); } else if (parts.length == 2) { userFile.setKey(parts[0]); userFile.setGlob(parts[1]); } else { System.err.println("error in option -commitfiledummy " + actOptionCommitfiledummy); exiter(); } userFile.setGlobdir(p2.getBaseDir()); System.err.println("entering (dummy-)file into commit '" + commitFiledummy.getName() + "' (" + userFile.getKey() + "=" + userFile.getGlob() + ")"); } } if (commandline.hasOption("commitvariable")) { for (String actOptionCommitvariable : commandline.getOptionValues("commitvariable")) { if (actOptionCommitvariable.matches(".+=.+")) { String[] parts = actOptionCommitvariable.split("="); Variable userVariable = new Variable(); if (parts.length == 1) { userVariable.setKey("default"); userVariable.setValue(parts[0]); } else if (parts.length == 2) { userVariable.setKey(parts[0]); userVariable.setValue(parts[1]); } else { System.err.println("error in option -commitvariable"); exiter(); } // commit.addVariable(variable); // die auf der kommandozeile uebergebenen Informationen sollen in die vorhandenen commits im rootStep gemappt werden // alle vorhandenen commits in step root durchgehen und dem passenden file zuordnen for (Commit actCommit : stepRoot.getCommit()) { // alle files des aktuellen commits for (Variable actVariable : actCommit.getVariable()) { if (actVariable.getKey().equals(userVariable.getKey())) { // wenn actFile schon ein valider eintrag ist, dann soll ein klon befuellt werden if (actVariable.getGlob() != null) { // wenn die maximale erlaubte anzahl noch nicht erreicht ist if (actCommit.getVariable(actVariable.getKey()).size() < actVariable .getMaxoccur()) { Variable newVariable = actVariable.clone(); newVariable.setValue(userVariable.getValue()); System.err.println("entering variable into commit '" + actCommit.getName() + "' (" + newVariable.getKey() + "=" + newVariable.getValue() + ")"); actCommit.addVariable(newVariable); break; } else { System.err.println("fatal: you only may commit " + actVariable.getMaxoccur() + " " + actVariable.getKey() + "-variable(s) into commit " + actCommit.getName()); exiter(); } } // ansonsten das bereits vorhandene file im commit mit den daten befuellen else { actVariable.setValue(userVariable.getValue()); System.err.println("entering variable into commit '" + actCommit.getName() + "' (" + actVariable.getKey() + "=" + actVariable.getValue() + ")"); break; } } } } } else { System.err.println("-commitvariable " + actOptionCommitvariable + " does not match pattern \"NAME=VALUE\"."); exiter(); } } } if (commandline.hasOption("commitvariabledummy")) { // diese files werden nicht in bestehende commits der prozessdefinition eingetragen, sondern in ein spezielles commit Commit commitVariabledummy = new Commit(); commitVariabledummy.setName("variableDummy"); stepRoot.addCommit(commitVariabledummy); for (String actOptionCommitvariabledummy : commandline.getOptionValues("commitvariabledummy")) { String[] parts = actOptionCommitvariabledummy.split("="); Variable userVariable = new Variable(); commitVariabledummy.addVariable(userVariable); if (parts.length == 1) { userVariable.setKey(parts[0]); userVariable.setValue(parts[0]); } else if (parts.length == 2) { userVariable.setKey(parts[0]); userVariable.setValue(parts[1]); } else { System.err.println("error in option -commitvariabledummy"); exiter(); } System.err.println("entering variable into commit '" + commitVariabledummy.getName() + "' (" + userVariable.getKey() + "=" + userVariable.getValue() + ")"); } } // if (commandline.hasOption("basedir")) // { // p2.setBaseDir(commandline.getOptionValue("basedir")); // } // commit.doIt(); stepRoot.commit(); // root-verzeichnis erstellen p2.makeRootdir(); // den pfad fuers binary setzen // p2.setOutfilebinary(p2.getRootdir() + "/process.pmb"); System.err.println("info: writing process instance " + p2.getOutfilebinary()); // binary schreiben p2.writeBinary(); try { Thread.sleep(1500, 0); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Runtime.getRuntime().exec("process-manager -help"); // starten nur, falls es nicht abgewaehlt wurde if (!commandline.hasOption("nostart")) { System.err.println("info: starting processmanager for instance " + p2.getOutfilebinary()); String aufrufString = ini.get("apps", "pkraft-manager") + " -instance " + p2.getOutfilebinary(); System.err.println("calling: " + aufrufString); ArrayList<String> processSyscallWithArgs = new ArrayList<String>( Arrays.asList(aufrufString.split(" "))); ProcessBuilder pb = new ProcessBuilder(processSyscallWithArgs); // ProcessBuilder pb = new ProcessBuilder("processmanager -instance "+p2.getOutfilebinary()); // Map<String,String> env = pb.environment(); // String path = env.get("PATH"); // System.out.println("PATH: "+path); // java.lang.Process p = pb.start(); System.err.println("pid: " + p.hashCode()); } else { System.err.println("info: NOT starting processmanager for instance " + p2.getOutfilebinary()); } }
From source file:ar.edu.taco.TacoMain.java
/** * @param args/* w w w . j a va 2s.co m*/ */ @SuppressWarnings({ "static-access" }) public static void main(String[] args) { @SuppressWarnings("unused") int loopUnrolling = 3; String tacoVersion = getManifestAttribute(Attributes.Name.IMPLEMENTATION_VERSION); String tacoCreatedBy = getManifestAttribute(new Name("Created-By")); System.out.println("TACO: Taco static analysis tool."); System.out.println("Created By: " + tacoCreatedBy); System.out.println("Version: " + tacoVersion); System.out.println(""); System.out.println(""); Option helpOption = new Option("h", "help", false, "print this message"); Option versionOption = new Option("v", "version", false, "shows version"); Option configFileOption = OptionBuilder.withArgName("path").withLongOpt("configFile").hasArg() .withDescription("set the configuration file").create("cf"); Option classToCheckOption = OptionBuilder.withArgName("classname").withLongOpt("classToCheck").hasArg() .withDescription("set the class to be checked").create('c'); Option methodToCheckOption = OptionBuilder.withArgName("methodname").withLongOpt("methodToCheck").hasArg() .withDescription("set the method to be checked").create('m'); Option dependenciesOption = OptionBuilder.withArgName("classname").withLongOpt("dependencies").hasArgs() .withDescription("additional sources to be parsed").create('d'); Option relevantClassesOption = OptionBuilder.withArgName("classname").withLongOpt("relevantClasses") .hasArgs().withDescription("Set the relevant classes to be used").create("rd"); Option loopsOptions = OptionBuilder.withArgName("integer").withLongOpt("unroll").hasArg() .withDescription("set number of loop unrollings").create('u'); Option bitOptions = OptionBuilder.withArgName("integer").withLongOpt("width").hasArg() .withDescription("set bit width").create('w'); Option instOptions = OptionBuilder.withArgName("integer").withLongOpt("bound").hasArg() .withDescription("set class bound").create('b'); Option skolemizeOption = OptionBuilder.withLongOpt("skolemize") .withDescription("set whether or not skolemize").create("sk"); Option simulateOption = OptionBuilder.withLongOpt("simulate") .withDescription("run method instead of checking").create("r"); Option modularReasoningOption = OptionBuilder.withLongOpt("modularReasoning") .withDescription("check method using modular reasoning").create("mr"); Option relevancyAnalysisOption = OptionBuilder.withLongOpt("relevancyAnalysis") .withDescription("calculate the needed relevantClasses").create("ra"); Option scopeRestrictionOption = OptionBuilder.withLongOpt("scopeRestriction") .withDescription("restrict signature scope to value set in -b option").create("sr"); /* * Option noVerifyOption = OptionBuilder.withLongOpt( * "noVerify").withDescription( * "builds output but does not invoke verification engine").create( * "nv"); */ Options options = new Options(); options.addOption(helpOption); options.addOption(versionOption); options.addOption(configFileOption); options.addOption(classToCheckOption); options.addOption(methodToCheckOption); options.addOption(dependenciesOption); options.addOption(relevantClassesOption); options.addOption(loopsOptions); options.addOption(bitOptions); options.addOption(instOptions); options.addOption(skolemizeOption); options.addOption(simulateOption); options.addOption(modularReasoningOption); options.addOption(relevancyAnalysisOption); options.addOption(scopeRestrictionOption); // options.addOption(noVerifyOption) String configFileArgument = null; Properties overridingProperties = new Properties(); TacoCustomScope tacoScope = new TacoCustomScope(); // create the parser CommandLineParser parser = new PosixParser(); try { // parse the command line arguments CommandLine line = parser.parse(options, args); // help if (line.hasOption(helpOption.getOpt())) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(120, CMD, HEADER, options, FOOTER, true); return; } // version if (line.hasOption(versionOption.getOpt())) { System.out.println(FOOTER); System.out.println(""); return; } // Configuration file if (line.hasOption(configFileOption.getOpt())) { configFileArgument = line.getOptionValue(configFileOption.getOpt()); } // class to check if (line.hasOption(classToCheckOption.getOpt())) { overridingProperties.put(TacoConfigurator.CLASS_TO_CHECK_FIELD, line.getOptionValue(classToCheckOption.getOpt())); } // method to check if (line.hasOption(methodToCheckOption.getOpt())) { String methodtoCheck = line.getOptionValue(methodToCheckOption.getOpt()); if (!methodtoCheck.matches("^[A-Za-z0-9_-]+_[0-9]")) { methodtoCheck = methodtoCheck + "_0"; } overridingProperties.put(TacoConfigurator.METHOD_TO_CHECK_FIELD, methodtoCheck); } // Dependencies classes if (line.hasOption(dependenciesOption.getOpt())) { String dependenciesClasses = ""; for (String aDependencyClass : line.getOptionValues(dependenciesOption.getOpt())) { dependenciesClasses += aDependencyClass; } overridingProperties.put(TacoConfigurator.CLASSES_FIELD, dependenciesClasses); } // Relevant classes if (line.hasOption(relevantClassesOption.getOpt())) { String relevantClasses = ""; for (String aRelevantClass : line.getOptionValues(relevantClassesOption.getOpt())) { relevantClasses += aRelevantClass; } overridingProperties.put(TacoConfigurator.RELEVANT_CLASSES, relevantClasses); } // Loop unrolling if (line.hasOption(loopsOptions.getOpt())) { loopUnrolling = Integer.parseInt(line.getOptionValue(loopsOptions.getOpt())); } // Int bitwidth if (line.hasOption(bitOptions.getOpt())) { String alloy_bitwidth_str = line.getOptionValue(bitOptions.getOpt()); overridingProperties.put(TacoConfigurator.BITWIDTH, alloy_bitwidth_str); int alloy_bitwidth = new Integer(alloy_bitwidth_str); tacoScope.setAlloyBitwidth(alloy_bitwidth); } // instances scope if (line.hasOption(instOptions.getOpt())) { String assertionsArguments = "for " + line.getOptionValue(instOptions.getOpt()); overridingProperties.put(TacoConfigurator.ASSERTION_ARGUMENTS, assertionsArguments); } // Skolemize if (line.hasOption(skolemizeOption.getOpt())) { overridingProperties.put(TacoConfigurator.SKOLEMIZE_INSTANCE_INVARIANT, false); overridingProperties.put(TacoConfigurator.SKOLEMIZE_INSTANCE_ABSTRACTION, false); } // Simulation if (line.hasOption(simulateOption.getOpt())) { overridingProperties.put(TacoConfigurator.INCLUDE_SIMULATION_PROGRAM_DECLARATION, true); overridingProperties.put(TacoConfigurator.GENERATE_CHECK, false); overridingProperties.put(TacoConfigurator.GENERATE_RUN, false); } // Modular Reasoning if (line.hasOption(modularReasoningOption.getOpt())) { overridingProperties.put(TacoConfigurator.MODULAR_REASONING, true); } // Relevancy Analysis if (line.hasOption(relevancyAnalysisOption.getOpt())) { overridingProperties.put(TacoConfigurator.RELEVANCY_ANALYSIS, true); } } catch (ParseException e) { System.err.println("Command line parsing failed: " + e.getMessage()); } try { System.out.println("****** Starting Taco (version. " + tacoVersion + ") ****** "); System.out.println(""); File file = new File("config/log4j.xml"); if (file.exists()) { DOMConfigurator.configure("config/log4j.xml"); } else { System.err.println("log4j:WARN File config/log4j.xml not found"); } TacoMain main = new TacoMain(); // BUILD TacoScope // main.run(configFileArgument, overridingProperties); } catch (IllegalArgumentException e) { System.err.println("Error found:"); System.err.println(e.getMessage()); } catch (MethodToCheckNotFoundException e) { System.err.println("Error found:"); System.err.println("Method to check was not found. Please verify config file, or add -m option"); } catch (TacoException e) { System.err.println("Error found:"); System.err.println(e.getMessage()); } }
From source file:com.wittawat.wordseg.Main.java
public static void main(String[] args) throws Exception { Console con = System.console(); if (con == null) { System.out.println("The system must support console to run the program."); System.exit(1);// w w w .j a v a2s. com } // Load model System.out.println("Loading model ..."); Classifier model = Data.getDefaultModel(); System.out.println("Finished loading model."); System.out.println(getAgreement()); boolean isUseDict = true; // Dummy statement to eliminate all lazy loading System.out.println("\n" + new NukeTokenizer3( "?????", model, isUseDict).tokenize() + "\n"); System.out.println(getHelp()); final String SET_DICT_PAT_STR = "\\s*set\\s+dict\\s+(true|false)\\s*"; final Pattern SET_DICT_PAT = Pattern.compile(SET_DICT_PAT_STR); while (true) { System.out.print(">> "); String line = con.readLine(); if (line != null && !line.trim().equals("")) { line = line.trim(); try { if (line.equals("h") || line.equals("help")) { System.out.println(getHelp()); } else if (line.equals("about")) { System.out.println(getAbout()); } else if (line.equals("agreement")) { System.out.println(getAgreement()); } else if (SET_DICT_PAT.matcher(line).find()) { Matcher m = SET_DICT_PAT.matcher(line); m.find(); String v = m.group(1); isUseDict = v.equals("true"); System.out.println("Dictionary will " + (isUseDict ? "" : "not ") + "be used."); } else if (line.matches("q|quit|exit")) { System.out.println("Bye"); System.exit(0); } else if (line.contains(":tokfile:")) { String[] splits = line.split(":tokfile:"); String in = splits[0]; String out = splits[1]; String content = FileUtils.readFileToString(new File(in)); long start = new Date().getTime(); NukeTokenizer tokenizer = new NukeTokenizer3(content, model, isUseDict); String tokenized = tokenizer.tokenize(); long end = new Date().getTime(); System.out.println("Time to tokenize: " + (end - start) + " ms."); FileUtils.writeStringToFile(new File(out), tokenized); } else if (line.contains(":tokfile")) { String[] splits = line.split(":tokfile"); String in = splits[0]; String content = FileUtils.readFileToString(new File(in)); long start = new Date().getTime(); NukeTokenizer tokenizer = new NukeTokenizer3(content, model, isUseDict); String tokenized = tokenizer.tokenize(); long end = new Date().getTime(); System.out.println(tokenized); System.out.println("Time to tokenize: " + (end - start) + " ms."); } else if (line.contains(":tok:")) { String[] splits = line.split(":tok:"); String inText = splits[0]; String out = splits[1]; long start = new Date().getTime(); NukeTokenizer tokenizer = new NukeTokenizer3(inText, model, isUseDict); String tokenized = tokenizer.tokenize(); long end = new Date().getTime(); System.out.println("Time to tokenize: " + (end - start) + " ms."); FileUtils.writeStringToFile(new File(out), tokenized); } else if (line.contains(":tok")) { String[] splits = line.split(":tok"); String inText = splits[0]; long start = new Date().getTime(); NukeTokenizer tokenizer = new NukeTokenizer3(inText, model, isUseDict); String tokenized = tokenizer.tokenize(); long end = new Date().getTime(); System.out.println(tokenized); System.out.println("Time to tokenize: " + (end - start) + " ms."); } else { System.out.println("Unknown command"); } } catch (Exception e) { System.out.println("Error. See the exception."); e.printStackTrace(); } } } }
From source file:edu.ku.brc.util.GeoRefConverter.java
/** * @param args//from w ww .j a va2 s .c o m * @throws Exception * @throws Exception */ public static void main(String[] args) throws Exception { String destFormat = GeoRefFormat.DMS_PLUS_MINUS.name(); String[] inputStrings = new String[] { // +/- Deg Min Sec "//+/- Deg Min Sec", "0 0 0", "0 0 0.", "-32 45 16.8232", "-32d 45' 16.8232\"", "-32d45'16.8232\"", "-3245'16.8232\"", "-32 45' 16.82\"", "-32 45 16.82", "-32 45 6.8232", "-32 45 6.82", "-32 45 0.82", "-132 45 16.82151", "-132 45 6.82", "32 45 16.8232", "32 45 16.82", "32 45 6.8232", "32 45 6.82", "32 45 0.82", "132 45 16.82151", "132 45 6.82", // Deg Min Sec N/S/E/W "//Deg Min Sec N/S/E/W", "32 45 16.8232 N", "32 45 16.82 N", "32d45'16.82\" N", "32d45'16.82\"N", "32d 45' 16.82\" N", "32 45' 16.82\" N", "32 45 16.82 N", "32 45 6.8232 N", "32 45 6.82 N", "32 45 0.82 N", "132 45 16.82151 N", "132 45 6.82 N", "32 45 16.8232 S", "32 45 16.82 S", "32 45 6.8232 S", "32 45 6.82 S", "32 45 0.82 S", "132 45 16.82151 S", "132 45 6.82 S", "32 45 16.8232 E", "32 45 16.82 E", "32 45 6.8232 E", "32 45 6.82 E", "32 45 0.82 E", "132 45 16.82151 E", "132 45 6.82 E", "32 45 16.8232 W", "32 45 16.82 W", "32 45 6.8232 W", "32 45 6.82 W", "32 45 0.82 W", "132 45 16.82151 W", "132 45 6.82 W", // +/- Deg Min "//+/- Deg Min", "0 0", "0 0.", "-32 16.8232", "-32 16.82", "-32 16.82'", "-3216.82", "-32d 16", "-32 16.82", "-32 6.8232", "-32 6.82", "-32 0.82", "-132 16.82151", "-132 6.82", "32 16.8232", "32 16.82", "32 6.8232", "32 6.82", "32 0.82", "132 16.82151", "132 6.82", // Deg Min N/S/E/W "//Deg Min N/S/E/W", "32 16.8232 N", "32 16.82 N", "32 6.8232 N", "32 6.82 N", "32 0.82 N", "132 16.82151 N", "132 6.82 N", "32 16.8232 S", "32 16.82 S", "32 6.8232 S", "32 6.82 S", "32 0.82 S", "132 16.82151 S", "132 6.82 S", "32 16.8232 E", "32 16.82 E", "32 6.8232 E", "32 6.82 E", "32 0.82 E", "132 16.82151 E", "132 6.82 E", "32 16.8232 W", "32 16.82 W", "32 6.8232 W", "32 6.82 W", "32 0.82 W", "132 16.82151 W", "132 6.82 W", // +/- Decimal Degrees "//+/- Decimal Degrees", "0", "0.", "-16.8232", "-16.8232", "-16.82", "-6.8232", "-6.82", "-0.82", "-116.82151", "-116.82", "-1.82", "16.8232", "16.82", "6.8232", "6.82", "0.82", "116.82151", "116.82", "1.82", // Decimal Degrees N/S/E/W "//Decimal Degrees N/S/E/W", "16.8232 N", "16.82 N", "16.8232 N", "16.82 N", "16.8232N", "16.82N", "6.8232 N", "6.82 N", "0.82 N", "116.82151 N", "116.82 N", "1.82 N", "16.8232 S", "16.82 S", "6.8232 S", "6.82 S", "0.82 S", "116.82151 S", "116.82 S", "1.82 S", "16.8232 E", "16.82 E", "6.8232 E", "6.82 E", "0.82 E", "116.82151 E", "116.82 E", "1.82 E", "16.8232 W", "16.82 W", "6.8232 W", "6.82 W", "0.82 W", "116.82151 W", "116.82 W", "1.82 W", "41 43." }; for (String input : inputStrings) { if (input.length() == 0) { continue; } if (input.startsWith("//")) { System.out.println(); System.out.println("----------------------------------"); System.out.println("----------------------------------"); System.out.println(input.substring(2)); System.out.println("----------------------------------"); System.out.println("----------------------------------"); continue; } System.out.println("Input: " + input); BigDecimal degreesPlusMinus = null; for (GeoRefFormat format : GeoRefFormat.values()) { if (input.matches(format.regex)) { System.out.println("Format match: " + format.name()); degreesPlusMinus = format.convertToDecimalDegrees(input); break; } } // if we weren't able to find a matching format, throw an exception if (degreesPlusMinus == null) { System.out.println("No matching format found"); System.out.println("----------------------------------"); continue; } int decimalFmtLen = 0; int decIndex = input.lastIndexOf('.'); if (decIndex > -1 && input.length() > decIndex) { decimalFmtLen = input.length() - decIndex; } String convertedVal = null; if (destFormat == GeoRefFormat.DMS_PLUS_MINUS.name()) { convertedVal = LatLonConverter.convertToSignedDDMMSS(degreesPlusMinus, Math.min(LatLonConverter.DDMMSS_LEN, decimalFmtLen)); } else if (destFormat == GeoRefFormat.DM_PLUS_MINUS.name()) { convertedVal = LatLonConverter.convertToSignedDDMMMM(degreesPlusMinus, Math.min(LatLonConverter.DDMMMM_LEN, decimalFmtLen)); } else if (destFormat == GeoRefFormat.D_PLUS_MINUS.name()) { convertedVal = LatLonConverter.convertToSignedDDDDDD(degreesPlusMinus, Math.min(LatLonConverter.DDDDDD_LEN, decimalFmtLen)); } System.out.println("Converted value: " + convertedVal); System.out.println("----------------------------------"); } GeoRefConverter converter = new GeoRefConverter(); for (String input : inputStrings) { if (input.length() == 0) { continue; } if (input.startsWith("//")) { System.out.println(); System.out.println("----------------------------------"); System.out.println("----------------------------------"); System.out.println(input.substring(2)); System.out.println("----------------------------------"); System.out.println("----------------------------------"); continue; } System.out.println("Input: " + input); String decimalDegrees = converter.convert(input, GeoRefConverter.GeoRefFormat.D_PLUS_MINUS.name()); System.out.println("Decimal degrees: " + decimalDegrees); } System.out.println("----------------------------------"); System.out.println("----------------------------------"); System.out.println("----------------------------------"); System.out.println("----------------------------------"); System.out.println("----------------------------------"); System.out.println("----------------------------------"); System.out.println("----------------------------------"); System.out.println("----------------------------------"); String problemString = "41 43 18."; System.out.println("input: " + problemString); String d = converter.convert(problemString, GeoRefFormat.D_PLUS_MINUS.name()); String dm = converter.convert(problemString, GeoRefFormat.DM_PLUS_MINUS.name()); String dms = converter.convert(problemString, GeoRefFormat.DMS_PLUS_MINUS.name()); System.out.println(d + " : " + dm + " : " + dms); problemString = d; System.out.println("input: " + problemString); String d2 = converter.convert(problemString, GeoRefFormat.D_PLUS_MINUS.name()); String dm2 = converter.convert(problemString, GeoRefFormat.DM_PLUS_MINUS.name()); String dms2 = converter.convert(problemString, GeoRefFormat.DMS_PLUS_MINUS.name()); System.out.println(d2 + " : " + dm2 + " : " + dms2); problemString = dm; System.out.println("input: " + problemString); String d3 = converter.convert(problemString, GeoRefFormat.D_PLUS_MINUS.name()); String dm3 = converter.convert(problemString, GeoRefFormat.DM_PLUS_MINUS.name()); String dms3 = converter.convert(problemString, GeoRefFormat.DMS_PLUS_MINUS.name()); System.out.println(d3 + " : " + dm3 + " : " + dms3); }
From source file:Cresendo.java
public static void main(String[] args) { String cfgFileReceiver = null; // Path to config file for eif receiver agent String cfgFileEngine = null; // Path to config file for xml event engine Options opts = null; // Command line options HelpFormatter hf = null; // Command line help formatter // Setup the message record which will contain text written to the log file ///*from ww w . ja v a2 s .c om*/ // The message logger object is created when the "-l" is processed // as this object need to be associated with a log file // LogRecord msg = new LogRecord(LogRecord.TYPE_INFO, "Cresendo", "main", "", "", "", "", ""); // Get the directory separator (defaults to "/") // dirSep = System.getProperty("file.separator", "/"); // Initialise the structure containing the event handler objects // Vector<IEventHandler> eventHandler = new Vector<IEventHandler>(10, 10); // Process the command line arguments // try { opts = new Options(); hf = new HelpFormatter(); opts.addOption("h", "help", false, "Command line arguments help"); opts.addOption("i", "instance name", true, "Name of cresendo instance"); opts.addOption("l", "log dir", true, "Path to log file directory"); opts.addOption("c", "config dir", true, "Path to configuarion file directory"); opts.getOption("l").setRequired(true); opts.getOption("c").setRequired(true); BasicParser parser = new BasicParser(); CommandLine cl = parser.parse(opts, args); // Print out some help and exit // if (cl.hasOption('h')) { hf.printHelp("Options", opts); System.exit(0); } // Set the instance name // if (cl.hasOption('i')) { instanceName = cl.getOptionValue('i'); // Set to something other than "default" } // Setup the message and trace logging objects for the EventEngine // if (cl.hasOption('l')) { // Setup the the paths to the message, trace and status log files // logDir = cl.getOptionValue("l"); logPath = logDir + dirSep + instanceName + "-engine.log"; tracePath = logDir + dirSep + instanceName + "-engine.trace"; statusPath = logDir + dirSep + instanceName + "-engine.status"; } else { // NOTE: This should be picked up by the MissingOptionException catch below // but I couldn't get this to work so I added the following code: // hf.printHelp("Option 'l' is a required option", opts); System.exit(1); } // Read the receiver and engine config files in the config directory // if (cl.hasOption('c')) { // Setup and check path to eif config file for TECAgent receiver object // configDir = cl.getOptionValue("c"); cfgFileReceiver = configDir + dirSep + instanceName + ".conf"; checkConfigFile(cfgFileReceiver); // Setup and check path to xml config file for the EventEngine // cfgFileEngine = cl.getOptionValue("c") + dirSep + instanceName + ".xml"; checkConfigFile(cfgFileEngine); } else { // NOTE: This should be picked up by the MissingOptionException catch below // but I couldn't get this to work so I added the following code: // hf.printHelp("Option 'c' is a required option", opts); System.exit(1); } } catch (UnrecognizedOptionException e) { hf.printHelp(e.toString(), opts); System.exit(1); } catch (MissingOptionException e) { hf.printHelp(e.toString(), opts); System.exit(1); } catch (MissingArgumentException e) { hf.printHelp(e.toString(), opts); System.exit(1); } catch (ParseException e) { e.printStackTrace(); System.exit(1); } catch (Exception e) { System.err.println(e.toString()); System.exit(1); } // Main program // try { // ===================================================================== // Setup the message, trace and status logger objects // try { msgHandler = new FileHandler("cresendo", "message handler", logPath); msgHandler.openDevice(); msgLogger = new MessageLogger("cresendo", "message log"); msgLogger.addHandler(msgHandler); trcHandler = new FileHandler("cresendo", "trace handler", tracePath); trcHandler.openDevice(); trcLogger = new TraceLogger("cresendo", "trace log"); trcLogger.addHandler(trcHandler); statLogger = new StatusLogger(statusPath); } catch (Exception e) { System.err.println(e.toString()); System.exit(1); } // Add the shutdown hook // Runtime.getRuntime().addShutdownHook(new ShutdownThread(msgLogger, instanceName)); // --------------------------------------------------------------------- // ===================================================================== // Load and parse the xml event engine configuration file // // msg.setText("Loading xml engine from: '" + cfgFileEngine + "'"); try { XMLConfiguration xmlProcessor = new XMLConfiguration(); xmlProcessor.setFileName(cfgFileEngine); // Validate the xml against a document type declaration // xmlProcessor.setValidating(true); // Don't interpolate the tag contents by splitting them on a delimiter // (ie by default a comma) // xmlProcessor.setDelimiterParsingDisabled(true); // This will throw a ConfigurationException if the xml document does not // conform to its dtd. By doing this we hopefully catch any errors left // behind after the xml configuration file has been edited. // xmlProcessor.load(); // Setup the trace flag // ConfigurationNode engine = xmlProcessor.getRootNode(); List rootAttribute = engine.getAttributes(); for (Iterator it = rootAttribute.iterator(); it.hasNext();) { ConfigurationNode attr = (ConfigurationNode) it.next(); String attrName = attr.getName(); String attrValue = (String) attr.getValue(); if (attrValue == null || attrValue == "") { System.err.println("\n Error: The value of the attribute '" + attrName + "'" + "\n in the xml file '" + cfgFileEngine + "'" + "\n is not set"); System.exit(1); } if (attrName.matches("trace")) { if (attrValue.matches("true") || attrValue.matches("on")) { trcLogger.setLogging(true); } } if (attrName.matches("status")) { if (attrValue.matches("true") || attrValue.matches("on")) { statLogger.setLogging(true); } else { statLogger.setLogging(false); } } if (attrName.matches("interval")) { if (!attrValue.matches("[0-9]+")) { System.err.println("\n Error: The value of the interval attribute in: '" + cfgFileEngine + "'" + "\n should only contain digits from 0 to 9." + "\n It currently contains: '" + attrValue + "'"); System.exit(1); } statLogger.setInterval(Integer.parseInt(attrValue)); } } // Now build and instantiate the list of classes that will process events // received by the TECAgent receiver in a chain like manner. // List classes = xmlProcessor.configurationsAt("class"); for (Iterator it = classes.iterator(); it.hasNext();) { HierarchicalConfiguration sub = (HierarchicalConfiguration) it.next(); // sub contains now all data contained in a single <class></class> tag set // String className = sub.getString("name"); // Log message // msg.setText(msg.getText() + "\n Instantiated event handler class: '" + className + "'"); // The angle brackets describing the class of object held by the // Vector are implemented by Java 1.5 and have 2 effects. // // 1. The list accepts only elements of that class and nothing else // (Of course thanks to Auto-Wrap you can also add double-values) // // 2. the get(), firstElement() ... Methods don't return a Object, but // they deliver an element of the class. // Vector<Class> optTypes = new Vector<Class>(10, 10); Vector<Object> optValues = new Vector<Object>(10, 10); for (int i = 0; i <= sub.getMaxIndex("option"); i++) { Object optValue = null; String optVarName = sub.getString("option(" + i + ")[@varname]"); String optJavaType = sub.getString("option(" + i + ")[@javatype]"); // Use the specified java type in order to make the method call // to the heirarchical sub object [painful :-((] // if (optJavaType.matches("byte")) { optTypes.addElement(byte.class); optValue = sub.getByte("option(" + i + ")"); if (optValue == null) // Catch nulls { optValue = 0; // Set to something nullish } } else if (optJavaType.matches("short")) { optTypes.addElement(byte.class); optValue = sub.getShort("option(" + i + ")"); if (optValue == null) // Catch nulls { optValue = 0; // Set to something nullish } } else if (optJavaType.matches("int")) { optTypes.addElement(int.class); optValue = sub.getInt("option(" + i + ")"); if (optValue == null) // Catch nulls { optValue = 0; // Set to something nullish } } else if (optJavaType.matches("long")) { optTypes.addElement(long.class); optValue = sub.getLong("option(" + i + ")"); if (optValue == null) // Catch nulls { optValue = 0; // Set to something nullish } } else if (optJavaType.matches("float")) { optTypes.addElement(float.class); optValue = sub.getFloat("option(" + i + ")"); if (optValue == null) // Catch nulls { optValue = 0.0; // Set to something nullish } } else if (optJavaType.matches("double")) { optTypes.addElement(double.class); optValue = sub.getDouble("option(" + i + ")"); if (optValue == null) // Catch nulls { optValue = 0.0; // Set to something nullish } } else if (optJavaType.matches("boolean")) { optTypes.addElement(boolean.class); optValue = sub.getBoolean("option(" + i + ")"); if (optValue == null) // Catch nulls { optValue = false; // Set to something nullish } } else if (optJavaType.matches("String")) { optTypes.addElement(String.class); optValue = sub.getString("option(" + i + ")"); if (optValue == null) // Catch nulls { optValue = ""; // Set it to something nullish } } else { System.err.println( "Error: Unsupported java type found in xml config: '" + optJavaType + "'"); System.exit(1); } // Add option value element // // System.out.println("Option value is: '" + optValue.toString() + "'\n"); // optValues.addElement(optValue); // Append to message text // String msgTemp = msg.getText(); msgTemp += "\n option name: '" + optVarName + "'"; msgTemp += "\n option type: '" + optJavaType + "'"; msgTemp += "\n option value: '" + optValues.lastElement().toString() + "'"; msg.setText(msgTemp); } try { // Instantiate the class with the java reflection api // Class klass = Class.forName(className); // Setup an array of paramater types in order to retrieve the matching constructor // Class[] types = optTypes.toArray(new Class[optTypes.size()]); // Get the constructor for the class which matches the parameter types // Constructor konstruct = klass.getConstructor(types); // Create an instance of the event handler // IEventHandler eventProcessor = (IEventHandler) konstruct.newInstance(optValues.toArray()); // Add the instance to the list of event handlers // eventHandler.addElement(eventProcessor); } catch (InvocationTargetException e) { System.err.println("Error: " + e.toString()); System.exit(1); } catch (ClassNotFoundException e) { System.err.println("Error: class name not found: '" + className + "' \n" + e.toString()); System.exit(1); } catch (Exception e) { System.err.println( "Error: failed to instantiate class: '" + className + "' \n" + e.toString()); System.exit(1); } } } catch (ConfigurationException cex) // Something went wrong loading the xml file { System.err.println("\n" + "Error loading XML file: " + cfgFileEngine + "\n" + cex.toString()); System.exit(1); } catch (Exception e) { System.err.println(e.toString()); System.exit(1); } // --------------------------------------------------------------------- // ===================================================================== // Setup the TECAgent receiver // Reader cfgIn = null; try { cfgIn = new FileReader(cfgFileReceiver); } catch (Exception e) { System.err.println(e.toString()); System.exit(1); } // Start the TECAgent receiver and register the event engine handler // TECAgent receiver = new TECAgent(cfgIn, TECAgent.RECEIVER_MODE, false); EventEngine ee = new EventEngine(eventHandler, msgLogger, trcLogger); receiver.registerListener(ee); // Construct message and send it to the message log // String text = "\n Cresendo instance '" + instanceName + "' listening for events on port '" + receiver.getConfigVal("ServerPort") + "'"; msg.setText(msg.getText() + text); msgLogger.log(msg); // Send message to log // --------------------------------------------------------------------- // ===================================================================== // Initiate status logging // if (statLogger.isLogging()) { int seconds = statLogger.getInterval(); while (true) { try { statLogger.log(); } catch (Exception ex) { System.err.println("\n An error occurred while writing to '" + statusPath + "'" + "\n '" + ex.toString() + "'"); } Thread.sleep(seconds * 1000); // Convert sleep time to milliseconds } } // --------------------------------------------------------------------- } catch (Exception e) { System.err.println(e.toString()); System.exit(1); } }
From source file:examples.mail.IMAPExportMbox.java
public static void main(String[] args) throws IOException { int connect_timeout = CONNECT_TIMEOUT; int read_timeout = READ_TIMEOUT; int argIdx = 0; String eol = EOL_DEFAULT;/*from ww w . j av a2 s . c o m*/ boolean printHash = false; boolean printMarker = false; int retryWaitSecs = 0; for (argIdx = 0; argIdx < args.length; argIdx++) { if (args[argIdx].equals("-c")) { connect_timeout = Integer.parseInt(args[++argIdx]); } else if (args[argIdx].equals("-r")) { read_timeout = Integer.parseInt(args[++argIdx]); } else if (args[argIdx].equals("-R")) { retryWaitSecs = Integer.parseInt(args[++argIdx]); } else if (args[argIdx].equals("-LF")) { eol = LF; } else if (args[argIdx].equals("-CRLF")) { eol = CRLF; } else if (args[argIdx].equals("-.")) { printHash = true; } else if (args[argIdx].equals("-X")) { printMarker = true; } else { break; } } final int argCount = args.length - argIdx; if (argCount < 2) { System.err.println("Usage: IMAPExportMbox [-LF|-CRLF] [-c n] [-r n] [-R n] [-.] [-X]" + " imap[s]://user:password@host[:port]/folder/path [+|-]<mboxfile> [sequence-set] [itemnames]"); System.err.println( "\t-LF | -CRLF set end-of-line to LF or CRLF (default is the line.separator system property)"); System.err.println("\t-c connect timeout in seconds (default 10)"); System.err.println("\t-r read timeout in seconds (default 10)"); System.err.println("\t-R temporary failure retry wait in seconds (default 0; i.e. disabled)"); System.err.println("\t-. print a . for each complete message received"); System.err.println("\t-X print the X-IMAP line for each complete message received"); System.err.println( "\tthe mboxfile is where the messages are stored; use '-' to write to standard output."); System.err.println( "\tPrefix filename with '+' to append to the file. Prefix with '-' to allow overwrite."); System.err.println( "\ta sequence-set is a list of numbers/number ranges e.g. 1,2,3-10,20:* - default 1:*"); System.err .println("\titemnames are the message data item name(s) e.g. BODY.PEEK[HEADER.FIELDS (SUBJECT)]" + " or a macro e.g. ALL - default (INTERNALDATE BODY.PEEK[])"); System.exit(1); } final URI uri = URI.create(args[argIdx++]); final String file = args[argIdx++]; String sequenceSet = argCount > 2 ? args[argIdx++] : "1:*"; final String itemNames; // Handle 0, 1 or multiple item names if (argCount > 3) { if (argCount > 4) { StringBuilder sb = new StringBuilder(); sb.append("("); for (int i = 4; i <= argCount; i++) { if (i > 4) { sb.append(" "); } sb.append(args[argIdx++]); } sb.append(")"); itemNames = sb.toString(); } else { itemNames = args[argIdx++]; } } else { itemNames = "(INTERNALDATE BODY.PEEK[])"; } final boolean checkSequence = sequenceSet.matches("\\d+:(\\d+|\\*)"); // are we expecting a sequence? final MboxListener chunkListener; if (file.equals("-")) { chunkListener = null; } else if (file.startsWith("+")) { final File mbox = new File(file.substring(1)); System.out.println("Appending to file " + mbox); chunkListener = new MboxListener(new BufferedWriter(new FileWriter(mbox, true)), eol, printHash, printMarker, checkSequence); } else if (file.startsWith("-")) { final File mbox = new File(file.substring(1)); System.out.println("Writing to file " + mbox); chunkListener = new MboxListener(new BufferedWriter(new FileWriter(mbox, false)), eol, printHash, printMarker, checkSequence); } else { final File mbox = new File(file); if (mbox.exists()) { throw new IOException("mailbox file: " + mbox + " already exists!"); } System.out.println("Creating file " + mbox); chunkListener = new MboxListener(new BufferedWriter(new FileWriter(mbox)), eol, printHash, printMarker, checkSequence); } String path = uri.getPath(); if (path == null || path.length() < 1) { throw new IllegalArgumentException("Invalid folderPath: '" + path + "'"); } String folder = path.substring(1); // skip the leading / // suppress login details final PrintCommandListener listener = new PrintCommandListener(System.out, true) { @Override public void protocolReplyReceived(ProtocolCommandEvent event) { if (event.getReplyCode() != IMAPReply.PARTIAL) { // This is dealt with by the chunk listener super.protocolReplyReceived(event); } } }; // Connect and login final IMAPClient imap = IMAPUtils.imapLogin(uri, connect_timeout * 1000, listener); String maxIndexInFolder = null; try { imap.setSoTimeout(read_timeout * 1000); if (!imap.select(folder)) { throw new IOException("Could not select folder: " + folder); } for (String line : imap.getReplyStrings()) { maxIndexInFolder = matches(line, PATEXISTS, 1); if (maxIndexInFolder != null) { break; } } if (chunkListener != null) { imap.setChunkListener(chunkListener); } // else the command listener displays the full output without processing while (true) { boolean ok = imap.fetch(sequenceSet, itemNames); // If the fetch failed, can we retry? if (!ok && retryWaitSecs > 0 && chunkListener != null && checkSequence) { final String replyString = imap.getReplyString(); //includes EOL if (startsWith(replyString, PATTEMPFAIL)) { System.err.println("Temporary error detected, will retry in " + retryWaitSecs + "seconds"); sequenceSet = (chunkListener.lastSeq + 1) + ":*"; try { Thread.sleep(retryWaitSecs * 1000); } catch (InterruptedException e) { // ignored } } else { throw new IOException( "FETCH " + sequenceSet + " " + itemNames + " failed with " + replyString); } } else { break; } } } catch (IOException ioe) { String count = chunkListener == null ? "?" : Integer.toString(chunkListener.total); System.err.println("FETCH " + sequenceSet + " " + itemNames + " failed after processing " + count + " complete messages "); if (chunkListener != null) { System.err.println("Last complete response seen: " + chunkListener.lastFetched); } throw ioe; } finally { if (printHash) { System.err.println(); } if (chunkListener != null) { chunkListener.close(); final Iterator<String> missingIds = chunkListener.missingIds.iterator(); if (missingIds.hasNext()) { StringBuilder sb = new StringBuilder(); for (;;) { sb.append(missingIds.next()); if (!missingIds.hasNext()) { break; } sb.append(","); } System.err.println("*** Missing ids: " + sb.toString()); } } imap.logout(); imap.disconnect(); } if (chunkListener != null) { System.out.println("Processed " + chunkListener.total + " messages."); } if (maxIndexInFolder != null) { System.out.println("Folder contained " + maxIndexInFolder + " messages."); } }
From source file:ReplayTest.java
public static void main(String[] args) throws IOException { int cnt = 0;/*from ww w . j av a 2 s. c o m*/ String operateTime = ""; String operateType = ""; String uuid = ""; String programId = ""; List<String> lines = Files.readLines(new File("e:\\test\\sample4.txt"), Charsets.UTF_8); System.out.println(lines.size()); for (String value1 : lines) { String[] values = value1.split(SPLIT_T); //logArr16?operateDate=2014-04-25 17:59:59 621, operateType=STARTUP, deviceCode=010333501065233, versionId=, mac=10:48:b1:06:4d:23, platformId=00000032AmlogicMDZ-05-201302261821793, ipAddress=60.10.133.10 if (values.length != 16) { continue; } String logContent = values[15]; if (logContent == null || logContent.trim().length() <= 0) { System.out.println("logContent"); return; } String[] contentArr = logContent.split(COMMA_SIGN);//content if (contentArr == null || contentArr.length != 3) { System.out.println("logContentArr:" + contentArr.length); return; } StringBuffer stringBuffer = new StringBuffer(); //1.CNTVID?? stringBuffer.append(StringsUtils.getEncodeingStr(values[3])).append(SPLIT); //2.IP? if (null == values[7] || EMPTY.equals(values[7])) { stringBuffer.append(StringsUtils.getEncodeingStr(EMPTY)).append(SPLIT); } else { stringBuffer.append(StringsUtils.getEncodeingStr(values[7].trim())).append(SPLIT); } //3.OperateTtype ? 1: 2:? operateType = StringUtils.substringAfter(contentArr[0].trim(), EQUAL_SIGN); if (null == operateType || EMPTY.equals(operateType)) { stringBuffer.append(StringsUtils.getEncodeingStr(EMPTY)).append(SPLIT); } else if ("on".equals(operateType)) { stringBuffer.append(StringsUtils.getEncodeingStr("1")).append(SPLIT); } else if ("out".equals(operateType)) { stringBuffer.append(StringsUtils.getEncodeingStr("2")).append(SPLIT); } // 4.operateTime ? operateTime = DateUtil.convertDateToString("yyyyMMdd HHmmss", DateUtil.convertStringToDate("yyyy-MM-dd HH:mm:ss SSS", values[10].trim())); if (operateTime == null || EMPTY.equals(operateTime)) { stringBuffer.append(StringsUtils.getEncodeingStr(EMPTY)).append(SPLIT); } else { stringBuffer.append(StringsUtils.getEncodeingStr(operateTime)).append(SPLIT); } //5.url_addr ? stringBuffer.append(StringsUtils.getEncodeingStr(EMPTY)).append(SPLIT); //6.channel? uuid = StringUtils.substringAfter(contentArr[1].trim(), EQUAL_SIGN); if (uuid == null || EMPTY.equals(uuid)) { stringBuffer.append(StringsUtils.getEncodeingStr(EMPTY)).append(SPLIT); } else { stringBuffer.append(StringsUtils.getEncodeingStr(uuid)).append(SPLIT); } //7.programId id programId = StringUtils.substringAfter(contentArr[2].trim(), EQUAL_SIGN); if (!programId.matches("\\d+")) { //id???? return; } else { stringBuffer.append(StringsUtils.getEncodeingStr(programId)).append(SPLIT); } //8.EPGCode EPG?,?EPGCode? stringBuffer.append(StringsUtils.getEncodeingStr("06")).append(SPLIT); //9.DataSource??12 stringBuffer.append(DATA_SOURCE).append(SPLIT); //10.Fsource??????? stringBuffer.append(F_SOURCE).append(SPLIT); //11.resolution ?,? stringBuffer.append(StringsUtils.getEncodeingStr(EMPTY)); System.out.println(stringBuffer.toString()); cnt++; } System.out.println(":" + cnt); }