List of usage examples for java.lang String substring
public String substring(int beginIndex, int endIndex)
From source file:GoogleImages.java
public static void main(String[] args) throws InterruptedException { String searchTerm = "s woman"; // term to search for (use spaces to separate terms) int offset = 40; // we can only 20 results at a time - use this to offset and get more! String fileSize = "50mp"; // specify file size in mexapixels (S/M/L not figured out yet) String source = null; // string to save raw HTML source code // format spaces in URL to avoid problems searchTerm = searchTerm.replaceAll(" ", "%20"); // get Google image search HTML source code; mostly built from PhyloWidget example: // http://code.google.com/p/phylowidget/source/browse/trunk/PhyloWidget/src/org/phylowidget/render/images/ImageSearcher.java int offset2 = 0; Set urlsss = new HashSet<String>(); while (offset2 < 600) { try {//ww w .jav a 2s .c o m URL query = new URL("https://www.google.ru/search?start=" + offset2 + "&q=angry+woman&newwindow=1&client=opera&hs=bPE&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiAgcKozIfNAhWoHJoKHSb_AUoQ_AUIBygB&biw=1517&bih=731&dpr=0.9#imgrc=G_1tH3YOPcc8KM%3A"); HttpURLConnection urlc = (HttpURLConnection) query.openConnection(); // start connection... urlc.setInstanceFollowRedirects(true); urlc.setRequestProperty("User-Agent", ""); urlc.connect(); BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream())); // stream in HTTP source to file StringBuffer response = new StringBuffer(); char[] buffer = new char[1024]; while (true) { int charsRead = in.read(buffer); if (charsRead == -1) { break; } response.append(buffer, 0, charsRead); } in.close(); // close input stream (also closes network connection) source = response.toString(); } // any problems connecting? let us know catch (Exception e) { e.printStackTrace(); } // print full source code (for debugging) // println(source); // extract image URLs only, starting with 'imgurl' if (source != null) { // System.out.println(source); int c = StringUtils.countMatches(source, "http://www.vmir.su"); System.out.println(c); int index = source.indexOf("src="); System.out.println(source.subSequence(index, index + 200)); while (index >= 0) { System.out.println(index); index = source.indexOf("src=", index + 1); if (index == -1) { break; } String rr = source.substring(index, index + 200 > source.length() ? source.length() : index + 200); if (rr.contains("\"")) { rr = rr.substring(5, rr.indexOf("\"", 5)); } System.out.println(rr); urlsss.add(rr); } } offset2 += 20; Thread.sleep(1000); System.out.println("off set = " + offset2); } System.out.println(urlsss); urlsss.forEach(new Consumer<String>() { public void accept(String s) { try { saveImage(s, "C:\\Users\\Java\\Desktop\\ang\\" + UUID.randomUUID().toString() + ".jpg"); } catch (IOException ex) { Logger.getLogger(GoogleImages.class.getName()).log(Level.SEVERE, null, ex); } } }); // String[][] m = matchAll(source, "img height=\"\\d+\" src=\"([^\"]+)\""); // older regex, no longer working but left for posterity // built partially from: http://www.mkyong.com/regular-expressions/how-to-validate-image-file-extension-with-regular-expression // String[][] m = matchAll(source, "imgurl=(.*?\\.(?i)(jpg|jpeg|png|gif|bmp|tif|tiff))"); // (?i) means case-insensitive // for (int i = 0; i < m.length; i++) { // iterate all results of the match // println(i + ":\t" + m[i][1]); // print (or store them)** // } }
From source file:com.icebreak.p2p.trade.impl.TradeServiceImpl.java
public static void main(String[] args) { String guaranteeLicenseNoNew = ""; String guaranteeLicenseNo = "??TRB?[2013]001?"; String strDate = "20131112"; int count = 1; if (guaranteeLicenseNo.indexOf("?") > 0) { String prefix = guaranteeLicenseNo.substring(0, guaranteeLicenseNo.indexOf("?")); guaranteeLicenseNoNew = prefix + "[" + strDate.substring(0, 4) + "]" + strDate.substring(4, strDate.length()) + StringUtils.leftPad(String.valueOf(++count), 3, "0") + "?"; } else {//from w ww. j a va2s.c om String prefix = guaranteeLicenseNo.substring(0, guaranteeLicenseNo.indexOf("TRB") + 3); guaranteeLicenseNoNew = prefix + "[" + strDate.substring(0, 4) + "]" + strDate.substring(4, strDate.length()) + StringUtils.leftPad(String.valueOf(++count), 3, "0") + "?"; } System.out.println(guaranteeLicenseNoNew); }
From source file:ch.kostceco.tools.kostval.KOSTVal.java
/** Die Eingabe besteht aus 2 oder 3 Parameter: [0] Validierungstyp [1] Pfad zur Val-File [2] * option: Verbose/*from w w w . ja va2s .c om*/ * * @param args * @throws IOException */ @SuppressWarnings("unused") public static void main(String[] args) throws IOException { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:config/applicationContext.xml"); // Zeitstempel Start java.util.Date nowStart = new java.util.Date(); java.text.SimpleDateFormat sdfStart = new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); String ausgabeStart = sdfStart.format(nowStart); /* TODO: siehe Bemerkung im applicationContext-services.xml bezglich Injection in der * Superklasse aller Impl-Klassen ValidationModuleImpl validationModuleImpl = * (ValidationModuleImpl) context.getBean("validationmoduleimpl"); */ KOSTVal kostval = (KOSTVal) context.getBean("kostval"); File configFile = new File("configuration" + File.separator + "kostval.conf.xml"); // Ueberprfung des Parameters (Log-Verzeichnis) String pathToLogfile = kostval.getConfigurationService().getPathToLogfile(); File directoryOfLogfile = new File(pathToLogfile); if (!directoryOfLogfile.exists()) { directoryOfLogfile.mkdir(); } // Im Logverzeichnis besteht kein Schreibrecht if (!directoryOfLogfile.canWrite()) { System.out.println( kostval.getTextResourceService().getText(ERROR_LOGDIRECTORY_NOTWRITABLE, directoryOfLogfile)); System.exit(1); } if (!directoryOfLogfile.isDirectory()) { System.out.println(kostval.getTextResourceService().getText(ERROR_LOGDIRECTORY_NODIRECTORY)); System.exit(1); } // Ist die Anzahl Parameter (mind. 2) korrekt? if (args.length < 2) { System.out.println(kostval.getTextResourceService().getText(ERROR_PARAMETER_USAGE)); System.exit(1); } File valDatei = new File(args[1]); File logDatei = null; logDatei = valDatei; // Informationen zum Arbeitsverzeichnis holen String pathToWorkDir = kostval.getConfigurationService().getPathToWorkDir(); /* Nicht vergessen in "src/main/resources/config/applicationContext-services.xml" beim * entsprechenden Modul die property anzugeben: <property name="configurationService" * ref="configurationService" /> */ // Informationen holen, welche Formate validiert werden sollen String pdfaValidation = kostval.getConfigurationService().pdfaValidation(); String siardValidation = kostval.getConfigurationService().siardValidation(); String tiffValidation = kostval.getConfigurationService().tiffValidation(); String jp2Validation = kostval.getConfigurationService().jp2Validation(); // Konfiguration des Loggings, ein File Logger wird zustzlich erstellt LogConfigurator logConfigurator = (LogConfigurator) context.getBean("logconfigurator"); String logFileName = logConfigurator.configure(directoryOfLogfile.getAbsolutePath(), logDatei.getName()); File logFile = new File(logFileName); // Ab hier kann ins log geschrieben werden... String formatValOn = ""; // ermitteln welche Formate validiert werden knnen respektive eingeschaltet sind if (pdfaValidation.equals("yes")) { formatValOn = "PDF/A"; if (tiffValidation.equals("yes")) { formatValOn = formatValOn + ", TIFF"; } if (jp2Validation.equals("yes")) { formatValOn = formatValOn + ", JP2"; } if (siardValidation.equals("yes")) { formatValOn = formatValOn + ", SIARD"; } } else if (tiffValidation.equals("yes")) { formatValOn = "TIFF"; if (jp2Validation.equals("yes")) { formatValOn = formatValOn + ", JP2"; } if (siardValidation.equals("yes")) { formatValOn = formatValOn + ", SIARD"; } } else if (jp2Validation.equals("yes")) { formatValOn = "JP2"; if (siardValidation.equals("yes")) { formatValOn = formatValOn + ", SIARD"; } } else if (siardValidation.equals("yes")) { formatValOn = "SIARD"; } LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_HEADER)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_START, ausgabeStart)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_END)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_FORMATON, formatValOn)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_INFO)); System.out.println("KOST-Val"); System.out.println(""); if (args[0].equals("--format") && formatValOn.equals("")) { // Formatvalidierung aber alle Formate ausgeschlossen LOGGER.logError(kostval.getTextResourceService().getText(ERROR_IOE, kostval.getTextResourceService().getText(ERROR_NOFILEENDINGS))); System.out.println(kostval.getTextResourceService().getText(ERROR_NOFILEENDINGS)); System.exit(1); } File xslOrig = new File("resources" + File.separator + "kost-val.xsl"); File xslCopy = new File(directoryOfLogfile.getAbsolutePath() + File.separator + "kost-val.xsl"); if (!xslCopy.exists()) { Util.copyFile(xslOrig, xslCopy); } File tmpDir = new File(pathToWorkDir); /* bestehendes Workverzeichnis Abbruch wenn nicht leer, da am Schluss das Workverzeichnis * gelscht wird und entsprechend bestehende Dateien gelscht werden knnen */ if (tmpDir.exists()) { if (tmpDir.isDirectory()) { // Get list of file in the directory. When its length is not zero the folder is not empty. String[] files = tmpDir.list(); if (files.length > 0) { LOGGER.logError(kostval.getTextResourceService().getText(ERROR_IOE, kostval.getTextResourceService().getText(ERROR_WORKDIRECTORY_EXISTS, pathToWorkDir))); System.out.println( kostval.getTextResourceService().getText(ERROR_WORKDIRECTORY_EXISTS, pathToWorkDir)); System.exit(1); } } } // Im Pfad keine Sonderzeichen xml-Validierung SIP 1d und SIARD C strzen ab String patternStr = "[^!#\\$%\\(\\)\\+,\\-_\\.=@\\[\\]\\{\\}\\~:\\\\a-zA-Z0-9 ]"; Pattern pattern = Pattern.compile(patternStr); String name = tmpDir.getAbsolutePath(); String[] pathElements = name.split("/"); for (int i = 0; i < pathElements.length; i++) { String element = pathElements[i]; Matcher matcher = pattern.matcher(element); boolean matchFound = matcher.find(); if (matchFound) { LOGGER.logError(kostval.getTextResourceService().getText(ERROR_IOE, kostval.getTextResourceService().getText(ERROR_SPECIAL_CHARACTER, name))); System.out.println(kostval.getTextResourceService().getText(ERROR_SPECIAL_CHARACTER, name)); System.exit(1); } } // die Anwendung muss mindestens unter Java 6 laufen String javaRuntimeVersion = System.getProperty("java.vm.version"); if (javaRuntimeVersion.compareTo("1.6.0") < 0) { LOGGER.logError(kostval.getTextResourceService().getText(ERROR_IOE, kostval.getTextResourceService().getText(ERROR_WRONG_JRE))); System.out.println(kostval.getTextResourceService().getText(ERROR_WRONG_JRE)); System.exit(1); } // bestehendes Workverzeichnis wieder anlegen if (!tmpDir.exists()) { tmpDir.mkdir(); } // Im workverzeichnis besteht kein Schreibrecht if (!tmpDir.canWrite()) { LOGGER.logError(kostval.getTextResourceService().getText(ERROR_IOE, kostval.getTextResourceService().getText(ERROR_WORKDIRECTORY_NOTWRITABLE, tmpDir))); System.out.println(kostval.getTextResourceService().getText(ERROR_WORKDIRECTORY_NOTWRITABLE, tmpDir)); System.exit(1); } /* Vorberitung fr eine allfllige Festhaltung bei unterschiedlichen PDFA-Validierungsresultaten * in einer PDF_Diagnosedatei sowie Zhler der SIP-Dateiformate */ String diaPath = kostval.getConfigurationService().getPathToDiagnose(); // Im diaverzeichnis besteht kein Schreibrecht File diaDir = new File(diaPath); if (!diaDir.exists()) { diaDir.mkdir(); } if (!diaDir.canWrite()) { LOGGER.logError(kostval.getTextResourceService().getText(ERROR_IOE, kostval.getTextResourceService().getText(ERROR_DIADIRECTORY_NOTWRITABLE, diaDir))); System.out.println(kostval.getTextResourceService().getText(ERROR_DIADIRECTORY_NOTWRITABLE, diaDir)); System.exit(1); } File xmlDiaOrig = new File("resources" + File.separator + "KaD-Diagnosedaten.kost-val.xml"); File xmlDiaCopy = new File(diaPath + File.separator + "KaD-Diagnosedaten.kost-val.xml"); if (!xmlDiaCopy.exists()) { Util.copyFile(xmlDiaOrig, xmlDiaCopy); } File xslDiaOrig = new File("resources" + File.separator + "kost-val_KaDdia.xsl"); File xslDiaCopy = new File(diaPath + File.separator + "kost-val_KaDdia.xsl"); if (!xslDiaCopy.exists()) { Util.copyFile(xslDiaOrig, xslDiaCopy); } /* Ueberprfung des optionalen Parameters (2 -v --> im Verbose-mode werden die originalen Logs * nicht gelscht (PDFTron, Jhove & Co.) */ boolean verbose = false; if (args.length > 2) { if (!(args[2].equals("-v"))) { LOGGER.logError(kostval.getTextResourceService().getText(ERROR_IOE, kostval.getTextResourceService().getText(ERROR_PARAMETER_OPTIONAL_1))); System.out.println(kostval.getTextResourceService().getText(ERROR_PARAMETER_OPTIONAL_1)); System.exit(1); } else { verbose = true; } } /* Initialisierung TIFF-Modul B (JHove-Validierung) berprfen der Konfiguration: existiert die * jhove.conf am angebenen Ort? */ String jhoveConf = kostval.getConfigurationService().getPathToJhoveConfiguration(); File fJhoveConf = new File(jhoveConf); if (!fJhoveConf.exists() || !fJhoveConf.getName().equals("jhove.conf")) { LOGGER.logError(kostval.getTextResourceService().getText(ERROR_IOE, kostval.getTextResourceService().getText(ERROR_JHOVECONF_MISSING))); System.out.println(kostval.getTextResourceService().getText(ERROR_JHOVECONF_MISSING)); System.exit(1); } // Im Pfad keine Sonderzeichen xml-Validierung SIP 1d und SIARD C strzen ab name = valDatei.getAbsolutePath(); pathElements = name.split("/"); for (int i = 0; i < pathElements.length; i++) { String element = pathElements[i]; Matcher matcher = pattern.matcher(element); boolean matchFound = matcher.find(); if (matchFound) { LOGGER.logError(kostval.getTextResourceService().getText(ERROR_IOE, kostval.getTextResourceService().getText(ERROR_SPECIAL_CHARACTER, name))); System.out.println(kostval.getTextResourceService().getText(ERROR_SPECIAL_CHARACTER, name)); System.exit(1); } } // Ueberprfung des Parameters (Val-Datei): existiert die Datei? if (!valDatei.exists()) { LOGGER.logError(kostval.getTextResourceService().getText(ERROR_IOE, kostval.getTextResourceService().getText(ERROR_VALFILE_FILENOTEXISTING))); System.out.println(kostval.getTextResourceService().getText(ERROR_VALFILE_FILENOTEXISTING)); System.exit(1); } if (args[0].equals("--format")) { LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_FORMAT1)); Integer countNio = 0; Integer countSummaryNio = 0; Integer count = 0; Integer pdfaCountIo = 0; Integer pdfaCountNio = 0; Integer siardCountIo = 0; Integer siardCountNio = 0; Integer tiffCountIo = 0; Integer tiffCountNio = 0; Integer jp2CountIo = 0; Integer jp2CountNio = 0; // TODO: Formatvalidierung an einer Datei --> erledigt --> nur Marker if (!valDatei.isDirectory()) { boolean valFile = valFile(valDatei, logFileName, directoryOfLogfile, verbose); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_FORMAT2)); // Lschen des Arbeitsverzeichnisses, falls eines angelegt wurde if (tmpDir.exists()) { Util.deleteDir(tmpDir); } LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_LOGEND)); // Zeitstempel End java.util.Date nowEnd = new java.util.Date(); java.text.SimpleDateFormat sdfEnd = new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); String ausgabeEnd = sdfEnd.format(nowEnd); ausgabeEnd = "<End>" + ausgabeEnd + "</End>"; Util.valEnd(ausgabeEnd, logFile); Util.amp(logFile); // Die Konfiguration hereinkopieren try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setExpandEntityReferences(false); Document docConfig = factory.newDocumentBuilder().parse(configFile); NodeList list = docConfig.getElementsByTagName("configuration"); Element element = (Element) list.item(0); Document docLog = factory.newDocumentBuilder().parse(logFile); Node dup = docLog.importNode(element, true); docLog.getDocumentElement().appendChild(dup); FileWriter writer = new FileWriter(logFile); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ElementToStream(docLog.getDocumentElement(), baos); String stringDoc2 = new String(baos.toByteArray()); writer.write(stringDoc2); writer.close(); // Der Header wird dabei leider verschossen, wieder zurck ndern String newstring = kostval.getTextResourceService().getText(MESSAGE_XML_HEADER); String oldstring = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><KOSTValLog>"; Util.oldnewstring(oldstring, newstring, logFile); } catch (Exception e) { LOGGER.logError("<Error>" + kostval.getTextResourceService().getText(ERROR_XML_UNKNOWN, e.getMessage())); System.out.println("Exception: " + e.getMessage()); } if (valFile) { // Lschen des Arbeitsverzeichnisses, falls eines angelegt wurde if (tmpDir.exists()) { Util.deleteDir(tmpDir); } // Validierte Datei valide System.exit(0); } else { // Lschen des Arbeitsverzeichnisses, falls eines angelegt wurde if (tmpDir.exists()) { Util.deleteDir(tmpDir); } // Fehler in Validierte Datei --> invalide System.exit(2); } } else { // TODO: Formatvalidierung ber ein Ordner --> erledigt --> nur Marker Map<String, File> fileMap = Util.getFileMap(valDatei, false); Set<String> fileMapKeys = fileMap.keySet(); for (Iterator<String> iterator = fileMapKeys.iterator(); iterator.hasNext();) { String entryName = iterator.next(); File newFile = fileMap.get(entryName); if (!newFile.isDirectory()) { valDatei = newFile; count = count + 1; // Ausgabe Dateizhler Ersichtlich das KOST-Val Dateien durchsucht System.out.print(count + " "); System.out.print("\r"); if (((valDatei.getAbsolutePath().toLowerCase().endsWith(".jp2"))) && jp2Validation.equals("yes")) { boolean valFile = valFile(valDatei, logFileName, directoryOfLogfile, verbose); // Lschen des Arbeitsverzeichnisses, falls eines angelegt wurde if (tmpDir.exists()) { Util.deleteDir(tmpDir); } if (valFile) { jp2CountIo = jp2CountIo + 1; // Lschen des Arbeitsverzeichnisses, falls eines angelegt wurde if (tmpDir.exists()) { Util.deleteDir(tmpDir); } } else { jp2CountNio = jp2CountNio + 1; // Lschen des Arbeitsverzeichnisses, falls eines angelegt wurde if (tmpDir.exists()) { Util.deleteDir(tmpDir); } } } else if (((valDatei.getAbsolutePath().toLowerCase().endsWith(".tiff") || valDatei.getAbsolutePath().toLowerCase().endsWith(".tif"))) && tiffValidation.equals("yes")) { boolean valFile = valFile(valDatei, logFileName, directoryOfLogfile, verbose); // Lschen des Arbeitsverzeichnisses, falls eines angelegt wurde if (tmpDir.exists()) { Util.deleteDir(tmpDir); } if (valFile) { tiffCountIo = tiffCountIo + 1; // Lschen des Arbeitsverzeichnisses, falls eines angelegt wurde if (tmpDir.exists()) { Util.deleteDir(tmpDir); } } else { tiffCountNio = tiffCountNio + 1; // Lschen des Arbeitsverzeichnisses, falls eines angelegt wurde if (tmpDir.exists()) { Util.deleteDir(tmpDir); } } } else if ((valDatei.getAbsolutePath().toLowerCase().endsWith(".siard")) && siardValidation.equals("yes")) { boolean valFile = valFile(valDatei, logFileName, directoryOfLogfile, verbose); // Lschen des Arbeitsverzeichnisses, falls eines angelegt wurde if (tmpDir.exists()) { Util.deleteDir(tmpDir); } if (valFile) { siardCountIo = siardCountIo + 1; // Lschen des Arbeitsverzeichnisses, falls eines angelegt wurde if (tmpDir.exists()) { Util.deleteDir(tmpDir); } } else { siardCountNio = siardCountNio + 1; // Lschen des Arbeitsverzeichnisses, falls eines angelegt wurde if (tmpDir.exists()) { Util.deleteDir(tmpDir); } } } else if (((valDatei.getAbsolutePath().toLowerCase().endsWith(".pdf") || valDatei.getAbsolutePath().toLowerCase().endsWith(".pdfa"))) && pdfaValidation.equals("yes")) { boolean valFile = valFile(valDatei, logFileName, directoryOfLogfile, verbose); // Lschen des Arbeitsverzeichnisses, falls eines angelegt wurde if (tmpDir.exists()) { Util.deleteDir(tmpDir); } if (valFile) { pdfaCountIo = pdfaCountIo + 1; // Lschen des Arbeitsverzeichnisses, falls eines angelegt wurde if (tmpDir.exists()) { Util.deleteDir(tmpDir); } } else { pdfaCountNio = pdfaCountNio + 1; // Lschen des Arbeitsverzeichnisses, falls eines angelegt wurde if (tmpDir.exists()) { Util.deleteDir(tmpDir); } } } else { countNio = countNio + 1; } } } System.out.print(" "); System.out.print("\r"); if (countNio.equals(count)) { // keine Dateien Validiert LOGGER.logError( kostval.getTextResourceService().getText(ERROR_INCORRECTFILEENDINGS, formatValOn)); System.out.println( kostval.getTextResourceService().getText(ERROR_INCORRECTFILEENDINGS, formatValOn)); } LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_FORMAT2)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_LOGEND)); // Zeitstempel End java.util.Date nowEnd = new java.util.Date(); java.text.SimpleDateFormat sdfEnd = new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); String ausgabeEnd = sdfEnd.format(nowEnd); ausgabeEnd = "<End>" + ausgabeEnd + "</End>"; Util.valEnd(ausgabeEnd, logFile); Util.amp(logFile); // Die Konfiguration hereinkopieren try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setExpandEntityReferences(false); Document docConfig = factory.newDocumentBuilder().parse(configFile); NodeList list = docConfig.getElementsByTagName("configuration"); Element element = (Element) list.item(0); Document docLog = factory.newDocumentBuilder().parse(logFile); Node dup = docLog.importNode(element, true); docLog.getDocumentElement().appendChild(dup); FileWriter writer = new FileWriter(logFile); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ElementToStream(docLog.getDocumentElement(), baos); String stringDoc2 = new String(baos.toByteArray()); writer.write(stringDoc2); writer.close(); // Der Header wird dabei leider verschossen, wieder zurck ndern String newstring = kostval.getTextResourceService().getText(MESSAGE_XML_HEADER); String oldstring = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><KOSTValLog>"; Util.oldnewstring(oldstring, newstring, logFile); } catch (Exception e) { LOGGER.logError("<Error>" + kostval.getTextResourceService().getText(ERROR_XML_UNKNOWN, e.getMessage())); System.out.println("Exception: " + e.getMessage()); } countSummaryNio = pdfaCountNio + siardCountNio + tiffCountNio + jp2CountNio; if (countNio.equals(count)) { // keine Dateien Validiert bestehendes Workverzeichnis ggf. lschen if (tmpDir.exists()) { Util.deleteDir(tmpDir); } System.exit(1); } else if (countSummaryNio == 0) { // bestehendes Workverzeichnis ggf. lschen if (tmpDir.exists()) { Util.deleteDir(tmpDir); } // alle Validierten Dateien valide System.exit(0); } else { // bestehendes Workverzeichnis ggf. lschen if (tmpDir.exists()) { Util.deleteDir(tmpDir); } // Fehler in Validierten Dateien --> invalide System.exit(2); } if (tmpDir.exists()) { Util.deleteDir(tmpDir); tmpDir.deleteOnExit(); } } LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_FORMAT2)); } else if (args[0].equals("--sip")) { LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_FORMAT1)); // TODO: Sipvalidierung --> erledigt --> nur Marker boolean validFormat = false; File originalSipFile = valDatei; File unSipFile = valDatei; File outputFile3c = null; String fileName3c = null; File tmpDirZip = null; // zuerst eine Formatvalidierung ber den Content dies ist analog aufgebaut wie --format Integer countNio = 0; Integer countSummaryNio = 0; Integer countSummaryIo = 0; Integer count = 0; Integer pdfaCountIo = 0; Integer pdfaCountNio = 0; Integer siardCountIo = 0; Integer siardCountNio = 0; Integer tiffCountIo = 0; Integer tiffCountNio = 0; Integer jp2CountIo = 0; Integer jp2CountNio = 0; if (!valDatei.isDirectory()) { Boolean zip = false; // Eine ZIP Datei muss mit PK.. beginnen if ((valDatei.getAbsolutePath().toLowerCase().endsWith(".zip") || valDatei.getAbsolutePath().toLowerCase().endsWith(".zip64"))) { FileReader fr = null; try { fr = new FileReader(valDatei); BufferedReader read = new BufferedReader(fr); // Hex 03 in Char umwandeln String str3 = "03"; int i3 = Integer.parseInt(str3, 16); char c3 = (char) i3; // Hex 04 in Char umwandeln String str4 = "04"; int i4 = Integer.parseInt(str4, 16); char c4 = (char) i4; // auslesen der ersten 4 Zeichen der Datei int length; int i; char[] buffer = new char[4]; length = read.read(buffer); for (i = 0; i != length; i++) ; // die beiden charArrays (soll und ist) mit einander vergleichen char[] charArray1 = buffer; char[] charArray2 = new char[] { 'P', 'K', c3, c4 }; if (Arrays.equals(charArray1, charArray2)) { // hchstwahrscheinlich ein ZIP da es mit 504B0304 respektive PK.. beginnt zip = true; } } catch (Exception e) { LOGGER.logError("<Error>" + kostval.getTextResourceService().getText(ERROR_XML_UNKNOWN, e.getMessage())); System.out.println("Exception: " + e.getMessage()); } } // wenn die Datei kein Directory ist, muss sie mit zip oder zip64 enden if ((!(valDatei.getAbsolutePath().toLowerCase().endsWith(".zip") || valDatei.getAbsolutePath().toLowerCase().endsWith(".zip64"))) || zip == false) { // Abbruch! D.h. Sip message beginnen, Meldung und Beenden ab hier bis System.exit( 1 ); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_FORMAT2)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_SIP1)); valDatei = originalSipFile; LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_VALERGEBNIS)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_VALTYPE, kostval.getTextResourceService().getText(MESSAGE_SIPVALIDATION))); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_VALFILE, valDatei.getAbsolutePath())); System.out.println(kostval.getTextResourceService().getText(MESSAGE_SIPVALIDATION)); System.out.println(valDatei.getAbsolutePath()); // die eigentliche Fehlermeldung LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_MODUL_Aa_SIP) + kostval.getTextResourceService().getText(ERROR_XML_AA_INCORRECTFILEENDING)); System.out.println(kostval.getTextResourceService().getText(ERROR_XML_AA_INCORRECTFILEENDING)); // Fehler im Validierten SIP --> invalide & Abbruch LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_VALERGEBNIS_INVALID)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_VALERGEBNIS_CLOSE)); System.out.println("Invalid"); System.out.println(""); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_SIP2)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_LOGEND)); // Zeitstempel End java.util.Date nowEnd = new java.util.Date(); java.text.SimpleDateFormat sdfEnd = new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); String ausgabeEnd = sdfEnd.format(nowEnd); ausgabeEnd = "<End>" + ausgabeEnd + "</End>"; Util.valEnd(ausgabeEnd, logFile); Util.amp(logFile); // Die Konfiguration hereinkopieren try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setExpandEntityReferences(false); Document docConfig = factory.newDocumentBuilder().parse(configFile); NodeList list = docConfig.getElementsByTagName("configuration"); Element element = (Element) list.item(0); Document docLog = factory.newDocumentBuilder().parse(logFile); Node dup = docLog.importNode(element, true); docLog.getDocumentElement().appendChild(dup); FileWriter writer = new FileWriter(logFile); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ElementToStream(docLog.getDocumentElement(), baos); String stringDoc2 = new String(baos.toByteArray()); writer.write(stringDoc2); writer.close(); // Der Header wird dabei leider verschossen, wieder zurck ndern String newstring = kostval.getTextResourceService().getText(MESSAGE_XML_HEADER); String oldstring = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><KOSTValLog>"; Util.oldnewstring(oldstring, newstring, logFile); } catch (Exception e) { LOGGER.logError("<Error>" + kostval.getTextResourceService().getText(ERROR_XML_UNKNOWN, e.getMessage())); System.out.println("Exception: " + e.getMessage()); } // bestehendes Workverzeichnis ggf. lschen if (tmpDir.exists()) { Util.deleteDir(tmpDir); } System.exit(1); } else { // geziptes SIP --> in temp dir entzipen String toplevelDir = valDatei.getName(); int lastDotIdx = toplevelDir.lastIndexOf("."); toplevelDir = toplevelDir.substring(0, lastDotIdx); tmpDirZip = new File( tmpDir.getAbsolutePath() + File.separator + "ZIP" + File.separator + toplevelDir); try { Zip64Archiver.unzip(valDatei.getAbsolutePath(), tmpDirZip.getAbsolutePath()); } catch (Exception e) { try { Zip64Archiver.unzip64(valDatei, tmpDirZip); } catch (Exception e1) { // Abbruch! D.h. Sip message beginnen, Meldung und Beenden ab hier bis System.exit LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_FORMAT2)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_SIP1)); valDatei = originalSipFile; LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_VALERGEBNIS)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_VALTYPE, kostval.getTextResourceService().getText(MESSAGE_SIPVALIDATION))); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_VALFILE, valDatei.getAbsolutePath())); System.out.println(kostval.getTextResourceService().getText(MESSAGE_SIPVALIDATION)); System.out.println(valDatei.getAbsolutePath()); // die eigentliche Fehlermeldung LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_MODUL_Aa_SIP) + kostval.getTextResourceService().getText(ERROR_XML_AA_CANNOTEXTRACTZIP)); System.out.println( kostval.getTextResourceService().getText(ERROR_XML_AA_CANNOTEXTRACTZIP)); // Fehler im Validierten SIP --> invalide & Abbruch LOGGER.logError( kostval.getTextResourceService().getText(MESSAGE_XML_VALERGEBNIS_INVALID)); LOGGER.logError( kostval.getTextResourceService().getText(MESSAGE_XML_VALERGEBNIS_CLOSE)); System.out.println("Invalid"); System.out.println(""); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_SIP2)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_LOGEND)); // Zeitstempel End java.util.Date nowEnd = new java.util.Date(); java.text.SimpleDateFormat sdfEnd = new java.text.SimpleDateFormat( "dd.MM.yyyy HH:mm:ss"); String ausgabeEnd = sdfEnd.format(nowEnd); ausgabeEnd = "<End>" + ausgabeEnd + "</End>"; Util.valEnd(ausgabeEnd, logFile); Util.amp(logFile); // Die Konfiguration hereinkopieren try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setExpandEntityReferences(false); Document docConfig = factory.newDocumentBuilder().parse(configFile); NodeList list = docConfig.getElementsByTagName("configuration"); Element element = (Element) list.item(0); Document docLog = factory.newDocumentBuilder().parse(logFile); Node dup = docLog.importNode(element, true); docLog.getDocumentElement().appendChild(dup); FileWriter writer = new FileWriter(logFile); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ElementToStream(docLog.getDocumentElement(), baos); String stringDoc2 = new String(baos.toByteArray()); writer.write(stringDoc2); writer.close(); // Der Header wird dabei leider verschossen, wieder zurck ndern String newstring = kostval.getTextResourceService().getText(MESSAGE_XML_HEADER); String oldstring = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><KOSTValLog>"; Util.oldnewstring(oldstring, newstring, logFile); } catch (Exception e2) { LOGGER.logError("<Error>" + kostval.getTextResourceService() .getText(ERROR_XML_UNKNOWN, e2.getMessage())); System.out.println("Exception: " + e2.getMessage()); } // bestehendes Workverzeichnis ggf. lschen if (tmpDir.exists()) { Util.deleteDir(tmpDir); } System.exit(1); } } valDatei = tmpDirZip; File toplevelfolder = new File( valDatei.getAbsolutePath() + File.separator + valDatei.getName()); if (toplevelfolder.exists()) { valDatei = toplevelfolder; } unSipFile = valDatei; } } else { // SIP ist ein Ordner valDatei bleibt unverndert } // Vorgngige Formatvalidierung (Schritt 3c) Map<String, File> fileMap = Util.getFileMap(valDatei, false); Set<String> fileMapKeys = fileMap.keySet(); int pdf = 0; int tiff = 0; int siard = 0; int txt = 0; int csv = 0; int xml = 0; int xsd = 0; int wave = 0; int mp3 = 0; int jp2 = 0; int jpx = 0; int jpeg = 0; int png = 0; int dng = 0; int svg = 0; int mpeg2 = 0; int mp4 = 0; int xls = 0; int odt = 0; int ods = 0; int odp = 0; int other = 0; for (Iterator<String> iterator = fileMapKeys.iterator(); iterator.hasNext();) { String entryName = iterator.next(); File newFile = fileMap.get(entryName); if (!newFile.isDirectory() && newFile.getAbsolutePath().contains(File.separator + "content" + File.separator)) { valDatei = newFile; count = count + 1; // Ausgabe Dateizhler Ersichtlich das KOST-Val Dateien durchsucht System.out.print(count + " "); System.out.print("\r"); String extension = valDatei.getName(); int lastIndexOf = extension.lastIndexOf("."); if (lastIndexOf == -1) { // empty extension extension = "other"; } else { extension = extension.substring(lastIndexOf); } if (extension.equalsIgnoreCase(".pdf") || extension.equalsIgnoreCase(".pdfa")) { pdf = pdf + 1; } else if (extension.equalsIgnoreCase(".tiff") || extension.equalsIgnoreCase(".tif")) { tiff = tiff + 1; } else if (extension.equalsIgnoreCase(".siard")) { siard = siard + 1; } else if (extension.equalsIgnoreCase(".txt")) { txt = txt + 1; } else if (extension.equalsIgnoreCase(".csv")) { csv = csv + 1; } else if (extension.equalsIgnoreCase(".xml")) { xml = xml + 1; } else if (extension.equalsIgnoreCase(".xsd")) { xsd = xsd + 1; } else if (extension.equalsIgnoreCase(".wav")) { wave = wave + 1; } else if (extension.equalsIgnoreCase(".mp3")) { mp3 = mp3 + 1; } else if (extension.equalsIgnoreCase(".jp2")) { jp2 = jp2 + 1; } else if (extension.equalsIgnoreCase(".jpx") || extension.equalsIgnoreCase(".jpf")) { jpx = jpx + 1; } else if (extension.equalsIgnoreCase(".jpe") || extension.equalsIgnoreCase(".jpeg") || extension.equalsIgnoreCase(".jpg")) { jpeg = jpeg + 1; } else if (extension.equalsIgnoreCase(".png")) { png = png + 1; } else if (extension.equalsIgnoreCase(".dng")) { dng = dng + 1; } else if (extension.equalsIgnoreCase(".svg")) { svg = svg + 1; } else if (extension.equalsIgnoreCase(".mpeg") || extension.equalsIgnoreCase(".mpg")) { mpeg2 = mpeg2 + 1; } else if (extension.equalsIgnoreCase(".f4a") || extension.equalsIgnoreCase(".f4v") || extension.equalsIgnoreCase(".m4a") || extension.equalsIgnoreCase(".m4v") || extension.equalsIgnoreCase(".mp4")) { mp4 = mp4 + 1; } else if (extension.equalsIgnoreCase(".xls") || extension.equalsIgnoreCase(".xlw") || extension.equalsIgnoreCase(".xlsx")) { xls = xls + 1; } else if (extension.equalsIgnoreCase(".odt") || extension.equalsIgnoreCase(".ott")) { odt = odt + 1; } else if (extension.equalsIgnoreCase(".ods") || extension.equalsIgnoreCase(".ots")) { ods = ods + 1; } else if (extension.equalsIgnoreCase(".odp") || extension.equalsIgnoreCase(".otp")) { odp = odp + 1; } else { other = other + 1; } if (((valDatei.getAbsolutePath().toLowerCase().endsWith(".jp2"))) && jp2Validation.equals("yes")) { boolean valFile = valFile(valDatei, logFileName, directoryOfLogfile, verbose); if (valFile) { jp2CountIo = jp2CountIo + 1; } else { jp2CountNio = jp2CountNio + 1; } } else if (((valDatei.getAbsolutePath().toLowerCase().endsWith(".tiff") || valDatei.getAbsolutePath().toLowerCase().endsWith(".tif"))) && tiffValidation.equals("yes")) { boolean valFile = valFile(valDatei, logFileName, directoryOfLogfile, verbose); if (valFile) { tiffCountIo = tiffCountIo + 1; } else { tiffCountNio = tiffCountNio + 1; } } else if ((valDatei.getAbsolutePath().toLowerCase().endsWith(".siard")) && siardValidation.equals("yes")) { // Arbeitsverzeichnis zum Entpacken des Archivs erstellen String pathToWorkDirSiard = kostval.getConfigurationService().getPathToWorkDir(); File tmpDirSiard = new File(pathToWorkDirSiard + File.separator + "SIARD"); if (tmpDirSiard.exists()) { Util.deleteDir(tmpDirSiard); } if (!tmpDirSiard.exists()) { tmpDirSiard.mkdir(); } boolean valFile = valFile(valDatei, logFileName, directoryOfLogfile, verbose); if (valFile) { siardCountIo = siardCountIo + 1; } else { siardCountNio = siardCountNio + 1; } } else if (((valDatei.getAbsolutePath().toLowerCase().endsWith(".pdf") || valDatei.getAbsolutePath().toLowerCase().endsWith(".pdfa"))) && pdfaValidation.equals("yes")) { boolean valFile = valFile(valDatei, logFileName, directoryOfLogfile, verbose); if (valFile) { pdfaCountIo = pdfaCountIo + 1; } else { pdfaCountNio = pdfaCountNio + 1; } } else { countNio = countNio + 1; } } } countSummaryNio = pdfaCountNio + siardCountNio + tiffCountNio + jp2CountNio; countSummaryIo = pdfaCountIo + siardCountIo + tiffCountIo + jp2CountIo; int countSummaryIoP = 100 / count * countSummaryIo; int countSummaryNioP = 100 / count * countSummaryNio; int countNioP = 100 / count * countNio; String summary3c = kostval.getTextResourceService().getText(MESSAGE_XML_SUMMARY_3C, count, countSummaryIo, countSummaryNio, countNio, countSummaryIoP, countSummaryNioP, countNioP); if (countSummaryNio == 0) { // alle Validierten Dateien valide validFormat = true; fileName3c = "3c_Valide.txt"; } else { // Fehler in Validierten Dateien --> invalide validFormat = false; fileName3c = "3c_Invalide.txt"; } // outputFile3c = new File( directoryOfLogfile + fileName3c ); outputFile3c = new File(pathToWorkDir + File.separator + fileName3c); try { outputFile3c.createNewFile(); } catch (IOException e) { e.printStackTrace(); } if (countNio == count) { // keine Dateien Validiert LOGGER.logError(kostval.getTextResourceService().getText(ERROR_INCORRECTFILEENDINGS, formatValOn)); System.out .println(kostval.getTextResourceService().getText(ERROR_INCORRECTFILEENDINGS, formatValOn)); } LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_FORMAT2)); // Start Normale SIP-Validierung mit auswertung Format-Val. im 3c LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_SIP1)); valDatei = unSipFile; LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_VALERGEBNIS)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_VALTYPE, kostval.getTextResourceService().getText(MESSAGE_SIPVALIDATION))); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_VALFILE, originalSipFile.getAbsolutePath())); System.out.println(kostval.getTextResourceService().getText(MESSAGE_SIPVALIDATION)); System.out.println(originalSipFile.getAbsolutePath()); Controllersip controller = (Controllersip) context.getBean("controllersip"); boolean okMandatory = false; okMandatory = controller.executeMandatory(valDatei, directoryOfLogfile); boolean ok = false; /* die Validierungen 1a - 1d sind obligatorisch, wenn sie bestanden wurden, knnen die * restlichen Validierungen, welche nicht zum Abbruch der Applikation fhren, ausgefhrt * werden. * * 1a wurde bereits getestet (vor der Formatvalidierung entsprechend fngt der Controller mit * 1b an */ if (okMandatory) { ok = controller.executeOptional(valDatei, directoryOfLogfile); } // Formatvalidierung validFormat ok = (ok && okMandatory && validFormat); if (ok) { // Validiertes SIP valide LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_VALERGEBNIS_VALID)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_VALERGEBNIS_CLOSE)); System.out.println("Valid"); System.out.println(""); } else { // Fehler im Validierten SIP --> invalide LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_VALERGEBNIS_INVALID)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_VALERGEBNIS_CLOSE)); System.out.println("Invalid"); System.out.println(""); } // ggf. Fehlermeldung 3c ergnzen Util.val3c(summary3c, logFile ); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_SIP2)); LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_LOGEND)); // Zeitstempel End java.util.Date nowEnd = new java.util.Date(); java.text.SimpleDateFormat sdfEnd = new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); String ausgabeEnd = sdfEnd.format(nowEnd); ausgabeEnd = "<End>" + ausgabeEnd + "</End>"; Util.valEnd(ausgabeEnd, logFile); Util.val3c(summary3c, logFile); Util.amp(logFile); // Die Konfiguration hereinkopieren try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setExpandEntityReferences(false); Document docConfig = factory.newDocumentBuilder().parse(configFile); NodeList list = docConfig.getElementsByTagName("configuration"); Element element = (Element) list.item(0); Document docLog = factory.newDocumentBuilder().parse(logFile); Node dup = docLog.importNode(element, true); docLog.getDocumentElement().appendChild(dup); FileWriter writer = new FileWriter(logFile); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ElementToStream(docLog.getDocumentElement(), baos); String stringDoc2 = new String(baos.toByteArray()); writer.write(stringDoc2); writer.close(); // Der Header wird dabei leider verschossen, wieder zurck ndern String newstring = kostval.getTextResourceService().getText(MESSAGE_XML_HEADER); String oldstring = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><KOSTValLog>"; Util.oldnewstring(oldstring, newstring, logFile); } catch (Exception e) { LOGGER.logError( "<Error>" + kostval.getTextResourceService().getText(ERROR_XML_UNKNOWN, e.getMessage())); System.out.println("Exception: " + e.getMessage()); } // bestehendes Workverzeichnis ggf. lschen if (tmpDir.exists()) { Util.deleteDir(tmpDir); } StringBuffer command = new StringBuffer("rd " + tmpDir.getAbsolutePath() + " /s /q"); try { // KaD-Diagnose-Datei mit den neusten Anzahl Dateien pro KaD-Format Updaten DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlDiaCopy); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("KOSTVal_FFCounter"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; if (pdf > 0) { String pdfNodeString = eElement.getElementsByTagName("pdf").item(0).getTextContent(); int pdfNodeValue = Integer.parseInt(pdfNodeString); pdf = pdf + pdfNodeValue; Util.kadDia("<pdf>" + pdfNodeValue + "</pdf>", "<pdf>" + pdf + "</pdf>", xmlDiaCopy); } if (tiff > 0) { String tiffNodeString = eElement.getElementsByTagName("tiff").item(0).getTextContent(); int tiffNodeValue = Integer.parseInt(tiffNodeString); tiff = tiff + tiffNodeValue; Util.kadDia("<tiff>" + tiffNodeValue + "</tiff>", "<tiff>" + tiff + "</tiff>", xmlDiaCopy); } if (siard > 0) { String siardNodeString = eElement.getElementsByTagName("siard").item(0) .getTextContent(); int siardNodeValue = Integer.parseInt(siardNodeString); siard = siard + siardNodeValue; Util.kadDia("<siard>" + siardNodeValue + "</siard>", "<siard>" + siard + "</siard>", xmlDiaCopy); } if (txt > 0) { String txtNodeString = eElement.getElementsByTagName("txt").item(0).getTextContent(); int txtNodeValue = Integer.parseInt(txtNodeString); txt = txt + txtNodeValue; Util.kadDia("<txt>" + txtNodeValue + "</txt>", "<txt>" + txt + "</txt>", xmlDiaCopy); } if (csv > 0) { String csvNodeString = eElement.getElementsByTagName("csv").item(0).getTextContent(); int csvNodeValue = Integer.parseInt(csvNodeString); csv = csv + csvNodeValue; Util.kadDia("<csv>" + csvNodeValue + "</csv>", "<csv>" + csv + "</csv>", xmlDiaCopy); } if (xml > 0) { String xmlNodeString = eElement.getElementsByTagName("xml").item(0).getTextContent(); int xmlNodeValue = Integer.parseInt(xmlNodeString); xml = xml + xmlNodeValue; Util.kadDia("<xml>" + xmlNodeValue + "</xml>", "<xml>" + xml + "</xml>", xmlDiaCopy); } if (xsd > 0) { String xsdNodeString = eElement.getElementsByTagName("xsd").item(0).getTextContent(); int xsdNodeValue = Integer.parseInt(xsdNodeString); xsd = xsd + xsdNodeValue; Util.kadDia("<xsd>" + xsdNodeValue + "</xsd>", "<xsd>" + xsd + "</xsd>", xmlDiaCopy); } if (wave > 0) { String waveNodeString = eElement.getElementsByTagName("wave").item(0).getTextContent(); int waveNodeValue = Integer.parseInt(waveNodeString); wave = wave + waveNodeValue; Util.kadDia("<wave>" + waveNodeValue + "</wave>", "<wave>" + wave + "</wave>", xmlDiaCopy); } if (mp3 > 0) { String mp3NodeString = eElement.getElementsByTagName("mp3").item(0).getTextContent(); int mp3NodeValue = Integer.parseInt(mp3NodeString); mp3 = mp3 + mp3NodeValue; Util.kadDia("<mp3>" + mp3NodeValue + "</mp3>", "<mp3>" + mp3 + "</mp3>", xmlDiaCopy); } if (jp2 > 0) { String jp2NodeString = eElement.getElementsByTagName("jp2").item(0).getTextContent(); int jp2NodeValue = Integer.parseInt(jp2NodeString); jp2 = jp2 + jp2NodeValue; Util.kadDia("<jp2>" + jp2NodeValue + "</jp2>", "<jp2>" + jp2 + "</jp2>", xmlDiaCopy); } if (jpx > 0) { String jpxNodeString = eElement.getElementsByTagName("jpx").item(0).getTextContent(); int jpxNodeValue = Integer.parseInt(jpxNodeString); jpx = jpx + jpxNodeValue; Util.kadDia("<jpx>" + jpxNodeValue + "</jpx>", "<jpx>" + jpx + "</jpx>", xmlDiaCopy); } if (jpeg > 0) { String jpegNodeString = eElement.getElementsByTagName("jpeg").item(0).getTextContent(); int jpegNodeValue = Integer.parseInt(jpegNodeString); jpeg = jpeg + jpegNodeValue; Util.kadDia("<jpeg>" + jpegNodeValue + "</jpeg>", "<jpeg>" + jpeg + "</jpeg>", xmlDiaCopy); } if (png > 0) { String pngNodeString = eElement.getElementsByTagName("png").item(0).getTextContent(); int pngNodeValue = Integer.parseInt(pngNodeString); png = png + pngNodeValue; Util.kadDia("<png>" + pngNodeValue + "</png>", "<png>" + png + "</png>", xmlDiaCopy); } if (dng > 0) { String dngNodeString = eElement.getElementsByTagName("dng").item(0).getTextContent(); int dngNodeValue = Integer.parseInt(dngNodeString); dng = dng + dngNodeValue; Util.kadDia("<dng>" + dngNodeValue + "</dng>", "<dng>" + dng + "</dng>", xmlDiaCopy); } if (svg > 0) { String svgNodeString = eElement.getElementsByTagName("svg").item(0).getTextContent(); int svgNodeValue = Integer.parseInt(svgNodeString); svg = svg + svgNodeValue; Util.kadDia("<svg>" + svgNodeValue + "</svg>", "<svg>" + svg + "</svg>", xmlDiaCopy); } if (mpeg2 > 0) { String mpeg2NodeString = eElement.getElementsByTagName("mpeg2").item(0) .getTextContent(); int mpeg2NodeValue = Integer.parseInt(mpeg2NodeString); mpeg2 = mpeg2 + mpeg2NodeValue; Util.kadDia("<mpeg2>" + mpeg2NodeValue + "</mpeg2>", "<mpeg2>" + mpeg2 + "</mpeg2>", xmlDiaCopy); } if (mp4 > 0) { String mp4NodeString = eElement.getElementsByTagName("mp4").item(0).getTextContent(); int mp4NodeValue = Integer.parseInt(mp4NodeString); mp4 = mp4 + mp4NodeValue; Util.kadDia("<mp4>" + mp4NodeValue + "</mp4>", "<mp4>" + mp4 + "</mp4>", xmlDiaCopy); } if (xls > 0) { String xlsNodeString = eElement.getElementsByTagName("xls").item(0).getTextContent(); int xlsNodeValue = Integer.parseInt(xlsNodeString); xls = xls + xlsNodeValue; Util.kadDia("<xls>" + xlsNodeValue + "</xls>", "<xls>" + xls + "</xls>", xmlDiaCopy); } if (odt > 0) { String odtNodeString = eElement.getElementsByTagName("odt").item(0).getTextContent(); int odtNodeValue = Integer.parseInt(odtNodeString); odt = odt + odtNodeValue; Util.kadDia("<odt>" + odtNodeValue + "</odt>", "<odt>" + odt + "</odt>", xmlDiaCopy); } if (ods > 0) { String odsNodeString = eElement.getElementsByTagName("ods").item(0).getTextContent(); int odsNodeValue = Integer.parseInt(odsNodeString); ods = ods + odsNodeValue; Util.kadDia("<ods>" + odsNodeValue + "</ods>", "<ods>" + ods + "</ods>", xmlDiaCopy); } if (odp > 0) { String odpNodeString = eElement.getElementsByTagName("odp").item(0).getTextContent(); int odpNodeValue = Integer.parseInt(odpNodeString); odp = odp + odpNodeValue; Util.kadDia("<odp>" + odpNodeValue + "</odp>", "<odp>" + odp + "</odp>", xmlDiaCopy); } if (other > 0) { String otherNodeString = eElement.getElementsByTagName("other").item(0) .getTextContent(); int otherNodeValue = Integer.parseInt(otherNodeString); other = other + otherNodeValue; Util.kadDia("<other>" + otherNodeValue + "</other>", "<other>" + other + "</other>", xmlDiaCopy); } } } } catch (Exception e) { e.printStackTrace(); } if (ok) { // bestehendes Workverzeichnis ggf. lschen if (tmpDir.exists()) { Util.deleteDir(tmpDir); } if (tmpDir.exists()) { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(command.toString()); } System.exit(0); } else { // bestehendes Workverzeichnis ggf. lschen if (tmpDir.exists()) { Util.deleteDir(tmpDir); } if (tmpDir.exists()) { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(command.toString()); } System.exit(2); } LOGGER.logError(kostval.getTextResourceService().getText(MESSAGE_XML_SIP2)); } else { /* Ueberprfung des Parameters (Val-Typ): format / sip args[0] ist nicht "--format" oder * "--sip" --> INVALIDE */ LOGGER.logError(kostval.getTextResourceService().getText(ERROR_IOE, kostval.getTextResourceService().getText(ERROR_PARAMETER_USAGE))); System.out.println(kostval.getTextResourceService().getText(ERROR_PARAMETER_USAGE)); if (tmpDir.exists()) { Util.deleteDir(tmpDir); tmpDir.deleteOnExit(); } System.exit(1); } }
From source file:de.prozesskraft.pkraft.Createdoc.java
public static void main(String[] args) throws org.apache.commons.cli.ParseException, IOException { Createdoc tmp = new Createdoc(); /*---------------------------- get options from ini-file//from w ww .j a v a 2 s. c o m ----------------------------*/ File installDir = new java.io.File(WhereAmI.getInstallDirectoryAbsolutePath(Createdoc.class) + "/.."); File inifile = new java.io.File(installDir.getAbsolutePath() + "/etc/pkraft-createdoc.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 odefinition = OptionBuilder.withArgName("definition").hasArg() .withDescription("[mandatory] process definition file") // .isRequired() .create("definition"); Option oformat = OptionBuilder.withArgName("format").hasArg() .withDescription("[mandatory, default=pdf] output format (pdf|pptx) ").create("format"); Option ooutput = OptionBuilder.withArgName("output").hasArg().withDescription( "[mandatory, default=out.<format>] output file with full documentation of process definition") // .isRequired() .create("output"); //// Option property = OptionBuilder.withArgName( "property=value" ) //// .hasArgs(2) //// .withValueSeparator() //// .withDescription( "use value for given property" ) //// .create("D"); // // /*---------------------------- // create options object // ----------------------------*/ Options options = new Options(); options.addOption(ohelp); options.addOption(ov); options.addOption(odefinition); options.addOption(oformat); options.addOption(ooutput); /*---------------------------- create the parser ----------------------------*/ CommandLineParser parser = new GnuParser(); // parse the command line arguments line = parser.parse(options, args); /*---------------------------- usage/help ----------------------------*/ if (line.hasOption("help")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("createdoc", options); System.exit(0); } if (line.hasOption("v")) { System.out.println("web: www.prozesskraft.de"); System.out.println("version: [% version %]"); System.out.println("date: [% date %]"); System.exit(0); } /*---------------------------- die variablen festlegen ----------------------------*/ int error = 0; String definition = null; String format = null; String output = null; // festlegen von definition if (line.hasOption("definition")) { definition = line.getOptionValue("definition"); if (!(new java.io.File(definition).exists())) { System.err.println("file does not exist " + definition); } } else { System.err.println("parameter -definition is mandatory"); error++; } // festlegen von format if (line.hasOption("format")) { if (line.getOptionValue("format").matches("pdf|pptx")) { format = line.getOptionValue("format"); } else { System.err.println("for -format use only pdf|pptx"); error++; } } else { format = "pdf"; } // festlegen von output if (line.hasOption("output")) { output = line.getOptionValue("output"); } else { output = "out." + format; } // feststellen ob output bereits existiert if (new java.io.File(output).exists()) { System.err.println("output already exists: " + output); error++; } // aussteigen, falls fehler aufgetaucht sind if (error > 0) { System.err.println("error(s) occured. try -help for help."); System.exit(1); } /*---------------------------- 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 process = new Process(); Reporter report; process.setInfilexml(definition); System.out.println("info: reading process definition " + definition); try { process.readXml(); process.setStepRanks(); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } //festlegen des temporaeren verzeichnisses fuer die Daten und Pfade erzeugen long jetztMillis = System.currentTimeMillis(); String randomPathJasperFilled = "/tmp/" + jetztMillis + "_jasperFilled"; String randomPathPng = "/tmp/" + jetztMillis + "_png"; String randomPathPdf = "/tmp/" + jetztMillis + "_pdf"; String randomPathPptx = "/tmp/" + jetztMillis + "_pptx"; new File(randomPathJasperFilled).mkdirs(); new File(randomPathPng).mkdirs(); new File(randomPathPdf).mkdirs(); new File(randomPathPptx).mkdirs(); ////////////////////////////////////////// // erstellen der Bilder // konfigurieren der processing ansicht // PmodelViewPage page = new PmodelViewPage(process); PmodelViewPage page = new PmodelViewPage(); page.einstellungen.getProcess().setStepRanks(); page.einstellungen.setSize(100); page.einstellungen.setZoom(100); // page.einstellungen.setZoom(8 * 100/process.getMaxLevel()); page.einstellungen.setTextsize(0); page.einstellungen.setRanksize(7); page.einstellungen.setWidth(2500); page.einstellungen.setHeight(750); page.einstellungen.setGravx(10); page.einstellungen.setGravy(0); page.einstellungen.setRootpositionratiox((float) 0.05); page.einstellungen.setRootpositionratioy((float) 0.5); page.einstellungen.setProcess(process); createContents(page); // mit open kann die page angezeigt werden if (!(produktiv)) { open(); } // // warten // System.out.println("stabilisierung ansicht: 5 sekunden warten gravitation = "+page.einstellungen.getGravx()); // long jetzt5 = System.currentTimeMillis(); // while (System.currentTimeMillis() < jetzt5 + 5000) // { // // } // // page.einstellungen.setGravx(10); // // warten int wartezeitSeconds = 1; if (produktiv) { wartezeitSeconds = page.einstellungen.getProcess().getStep().size() * 2; } System.out.println("stabilisierung ansicht: " + wartezeitSeconds + " sekunden warten gravitation = " + page.einstellungen.getGravx()); long jetzt6 = System.currentTimeMillis(); while (System.currentTimeMillis() < jetzt6 + (wartezeitSeconds * 1000)) { } page.einstellungen.setFix(true); // VORBEREITUNG) bild speichern processTopologyImagePath = randomPathPng + "/processTopology.png"; page.savePic(processTopologyImagePath); // zuerst 1 sekunde warten, dann autocrop long jetzt = System.currentTimeMillis(); while (System.currentTimeMillis() < jetzt + 1000) { } new AutoCropBorder(processTopologyImagePath); // VORBEREITUNG) fuer jeden step ein bild speichern for (Step actualStep : process.getStep()) { // root ueberspringen // if (actualStep.isRoot()); String stepImagePath = randomPathPng + "/step_" + actualStep.getName() + "_Topology.png"; // Farbe des Steps auf finished (gruen) aendern page.einstellungen.getProcess().getRootStep().setStatusOverwrite("waiting"); actualStep.setStatusOverwrite("finished"); // etwas warten, bis die farbe bezeichnet wurde long jetzt4 = System.currentTimeMillis(); while (System.currentTimeMillis() < jetzt4 + 500) { } page.savePic(stepImagePath); // zuerst 1 sekunde warten, dann autocrop long jetzt3 = System.currentTimeMillis(); while (System.currentTimeMillis() < jetzt3 + 1000) { } new AutoCropBorder(stepImagePath); stepTopologyImagePath.put(actualStep.getName(), stepImagePath); // farbe wieder auf grau aendern actualStep.setStatusOverwrite(null); System.out.println("erstelle bild fuer step: " + actualStep.getName()); long jetzt2 = System.currentTimeMillis(); while (System.currentTimeMillis() < jetzt2 + 1000) { } } page.destroy(); ////////////////////////////////////////// report = new Reporter(); // P03) erstellen des p03 System.out.println("info: generating p03."); String pdfPathP03 = null; String pptxPathP03 = null; String jasperPathP03 = null; String jasperFilledPathP03 = null; // P03) feststellen, welches jasperreports-template fuer den angeforderten typ verwendet werden soll if (ini.get("pkraft-createdoc", "p03") != null) { pdfPathP03 = randomPathPdf + "/p03.pdf"; pptxPathP03 = randomPathPptx + "/p03.pptx"; jasperPathP03 = installDir.getAbsolutePath() + "/" + ini.get("pkraft-createdoc", "p03"); jasperFilledPathP03 = (randomPathJasperFilled + "/p03.jasperFilled"); pdfRankFiles.put("0.0.03", pdfPathP03); pptxRankFiles.put("0.0.03", pptxPathP03); } else { System.err.println("no entry 'p03' found in ini file"); System.exit(1); } DateFormat dateFormat = new SimpleDateFormat("dd. MM. yyyy"); Date date = new Date(); report.setParameter("processName", process.getName()); report.setParameter("processVersion", process.getVersion()); report.setParameter("processDatum", dateFormat.format(date)); report.setParameter("processArchitectLogoImagePath", installDir.getAbsolutePath() + "/" + ini.get("pkraft-createdoc", "logo")); report.setParameter("processArchitectCompany", process.getArchitectCompany()); report.setParameter("processArchitectName", process.getArchitectName()); report.setParameter("processArchitectMail", process.getArchitectMail()); report.setParameter("processCustomerCompany", process.getCustomerCompany()); report.setParameter("processCustomerName", process.getCustomerName()); report.setParameter("processCustomerMail", process.getCustomerMail()); try { report.fillReportFileToFile(jasperPathP03, jasperFilledPathP03); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pdf try { report.convertFileToPdf(jasperFilledPathP03, pdfPathP03); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pptx try { report.convertFileToPptx(jasperFilledPathP03, pptxPathP03); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } report = null; //System.exit(0); ////////////////////////////////////////// report = new Reporter(); // P05) erstellen des p05 System.out.println("info: generating p05."); String pdfPathP05 = null; String pptxPathP05 = null; String jasperPathP05 = null; String jasperFilledPathP05 = null; // P05) feststellen, welches jasperreports-template fuer den angeforderten typ verwendet werden soll if (ini.get("pkraft-createdoc", "p05") != null) { pdfPathP05 = randomPathPdf + "/p05.pdf"; pptxPathP05 = randomPathPptx + "/p05.pptx"; jasperPathP05 = installDir.getAbsolutePath() + "/" + ini.get("pkraft-createdoc", "p05"); jasperFilledPathP05 = (randomPathJasperFilled + "/p05.jasperFilled"); pdfRankFiles.put("0.0.05", pdfPathP05); pptxRankFiles.put("0.0.05", pptxPathP05); } else { System.err.println("no entry 'p05' found in ini file"); System.exit(1); } report.setParameter("processName", process.getName()); report.setParameter("processVersion", process.getVersion()); report.setParameter("processArchitectCompany", process.getArchitectCompany()); report.setParameter("processArchitectName", process.getArchitectName()); report.setParameter("processArchitectMail", process.getArchitectMail()); report.setParameter("processCustomerCompany", process.getCustomerCompany()); report.setParameter("processCustomerName", process.getCustomerName()); report.setParameter("processCustomerMail", process.getCustomerMail()); try { report.fillReportFileToFile(jasperPathP05, jasperFilledPathP05); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pdf try { report.convertFileToPdf(jasperFilledPathP05, pdfPathP05); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pptx try { report.convertFileToPptx(jasperFilledPathP05, pptxPathP05); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } report = null; //System.exit(0); ////////////////////////////////////////// report = new Reporter(); // P08) erstellen des p08 System.out.println("info: generating p08."); String pdfPathP08 = null; String pptxPathP08 = null; String jasperPathP08 = null; String jasperFilledPathP08 = null; // P08) feststellen, welches jasperreports-template fuer den angeforderten typ verwendet werden soll if (ini.get("pkraft-createdoc", "p08") != null) { pdfPathP08 = randomPathPdf + "/p08.pdf"; pptxPathP08 = randomPathPptx + "/p08.pptx"; jasperPathP08 = installDir.getAbsolutePath() + "/" + ini.get("pkraft-createdoc", "p08"); jasperFilledPathP08 = (randomPathJasperFilled + "/p08.jasperFilled"); pdfRankFiles.put("0.0.08", pdfPathP08); pptxRankFiles.put("0.0.08", pptxPathP08); } else { System.err.println("no entry 'p08' found in ini file"); System.exit(1); } report.setParameter("processName", process.getName()); report.setParameter("processVersion", process.getVersion()); report.setParameter("processArchitectCompany", process.getArchitectCompany()); report.setParameter("processArchitectName", process.getArchitectName()); report.setParameter("processArchitectMail", process.getArchitectMail()); report.setParameter("processCustomerCompany", process.getCustomerCompany()); report.setParameter("processCustomerName", process.getCustomerName()); report.setParameter("processCustomerMail", process.getCustomerMail()); report.setParameter("processDescription", process.getDescription()); try { report.fillReportFileToFile(jasperPathP08, jasperFilledPathP08); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pdf try { report.convertFileToPdf(jasperFilledPathP08, pdfPathP08); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pptx try { report.convertFileToPptx(jasperFilledPathP08, pptxPathP08); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } report = null; //System.exit(0); ////////////////////////////////////////// report = new Reporter(); // P10) erstellen des p10 System.out.println("info: generating p10."); String pdfPathP10 = null; String pptxPathP10 = null; String jasperPathP10 = null; String jasperFilledPathP10 = null; // P10) feststellen, welches jasperreports-template fuer den angeforderten typ verwendet werden soll if (ini.get("pkraft-createdoc", "p10") != null) { pdfPathP10 = randomPathPdf + "/p10.pdf"; pptxPathP10 = randomPathPptx + "/p10.pptx"; jasperPathP10 = installDir.getAbsolutePath() + "/" + ini.get("pkraft-createdoc", "p10"); jasperFilledPathP10 = (randomPathJasperFilled + "/p10.jasperFilled"); pdfRankFiles.put("0.0.10", pdfPathP10); pptxRankFiles.put("0.0.10", pptxPathP10); } else { System.err.println("no entry 'p10' found in ini file"); System.exit(1); } report.setParameter("processName", process.getName()); report.setParameter("processVersion", process.getVersion()); report.setParameter("processArchitectCompany", process.getArchitectCompany()); report.setParameter("processArchitectName", process.getArchitectName()); report.setParameter("processArchitectMail", process.getArchitectMail()); report.setParameter("processCustomerCompany", process.getCustomerCompany()); report.setParameter("processCustomerName", process.getCustomerName()); report.setParameter("processCustomerMail", process.getCustomerMail()); // rootstep holen Step rootStep = process.getStep(process.getRootstepname()); // ueber alle commit iterieren for (Commit actualCommit : rootStep.getCommit()) { // ueber alle files iterieren for (de.prozesskraft.pkraft.File actualFile : actualCommit.getFile()) { HashMap<String, Object> row = new HashMap<String, Object>(); // Spalte 'origin' row.put("origin", "user/cb2"); // Spalte 'objectType' row.put("objectType", "file"); // Spalte 'minOccur' row.put("minOccur", "" + actualFile.getMinoccur()); // Spalte 'maxOccur' row.put("maxOccur", "" + actualFile.getMaxoccur()); // Spalte 'objectKey' row.put("objectKey", actualFile.getKey()); // die steps herausfinden, die dieses file benoetigen ArrayList<Step> allStepsThatNeedThisFileFromRoot = process.getStepWhichNeedFromRoot("file", actualFile.getKey()); String stepnameListe = ""; for (Step actStep : allStepsThatNeedThisFileFromRoot) { stepnameListe += "\n=> " + actStep.getName(); } // Spalte 'objectDescription' row.put("objectDescription", actualFile.getDescription() + stepnameListe); // Datensatz dem report hinzufuegen report.addField(row); } // ueber alle variablen iterieren for (de.prozesskraft.pkraft.Variable actualVariable : actualCommit.getVariable()) { HashMap<String, Object> row = new HashMap<String, Object>(); // Spalte 'origin' row.put("origin", "user/cb2"); // Spalte 'objectType' row.put("objectType", "variable"); // Spalte 'minOccur' row.put("minOccur", "" + actualVariable.getMinoccur()); // Spalte 'maxOccur' row.put("maxOccur", "" + actualVariable.getMaxoccur()); // Spalte 'objectKey' row.put("objectKey", actualVariable.getKey()); // die steps herausfinden, die dieses file benoetigen ArrayList<Step> allStepsThatNeedThisObjectFromRoot = process.getStepWhichNeedFromRoot("variable", actualVariable.getKey()); String stepnameListe = ""; for (Step actStep : allStepsThatNeedThisObjectFromRoot) { stepnameListe += "\n=> " + actStep.getName(); } // Spalte 'objectDescription' row.put("objectDescription", actualVariable.getDescription() + stepnameListe); // Datensatz dem report hinzufuegen report.addField(row); } } try { report.fillReportFileToFile(jasperPathP10, jasperFilledPathP10); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pdf try { report.convertFileToPdf(jasperFilledPathP10, pdfPathP10); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pptx try { report.convertFileToPptx(jasperFilledPathP10, pptxPathP10); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } report = null; ////////////////////////////////////////// report = new Reporter(); // P20) erstellen des p20 System.out.println("info: generating p20."); String pdfPathP20 = null; String pptxPathP20 = null; String jasperPathP20 = null; String jasperFilledPathP20 = null; // P20) feststellen, welches jasperreports-template fuer den angeforderten typ verwendet werden soll if (ini.get("pkraft-createdoc", "p20") != null) { pdfPathP20 = randomPathPdf + "/p20.pdf"; pptxPathP20 = randomPathPptx + "/p20.pptx"; jasperPathP20 = installDir.getAbsolutePath() + "/" + ini.get("pkraft-createdoc", "p20"); jasperFilledPathP20 = (randomPathJasperFilled + "/p20.jasperFilled"); pdfRankFiles.put("0.0.20", pdfPathP20); pptxRankFiles.put("0.0.20", pptxPathP20); } else { System.err.println("no entry 'p20' found in ini file"); System.exit(1); } report.setParameter("processName", process.getName()); report.setParameter("processVersion", process.getVersion()); report.setParameter("processArchitectCompany", process.getArchitectCompany()); report.setParameter("processArchitectName", process.getArchitectName()); report.setParameter("processArchitectMail", process.getArchitectMail()); report.setParameter("processCustomerCompany", process.getCustomerCompany()); report.setParameter("processCustomerName", process.getCustomerName()); report.setParameter("processCustomerMail", process.getCustomerMail()); // ueber alle steps iterieren (ausser root) for (Step actualStep : (ArrayList<Step>) process.getStep()) { // ueberspringen wenn es sich um root handelt if (!(actualStep.getName().equals(process.getRootstepname()))) { // ueber alle commit iterieren for (Commit actualCommit : actualStep.getCommit()) { // nur die, die toroot=true ( und spaeter auch tosdm=true) if (actualCommit.isTorootPresent()) { // ueber alle files iterieren for (de.prozesskraft.pkraft.File actualFile : actualCommit.getFile()) { HashMap<String, Object> row = new HashMap<String, Object>(); // Spalte 'destination' row.put("destination", "user/cb2"); // Spalte 'objectType' row.put("objectType", "file"); // Spalte 'minOccur' row.put("minOccur", "" + actualFile.getMinoccur()); // Spalte 'maxOccur' row.put("maxOccur", "" + actualFile.getMaxoccur()); // Spalte 'objectKey' row.put("objectKey", actualFile.getKey()); // Spalte 'objectDescription' row.put("objectDescription", actualFile.getDescription() + "\n<= " + actualStep.getName()); // Datensatz dem report hinzufuegen report.addField(row); } // ueber alle variablen iterieren for (de.prozesskraft.pkraft.Variable actualVariable : actualCommit.getVariable()) { HashMap<String, Object> row = new HashMap<String, Object>(); // Spalte 'objectType' row.put("destination", "user/cb2"); // Spalte 'objectType' row.put("objectType", "variable"); // Spalte 'minOccur' row.put("minOccur", "" + actualVariable.getMinoccur()); // Spalte 'maxOccur' row.put("maxOccur", "" + actualVariable.getMaxoccur()); // Spalte 'objectKey' row.put("objectKey", actualVariable.getKey()); // Spalte 'objectDescription' row.put("objectDescription", actualVariable.getDescription() + "\n<= " + actualStep.getName()); // Datensatz dem report hinzufuegen report.addField(row); } } } } } try { report.fillReportFileToFile(jasperPathP20, jasperFilledPathP20); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pdf try { report.convertFileToPdf(jasperFilledPathP20, pdfPathP20); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pptx try { report.convertFileToPptx(jasperFilledPathP20, pptxPathP20); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } report = null; ////////////////////////////////////////// report = new Reporter(); // P30) erstellen des p30 System.out.println("info: generating p30."); String pdfPathP30 = null; String pptxPathP30 = null; String jasperPathP30 = null; String jasperFilledPathP30 = null; // P30) feststellen, welches jasperreports-template fuer den angeforderten typ verwendet werden soll if (ini.get("pkraft-createdoc", "p30") != null) { pdfPathP30 = randomPathPdf + "/p30.pdf"; pptxPathP30 = randomPathPptx + "/p30.pptx"; jasperPathP30 = installDir.getAbsolutePath() + "/" + ini.get("pkraft-createdoc", "p30"); jasperFilledPathP30 = (randomPathJasperFilled + "/p30.jasperFilled"); pdfRankFiles.put("0.0.30", pdfPathP30); pptxRankFiles.put("0.0.30", pptxPathP30); } else { System.err.println("no entry 'p30' found in ini file"); System.exit(1); } report.setParameter("processName", process.getName()); report.setParameter("processVersion", process.getVersion()); report.setParameter("processArchitectCompany", process.getArchitectCompany()); report.setParameter("processArchitectName", process.getArchitectName()); report.setParameter("processArchitectMail", process.getArchitectMail()); report.setParameter("processCustomerCompany", process.getCustomerCompany()); report.setParameter("processCustomerName", process.getCustomerName()); report.setParameter("processCustomerMail", process.getCustomerMail()); // P1) bild an report melden report.setParameter("processTopologyImagePath", processTopologyImagePath); try { report.fillReportFileToFile(jasperPathP30, jasperFilledPathP30); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pdf try { report.convertFileToPdf(jasperFilledPathP30, pdfPathP30); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pptx try { report.convertFileToPptx(jasperFilledPathP30, pptxPathP30); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } report = null; // System.exit(0); ////////////////////////////////////////// report = new Reporter(); // P40) erstellen des p40 System.out.println("info: generating p40."); String pdfPathP40 = null; String pptxPathP40 = null; String jasperPathP40 = null; String jasperFilledPathP40 = null; // P40) feststellen, welches jasperreports-template fuer den angeforderten typ verwendet werden soll if (ini.get("pkraft-createdoc", "p40") != null) { pdfPathP40 = randomPathPdf + "/p40.pdf"; pptxPathP40 = randomPathPptx + "/p40.pptx"; jasperPathP40 = installDir.getAbsolutePath() + "/" + ini.get("pkraft-createdoc", "p40"); jasperFilledPathP40 = (randomPathJasperFilled + "/p40.jasperFilled"); pdfRankFiles.put("0.0.40", pdfPathP40); pptxRankFiles.put("0.0.40", pptxPathP40); } else { System.err.println("no entry 'p40' found in ini file"); System.exit(1); } report.setParameter("processName", process.getName()); report.setParameter("processVersion", process.getVersion()); report.setParameter("processArchitectCompany", process.getArchitectCompany()); report.setParameter("processArchitectName", process.getArchitectName()); report.setParameter("processArchitectMail", process.getArchitectMail()); report.setParameter("processCustomerCompany", process.getCustomerCompany()); report.setParameter("processCustomerName", process.getCustomerName()); report.setParameter("processCustomerMail", process.getCustomerMail()); // P40) bild an report melden report.setParameter("processTopologyImagePath", processTopologyImagePath); // Tabelle erzeugen ArrayList<Step> steps = process.getStep(); for (int x = 0; x < steps.size(); x++) { HashMap<String, Object> row = new HashMap<String, Object>(); Step actualStep = steps.get(x); // erste Spalte ist 'rank' // um die korrekte sortierung zu erhalten soll der rank-string auf jeweils 2 Stellen erweitert werden String[] rankArray = actualStep.getRank().split("\\."); Integer[] rankArrayInt = new Integer[rankArray.length]; for (int y = 0; y < rankArray.length; y++) { rankArrayInt[y] = Integer.parseInt(rankArray[y]); } String rankFormated = String.format("%02d.%02d", rankArrayInt); row.put("stepRank", rankFormated); // zweite Spalte ist 'stepname' row.put("stepName", actualStep.getName()); // System.out.println("stepName: "+actualStep.getName()); // dritte Spalte ist 'Beschreibung' row.put("stepDescription", actualStep.getDescription()); // System.out.println("stepRank: "+actualStep.getDescription()); // wenn nicht der root-step, dann row eintragen if (!(actualStep.getName().equals(process.getRootstepname()))) { report.addField(row); } } try { report.fillReportFileToFile(jasperPathP40, jasperFilledPathP40); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pdf try { report.convertFileToPdf(jasperFilledPathP40, pdfPathP40); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pptx try { report.convertFileToPptx(jasperFilledPathP40, pptxPathP40); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } report = null; ////////////////////////////////////////// // fuer jeden Step einen eigenen Input Report erzeugen for (Step actualStep : process.getStep()) { // root-step ueberspringen if (actualStep.getName().equals(process.getRootstepname())) { System.out.println("skipping step root"); } // alle anderen auswerten else { report = new Reporter(); // P51x) erstellen des p51 System.out.println( "info: generating p51 for step " + actualStep.getRank() + " => " + actualStep.getName()); String stepRank = actualStep.getRank(); String pdfPathP51 = null; String pptxPathP51 = null; String jasperPathP51 = null; String jasperFilledPathP51 = null; // P51x) feststellen, welches jasperreports-template fuer den angeforderten typ verwendet werden soll if (ini.get("pkraft-createdoc", "p51") != null) { pdfPathP51 = randomPathPdf + "/p5." + stepRank + ".1.pdf"; pptxPathP51 = randomPathPptx + "/p5." + stepRank + ".1.pptx"; jasperPathP51 = installDir.getAbsolutePath() + "/" + ini.get("pkraft-createdoc", "p51"); jasperFilledPathP51 = randomPathJasperFilled + "/p5." + stepRank + ".1.jasperFilled"; String[] rankArray = stepRank.split("\\."); Integer[] rankArrayInt = new Integer[rankArray.length]; for (int x = 0; x < rankArray.length; x++) { rankArrayInt[x] = Integer.parseInt(rankArray[x]); } String rankFormated = String.format("%03d.%03d", rankArrayInt); pdfRankFiles.put(rankFormated + ".1", pdfPathP51); pptxRankFiles.put(rankFormated + ".1", pptxPathP51); } else { System.err.println("no entry 'p51' found in ini file"); System.exit(1); } report.setParameter("processName", process.getName()); report.setParameter("processVersion", process.getVersion()); report.setParameter("processArchitectCompany", process.getArchitectCompany()); report.setParameter("processArchitectName", process.getArchitectName()); report.setParameter("processArchitectMail", process.getArchitectMail()); report.setParameter("processCustomerCompany", process.getCustomerCompany()); report.setParameter("processCustomerName", process.getCustomerName()); report.setParameter("processCustomerMail", process.getCustomerMail()); report.setParameter("stepName", actualStep.getName()); report.setParameter("stepRank", stepRank); report.setParameter("stepDescription", actualStep.getDescription()); String aufruf = ""; if (actualStep.getWork() != null) { // zusammensetzen des scriptaufrufs String interpreter = ""; if (actualStep.getWork().getInterpreter() != null) { interpreter = actualStep.getWork().getInterpreter(); } aufruf = interpreter + " " + actualStep.getWork().getCommand(); for (Callitem actualCallitem : actualStep.getWork().getCallitem()) { aufruf += " " + actualCallitem.getPar(); if (!(actualCallitem.getDel() == null)) { aufruf += actualCallitem.getDel(); } if (!(actualCallitem.getVal() == null)) { aufruf += actualCallitem.getVal(); } } } else if (actualStep.getSubprocess() != null) { aufruf = ini.get("apps", "pkraft-startinstance"); aufruf += " --pdomain " + actualStep.getSubprocess().getDomain(); aufruf += " --pname " + actualStep.getSubprocess().getName(); aufruf += " --pversion " + actualStep.getSubprocess().getVersion(); for (Commit actCommit : actualStep.getSubprocess().getStep().getCommit()) { for (de.prozesskraft.pkraft.File actFile : actCommit.getFile()) { aufruf += " --commitfile " + actFile.getGlob(); } for (Variable actVariable : actCommit.getVariable()) { aufruf += " --commitvariable " + actVariable.getKey() + "=" + actVariable.getValue(); } } } report.setParameter("stepWorkCall", aufruf); // P51x) bild an report melden report.setParameter("stepTopologyImagePath", stepTopologyImagePath.get(actualStep.getName())); // ueber alle lists iterieren for (List actualList : actualStep.getList()) { HashMap<String, Object> row = new HashMap<String, Object>(); // Spalte 'Woher?' row.put("origin", "-"); // Spalte 'typ' row.put("objectType", "wert"); // Spalte 'minOccur' row.put("minOccur", "-"); // Spalte 'maxOccur' row.put("maxOccur", "-"); // Spalte 'Label' row.put("objectKey", actualList.getName()); // Spalte 'Label' String listString = actualList.getItem().toString(); row.put("objectDescription", listString.substring(1, listString.length() - 1)); report.addField(row); } // ueber alle inits iterieren for (Init actualInit : actualStep.getInit()) { HashMap<String, Object> row = new HashMap<String, Object>(); // Spalte 'Woher?' if (actualInit.getFromstep().equals(process.getRootstepname())) { row.put("origin", "user/cb2"); } else { row.put("origin", actualInit.getFromstep()); } // Spalte 'typ' row.put("objectType", actualInit.getFromobjecttype()); // Spalte 'minOccur' row.put("minOccur", "" + actualInit.getMinoccur()); // Spalte 'maxOccur' row.put("maxOccur", "" + actualInit.getMaxoccur()); // Spalte 'Label' row.put("objectKey", actualInit.getListname()); // Spalte 'Label' row.put("objectDescription", "-"); report.addField(row); } try { report.fillReportFileToFile(jasperPathP51, jasperFilledPathP51); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pdf try { report.convertFileToPdf(jasperFilledPathP51, pdfPathP51); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pptx try { report.convertFileToPptx(jasperFilledPathP51, pptxPathP51); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } report = null; } } ////////////////////////////////////////// // fuer jeden Step einen eigenen Output Report erzeugen for (Step actualStep : process.getStep()) { // root-step ueberspringen if (actualStep.getName().equals(process.getRootstepname())) { System.out.println("skipping step root"); } // alle anderen auswerten else { report = new Reporter(); // P52x) erstellen des p52 System.out.println( "info: generating p52 for step " + actualStep.getRank() + " => " + actualStep.getName()); String stepRank = actualStep.getRank(); String pdfPathP52 = null; String pptxPathP52 = null; String jasperPathP52 = null; String jasperFilledPathP52 = null; // P52x) feststellen, welches jasperreports-template fuer den angeforderten typ verwendet werden soll if (ini.get("pkraft-createdoc", "p52") != null) { pdfPathP52 = randomPathPdf + "/p5." + stepRank + ".2.pdf"; pptxPathP52 = randomPathPptx + "/p5." + stepRank + ".2.pptx"; jasperPathP52 = installDir.getAbsolutePath() + "/" + ini.get("pkraft-createdoc", "p52"); jasperFilledPathP52 = randomPathJasperFilled + "/p5." + stepRank + ".1.jasperFilled"; String[] rankArray = stepRank.split("\\."); Integer[] rankArrayInt = new Integer[rankArray.length]; for (int x = 0; x < rankArray.length; x++) { rankArrayInt[x] = Integer.parseInt(rankArray[x]); } String rankFormated = String.format("%03d.%03d", rankArrayInt); pdfRankFiles.put(rankFormated + ".2", pdfPathP52); pptxRankFiles.put(rankFormated + ".2", pptxPathP52); } else { System.err.println("no entry 'p52' found in ini file"); System.exit(1); } report.setParameter("processName", process.getName()); report.setParameter("processVersion", process.getVersion()); report.setParameter("processArchitectCompany", process.getArchitectCompany()); report.setParameter("processArchitectName", process.getArchitectName()); report.setParameter("processArchitectMail", process.getArchitectMail()); report.setParameter("processCustomerCompany", process.getCustomerCompany()); report.setParameter("processCustomerName", process.getCustomerName()); report.setParameter("processCustomerMail", process.getCustomerMail()); report.setParameter("stepName", actualStep.getName()); report.setParameter("stepRank", stepRank); // logfile ermitteln String logfile = "-"; if (actualStep.getWork() != null) { if (actualStep.getWork().getLogfile() == null || actualStep.getWork().getLogfile().equals("")) { report.setParameter("stepWorkLogfile", actualStep.getWork().getLogfile()); } } else if (actualStep.getSubprocess() != null) { logfile = ".log"; } report.setParameter("stepWorkLogfile", logfile); // zusammensetzen der return/exitcode informationen String exitInfo = "exit 0 = kein fehler aufgetreten"; exitInfo += "\nexit >0 = ein fehler ist aufgetreten."; if (actualStep.getWork() != null) { for (Exit actualExit : actualStep.getWork().getExit()) { exitInfo += "\nexit " + actualExit.getValue() + " = " + actualExit.getMsg(); } } report.setParameter("stepWorkExit", exitInfo); // P52x) bild an report melden report.setParameter("stepTopologyImagePath", stepTopologyImagePath.get(actualStep.getName())); // ueber alle inits iterieren for (Commit actualCommit : actualStep.getCommit()) { // ueber alle files iterieren for (de.prozesskraft.pkraft.File actualFile : actualCommit.getFile()) { HashMap<String, Object> row = new HashMap<String, Object>(); // Spalte 'destination' if (actualCommit.isTorootPresent()) { row.put("destination", "user/cb2"); } else { row.put("destination", "prozessintern"); } // Spalte 'objectType' row.put("objectType", "file"); // Spalte 'minOccur' row.put("minOccur", "" + actualFile.getMinoccur()); // Spalte 'maxOccur' row.put("maxOccur", "" + actualFile.getMaxoccur()); // Spalte 'objectKey' row.put("objectKey", actualFile.getKey()); // Spalte 'objectDescription' row.put("objectDescription", actualFile.getDescription()); // Datensatz dem report hinzufuegen report.addField(row); } // ueber alle variablen iterieren for (de.prozesskraft.pkraft.Variable actualVariable : actualCommit.getVariable()) { HashMap<String, Object> row = new HashMap<String, Object>(); // Spalte 'destination' if (actualCommit.isTorootPresent()) { row.put("destination", "user/cb2"); } else { row.put("destination", "prozessintern"); } // Spalte 'objectType' row.put("objectType", "variable"); // Spalte 'minOccur' row.put("minOccur", "" + actualVariable.getMinoccur()); // Spalte 'maxOccur' row.put("maxOccur", "" + actualVariable.getMaxoccur()); // Spalte 'objectKey' row.put("objectKey", actualVariable.getKey()); // Spalte 'objectDescription' row.put("objectDescription", actualVariable.getDescription()); // Datensatz dem report hinzufuegen report.addField(row); } } try { report.fillReportFileToFile(jasperPathP52, jasperFilledPathP52); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pdf try { report.convertFileToPdf(jasperFilledPathP52, pdfPathP52); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } // export to pptx try { report.convertFileToPptx(jasperFilledPathP52, pptxPathP52); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } report = null; } } // warten bis alles auf platte geschrieben ist try { Thread.sleep(1000); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } // merge and output if (format.equals("pdf")) { mergePdf(pdfRankFiles, output); } else if (format.equals("pptx")) { mergePptx(pptxRankFiles, output); } System.out.println("info: generating process documentation ready."); System.exit(0); }
From source file:edu.nyu.vida.data_polygamy.scalar_function_computation.Aggregation.java
/** * @param args//w ww. j ava2 s . c o m */ @SuppressWarnings({ "deprecation" }) public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException { Options options = new Options(); Option forceOption = new Option("f", "force", false, "force the computation of the aggregate functions " + "even if files already exist"); forceOption.setRequired(false); options.addOption(forceOption); Option gOption = new Option("g", "group", true, "set group of datasets for which the aggregate functions" + " will be computed, followed by their temporal and spatial attribute indices"); gOption.setRequired(true); gOption.setArgName("GROUP"); gOption.setArgs(Option.UNLIMITED_VALUES); options.addOption(gOption); Option machineOption = new Option("m", "machine", true, "machine identifier"); machineOption.setRequired(true); machineOption.setArgName("MACHINE"); machineOption.setArgs(1); options.addOption(machineOption); Option nodesOption = new Option("n", "nodes", true, "number of nodes"); nodesOption.setRequired(true); nodesOption.setArgName("NODES"); nodesOption.setArgs(1); options.addOption(nodesOption); Option s3Option = new Option("s3", "s3", false, "data on Amazon S3"); s3Option.setRequired(false); options.addOption(s3Option); Option awsAccessKeyIdOption = new Option("aws_id", "aws-id", true, "aws access key id; " + "this is required if the execution is on aws"); awsAccessKeyIdOption.setRequired(false); awsAccessKeyIdOption.setArgName("AWS-ACCESS-KEY-ID"); awsAccessKeyIdOption.setArgs(1); options.addOption(awsAccessKeyIdOption); Option awsSecretAccessKeyOption = new Option("aws_key", "aws-id", true, "aws secrect access key; " + "this is required if the execution is on aws"); awsSecretAccessKeyOption.setRequired(false); awsSecretAccessKeyOption.setArgName("AWS-SECRET-ACCESS-KEY"); awsSecretAccessKeyOption.setArgs(1); options.addOption(awsSecretAccessKeyOption); Option bucketOption = new Option("b", "s3-bucket", true, "bucket on s3; " + "this is required if the execution is on aws"); bucketOption.setRequired(false); bucketOption.setArgName("S3-BUCKET"); bucketOption.setArgs(1); options.addOption(bucketOption); Option helpOption = new Option("h", "help", false, "display this message"); helpOption.setRequired(false); options.addOption(helpOption); HelpFormatter formatter = new HelpFormatter(); CommandLineParser parser = new PosixParser(); CommandLine cmd = null; try { cmd = parser.parse(options, args); } catch (ParseException e) { formatter.printHelp("hadoop jar data-polygamy.jar " + "edu.nyu.vida.data_polygamy.scalar_function_computation.Aggregation", options, true); System.exit(0); } if (cmd.hasOption("h")) { formatter.printHelp("hadoop jar data-polygamy.jar " + "edu.nyu.vida.data_polygamy.scalar_function_computation.Aggregation", options, true); System.exit(0); } boolean s3 = cmd.hasOption("s3"); String s3bucket = ""; String awsAccessKeyId = ""; String awsSecretAccessKey = ""; if (s3) { if ((!cmd.hasOption("aws_id")) || (!cmd.hasOption("aws_key")) || (!cmd.hasOption("b"))) { System.out.println( "Arguments 'aws_id', 'aws_key', and 'b'" + " are mandatory if execution is on AWS."); formatter.printHelp( "hadoop jar data-polygamy.jar " + "edu.nyu.vida.data_polygamy.scalar_function_computation.Aggregation", options, true); System.exit(0); } s3bucket = cmd.getOptionValue("b"); awsAccessKeyId = cmd.getOptionValue("aws_id"); awsSecretAccessKey = cmd.getOptionValue("aws_key"); } boolean snappyCompression = false; boolean bzip2Compression = false; String machine = cmd.getOptionValue("m"); int nbNodes = Integer.parseInt(cmd.getOptionValue("n")); Configuration s3conf = new Configuration(); if (s3) { s3conf.set("fs.s3.awsAccessKeyId", awsAccessKeyId); s3conf.set("fs.s3.awsSecretAccessKey", awsSecretAccessKey); s3conf.set("bucket", s3bucket); } String datasetNames = ""; String datasetIds = ""; String preProcessingDatasets = ""; ArrayList<String> shortDataset = new ArrayList<String>(); ArrayList<String> shortDatasetAggregation = new ArrayList<String>(); HashMap<String, String> datasetTempAtt = new HashMap<String, String>(); HashMap<String, String> datasetSpatialAtt = new HashMap<String, String>(); HashMap<String, String> preProcessingDataset = new HashMap<String, String>(); HashMap<String, String> datasetId = new HashMap<String, String>(); boolean removeExistingFiles = cmd.hasOption("f"); String[] datasetArgs = cmd.getOptionValues("g"); for (int i = 0; i < datasetArgs.length; i += 3) { String dataset = datasetArgs[i]; // getting pre-processing String tempPreProcessing = FrameworkUtils.searchPreProcessing(dataset, s3conf, s3); if (tempPreProcessing == null) { System.out.println("No pre-processing available for " + dataset); continue; } preProcessingDataset.put(dataset, tempPreProcessing); shortDataset.add(dataset); datasetTempAtt.put(dataset, ((datasetArgs[i + 1] == "null") ? null : datasetArgs[i + 1])); datasetSpatialAtt.put(dataset, ((datasetArgs[i + 2] == "null") ? null : datasetArgs[i + 2])); datasetId.put(dataset, null); } if (shortDataset.size() == 0) { System.out.println("No datasets to process."); System.exit(0); } // getting dataset id Path path = null; FileSystem fs = null; if (s3) { path = new Path(s3bucket + FrameworkUtils.datasetsIndexDir); fs = FileSystem.get(path.toUri(), s3conf); } else { fs = FileSystem.get(new Configuration()); path = new Path(fs.getHomeDirectory() + "/" + FrameworkUtils.datasetsIndexDir); } BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(path))); String line = br.readLine(); while (line != null) { String[] dt = line.split("\t"); if (datasetId.containsKey(dt[0])) { datasetId.put(dt[0], dt[1]); datasetNames += dt[0] + ","; datasetIds += dt[1] + ","; } line = br.readLine(); } br.close(); if (s3) fs.close(); datasetNames = datasetNames.substring(0, datasetNames.length() - 1); datasetIds = datasetIds.substring(0, datasetIds.length() - 1); Iterator<String> it = shortDataset.iterator(); while (it.hasNext()) { String dataset = it.next(); if (datasetId.get(dataset) == null) { System.out.println("No dataset id for " + dataset); System.exit(0); } } FrameworkUtils.createDir(s3bucket + FrameworkUtils.aggregatesDir, s3conf, s3); // getting smallest resolution HashMap<String, String> tempResMap = new HashMap<String, String>(); HashMap<String, String> spatialResMap = new HashMap<String, String>(); HashMap<String, String> datasetTemporalStrMap = new HashMap<String, String>(); HashMap<String, String> datasetSpatialStrMap = new HashMap<String, String>(); HashSet<String> input = new HashSet<String>(); for (String dataset : shortDataset) { String[] datasetArray = preProcessingDataset.get(dataset).split("-"); String datasetTemporalStr = datasetArray[datasetArray.length - 2]; int datasetTemporal = utils.temporalResolution(datasetTemporalStr); String datasetSpatialStr = datasetArray[datasetArray.length - 1]; int datasetSpatial = utils.spatialResolution(datasetSpatialStr); // finding all possible resolutions String[] temporalResolutions = FrameworkUtils.getAggTempResolutions(datasetTemporal); String[] spatialResolutions = FrameworkUtils.getAggSpatialResolutions(datasetSpatial); String temporalResolution = ""; String spatialResolution = ""; String tempRes = ""; String spatialRes = ""; boolean dataAdded = false; for (int i = 0; i < temporalResolutions.length; i++) { for (int j = 0; j < spatialResolutions.length; j++) { temporalResolution = temporalResolutions[i]; spatialResolution = spatialResolutions[j]; String aggregatesOutputFileName = s3bucket + FrameworkUtils.aggregatesDir + "/" + dataset + "/"; if (removeExistingFiles) { FrameworkUtils.removeFile(aggregatesOutputFileName, s3conf, s3); } if (!FrameworkUtils.fileExists(aggregatesOutputFileName, s3conf, s3)) { dataAdded = true; tempRes += temporalResolution + "-"; spatialRes += spatialResolution + "-"; } } } if (dataAdded) { input.add(s3bucket + FrameworkUtils.preProcessingDir + "/" + preProcessingDataset.get(dataset)); shortDatasetAggregation.add(dataset); tempResMap.put(dataset, tempRes.substring(0, tempRes.length() - 1)); spatialResMap.put(dataset, spatialRes.substring(0, spatialRes.length() - 1)); datasetTemporalStrMap.put(dataset, datasetTemporalStr); datasetSpatialStrMap.put(dataset, datasetSpatialStr); } } if (input.isEmpty()) { System.out.println("All the input datasets have aggregates."); System.out.println("Use -f in the beginning of the command line to force the computation."); System.exit(0); } it = input.iterator(); while (it.hasNext()) { preProcessingDatasets += it.next() + ","; } Job aggJob = null; String aggregatesOutputDir = s3bucket + FrameworkUtils.aggregatesDir + "/tmp/"; String jobName = "aggregates"; FrameworkUtils.removeFile(aggregatesOutputDir, s3conf, s3); Configuration aggConf = new Configuration(); Machine machineConf = new Machine(machine, nbNodes); aggConf.set("dataset-name", datasetNames); aggConf.set("dataset-id", datasetIds); for (int i = 0; i < shortDatasetAggregation.size(); i++) { String dataset = shortDatasetAggregation.get(i); String id = datasetId.get(dataset); aggConf.set("dataset-" + id + "-temporal-resolutions", tempResMap.get(dataset)); aggConf.set("dataset-" + id + "-spatial-resolutions", spatialResMap.get(dataset)); aggConf.set("dataset-" + id + "-temporal-att", datasetTempAtt.get(dataset)); aggConf.set("dataset-" + id + "-spatial-att", datasetSpatialAtt.get(dataset)); aggConf.set("dataset-" + id + "-temporal", datasetTemporalStrMap.get(dataset)); aggConf.set("dataset-" + id + "-spatial", datasetSpatialStrMap.get(dataset)); if (s3) aggConf.set("dataset-" + id, s3bucket + FrameworkUtils.preProcessingDir + "/" + preProcessingDataset.get(dataset)); else aggConf.set("dataset-" + id, FileSystem.get(new Configuration()).getHomeDirectory() + "/" + FrameworkUtils.preProcessingDir + "/" + preProcessingDataset.get(dataset)); } aggConf.set("mapreduce.tasktracker.map.tasks.maximum", String.valueOf(machineConf.getMaximumTasks())); aggConf.set("mapreduce.tasktracker.reduce.tasks.maximum", String.valueOf(machineConf.getMaximumTasks())); aggConf.set("mapreduce.jobtracker.maxtasks.perjob", "-1"); aggConf.set("mapreduce.reduce.shuffle.parallelcopies", "20"); aggConf.set("mapreduce.input.fileinputformat.split.minsize", "0"); aggConf.set("mapreduce.task.io.sort.mb", "200"); aggConf.set("mapreduce.task.io.sort.factor", "100"); machineConf.setMachineConfiguration(aggConf); if (s3) { machineConf.setMachineConfiguration(aggConf); aggConf.set("fs.s3.awsAccessKeyId", awsAccessKeyId); aggConf.set("fs.s3.awsSecretAccessKey", awsSecretAccessKey); } if (snappyCompression) { aggConf.set("mapreduce.map.output.compress", "true"); aggConf.set("mapreduce.map.output.compress.codec", "org.apache.hadoop.io.compress.SnappyCodec"); //aggConf.set("mapreduce.output.fileoutputformat.compress.codec", "org.apache.hadoop.io.compress.SnappyCodec"); } if (bzip2Compression) { aggConf.set("mapreduce.map.output.compress", "true"); aggConf.set("mapreduce.map.output.compress.codec", "org.apache.hadoop.io.compress.BZip2Codec"); //aggConf.set("mapreduce.output.fileoutputformat.compress.codec", "org.apache.hadoop.io.compress.BZip2Codec"); } aggJob = new Job(aggConf); aggJob.setJobName(jobName); aggJob.setMapOutputKeyClass(SpatioTemporalWritable.class); aggJob.setMapOutputValueClass(AggregationArrayWritable.class); aggJob.setOutputKeyClass(SpatioTemporalWritable.class); aggJob.setOutputValueClass(FloatArrayWritable.class); //aggJob.setOutputKeyClass(Text.class); //aggJob.setOutputValueClass(Text.class); aggJob.setMapperClass(AggregationMapper.class); aggJob.setCombinerClass(AggregationCombiner.class); aggJob.setReducerClass(AggregationReducer.class); aggJob.setNumReduceTasks(machineConf.getNumberReduces()); aggJob.setInputFormatClass(SequenceFileInputFormat.class); //aggJob.setOutputFormatClass(SequenceFileOutputFormat.class); LazyOutputFormat.setOutputFormatClass(aggJob, SequenceFileOutputFormat.class); //LazyOutputFormat.setOutputFormatClass(aggJob, TextOutputFormat.class); SequenceFileOutputFormat.setCompressOutput(aggJob, true); SequenceFileOutputFormat.setOutputCompressionType(aggJob, CompressionType.BLOCK); FileInputFormat.setInputDirRecursive(aggJob, true); FileInputFormat.setInputPaths(aggJob, preProcessingDatasets.substring(0, preProcessingDatasets.length() - 1)); FileOutputFormat.setOutputPath(aggJob, new Path(aggregatesOutputDir)); aggJob.setJarByClass(Aggregation.class); long start = System.currentTimeMillis(); aggJob.submit(); aggJob.waitForCompletion(true); System.out.println(jobName + "\t" + (System.currentTimeMillis() - start)); // moving files to right place for (String dataset : shortDatasetAggregation) { String from = s3bucket + FrameworkUtils.aggregatesDir + "/tmp/" + dataset + "/"; String to = s3bucket + FrameworkUtils.aggregatesDir + "/" + dataset + "/"; FrameworkUtils.renameFile(from, to, s3conf, s3); } }
From source file:DIA_Umpire_Quant.DIA_Umpire_ExtLibSearch.java
/** * @param args the command line arguments */// w ww.j a va2 s. c o m public static void main(String[] args) throws FileNotFoundException, IOException, Exception { System.out.println( "================================================================================================="); System.out.println("DIA-Umpire targeted re-extraction analysis using external library (version: " + UmpireInfo.GetInstance().Version + ")"); if (args.length != 1) { System.out.println( "command format error, the correct format should be: java -jar -Xmx10G DIA_Umpire_ExtLibSearch.jar diaumpire_module.params"); return; } try { ConsoleLogger.SetConsoleLogger(Level.INFO); ConsoleLogger.SetFileLogger(Level.DEBUG, FilenameUtils.getFullPath(args[0]) + "diaumpire_extlibsearch.log"); } catch (Exception e) { } Logger.getRootLogger().info("Version: " + UmpireInfo.GetInstance().Version); Logger.getRootLogger().info("Parameter file:" + args[0]); BufferedReader reader = new BufferedReader(new FileReader(args[0])); String line = ""; String WorkFolder = ""; int NoCPUs = 2; String ExternalLibPath = ""; String ExternalLibDecoyTag = "DECOY"; float ExtProbThreshold = 0.99f; float RTWindow_Ext = -1f; TandemParam tandemPara = new TandemParam(DBSearchParam.SearchInstrumentType.TOF5600); HashMap<String, File> AssignFiles = new HashMap<>(); //<editor-fold defaultstate="collapsed" desc="Reading parameter file"> while ((line = reader.readLine()) != null) { line = line.trim(); Logger.getRootLogger().info(line); if (!"".equals(line) && !line.startsWith("#")) { //System.out.println(line); if (line.equals("==File list begin")) { do { line = reader.readLine(); line = line.trim(); if (line.equals("==File list end")) { continue; } else if (!"".equals(line)) { File newfile = new File(line); if (newfile.exists()) { AssignFiles.put(newfile.getAbsolutePath(), newfile); } else { Logger.getRootLogger().info("File: " + newfile + " does not exist."); } } } while (!line.equals("==File list end")); } if (line.split("=").length < 2) { continue; } String type = line.split("=")[0].trim(); String value = line.split("=")[1].trim(); switch (type) { case "Path": { WorkFolder = value; break; } case "path": { WorkFolder = value; break; } case "Thread": { NoCPUs = Integer.parseInt(value); break; } case "Fasta": { tandemPara.FastaPath = value; break; } case "DecoyPrefix": { if (!"".equals(value)) { tandemPara.DecoyPrefix = value; } break; } case "ExternalLibPath": { ExternalLibPath = value; break; } case "ExtProbThreshold": { ExtProbThreshold = Float.parseFloat(value); break; } case "RTWindow_Ext": { RTWindow_Ext = Float.parseFloat(value); break; } case "ExternalLibDecoyTag": { ExternalLibDecoyTag = value; if (ExternalLibDecoyTag.endsWith("_")) { ExternalLibDecoyTag = ExternalLibDecoyTag.substring(0, ExternalLibDecoyTag.length() - 1); } break; } } } } //</editor-fold> //Initialize PTM manager using compomics library PTMManager.GetInstance(); //Check if the fasta file can be found if (!new File(tandemPara.FastaPath).exists()) { Logger.getRootLogger().info("Fasta file :" + tandemPara.FastaPath + " cannot be found, the process will be terminated, please check."); System.exit(1); } //Generate DIA file list ArrayList<DIAPack> FileList = new ArrayList<>(); File folder = new File(WorkFolder); if (!folder.exists()) { Logger.getRootLogger().info("The path : " + WorkFolder + " cannot be found."); System.exit(1); } for (final File fileEntry : folder.listFiles()) { if (fileEntry.isFile() && (fileEntry.getAbsolutePath().toLowerCase().endsWith(".mzxml") | fileEntry.getAbsolutePath().toLowerCase().endsWith(".mzml")) && !fileEntry.getAbsolutePath().toLowerCase().endsWith("q1.mzxml") && !fileEntry.getAbsolutePath().toLowerCase().endsWith("q2.mzxml") && !fileEntry.getAbsolutePath().toLowerCase().endsWith("q3.mzxml")) { AssignFiles.put(fileEntry.getAbsolutePath(), fileEntry); } if (fileEntry.isDirectory()) { for (final File fileEntry2 : fileEntry.listFiles()) { if (fileEntry2.isFile() && (fileEntry2.getAbsolutePath().toLowerCase().endsWith(".mzxml") | fileEntry2.getAbsolutePath().toLowerCase().endsWith(".mzml")) && !fileEntry2.getAbsolutePath().toLowerCase().endsWith("q1.mzxml") && !fileEntry2.getAbsolutePath().toLowerCase().endsWith("q2.mzxml") && !fileEntry2.getAbsolutePath().toLowerCase().endsWith("q3.mzxml")) { AssignFiles.put(fileEntry2.getAbsolutePath(), fileEntry2); } } } } Logger.getRootLogger().info("No. of files assigned :" + AssignFiles.size()); for (File fileEntry : AssignFiles.values()) { Logger.getRootLogger().info(fileEntry.getAbsolutePath()); } for (File fileEntry : AssignFiles.values()) { String mzXMLFile = fileEntry.getAbsolutePath(); if (mzXMLFile.toLowerCase().endsWith(".mzxml") | mzXMLFile.toLowerCase().endsWith(".mzml")) { DIAPack DiaFile = new DIAPack(mzXMLFile, NoCPUs); Logger.getRootLogger().info( "================================================================================================="); Logger.getRootLogger().info("Processing " + mzXMLFile); if (!DiaFile.LoadDIASetting()) { Logger.getRootLogger().info("Loading DIA setting failed, job is incomplete"); System.exit(1); } if (!DiaFile.LoadParams()) { Logger.getRootLogger().info("Loading parameters failed, job is incomplete"); System.exit(1); } Logger.getRootLogger().info("Loading identification results " + mzXMLFile + "...."); //If the serialization file for ID file existed if (DiaFile.ReadSerializedLCMSID()) { DiaFile.IDsummary.ReduceMemoryUsage(); DiaFile.IDsummary.FastaPath = tandemPara.FastaPath; FileList.add(DiaFile); } } } //<editor-fold defaultstate="collapsed" desc="Targeted re-extraction using external library"> //External library search Logger.getRootLogger().info("Targeted extraction using external library"); //Read exteranl library FragmentLibManager ExlibManager = FragmentLibManager.ReadFragmentLibSerialization(WorkFolder, FilenameUtils.getBaseName(ExternalLibPath)); if (ExlibManager == null) { ExlibManager = new FragmentLibManager(FilenameUtils.getBaseName(ExternalLibPath)); //Import traML file ExlibManager.ImportFragLibByTraML(ExternalLibPath, ExternalLibDecoyTag); //Check if there are decoy spectra ExlibManager.CheckDecoys(); //ExlibManager.ImportFragLibBySPTXT(ExternalLibPath); ExlibManager.WriteFragmentLibSerialization(WorkFolder); } Logger.getRootLogger() .info("No. of peptide ions in external lib:" + ExlibManager.PeptideFragmentLib.size()); for (DIAPack diafile : FileList) { if (diafile.IDsummary == null) { diafile.ReadSerializedLCMSID(); } //Generate RT mapping RTMappingExtLib RTmap = new RTMappingExtLib(diafile.IDsummary, ExlibManager, diafile.GetParameter()); RTmap.GenerateModel(); RTmap.GenerateMappedPepIon(); diafile.BuildStructure(); diafile.MS1FeatureMap.ReadPeakCluster(); diafile.GenerateMassCalibrationRTMap(); //Perform targeted re-extraction diafile.TargetedExtractionQuant(false, ExlibManager, ExtProbThreshold, RTWindow_Ext); diafile.MS1FeatureMap.ClearAllPeaks(); diafile.IDsummary.ReduceMemoryUsage(); //Remove target IDs below the defined probability threshold diafile.IDsummary.RemoveLowProbMappedIon(ExtProbThreshold); diafile.ExportID(); diafile.ClearStructure(); Logger.getRootLogger().info("Peptide ions: " + diafile.IDsummary.GetPepIonList().size() + " Mapped ions: " + diafile.IDsummary.GetMappedPepIonList().size()); } //</editor-fold> Logger.getRootLogger().info("Job done"); Logger.getRootLogger().info( "================================================================================================="); }
From source file:com.netscape.cms.servlet.test.DRMTest.java
public static void main(String args[]) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, SignatureException, IOException { String host = null;/*from w w w .j a v a 2s . c o m*/ String port = null; String token_pwd = null; String db_dir = "./"; String protocol = "http"; String clientCertNickname = "KRA Administrator of Instance pki-kra's SjcRedhat Domain ID"; // parse command line arguments Options options = new Options(); options.addOption("h", true, "Hostname of the DRM"); options.addOption("p", true, "Port of the DRM"); options.addOption("w", true, "Token password"); options.addOption("d", true, "Directory for tokendb"); options.addOption("s", true, "Attempt Optional Secure SSL connection"); options.addOption("c", true, "Optional SSL Client cert Nickname"); try { CommandLineParser parser = new PosixParser(); CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("h")) { host = cmd.getOptionValue("h"); } else { System.err.println("Error: no hostname provided."); usage(options); } if (cmd.hasOption("p")) { port = cmd.getOptionValue("p"); } else { System.err.println("Error: no port provided"); usage(options); } if (cmd.hasOption("w")) { token_pwd = cmd.getOptionValue("w"); } else { System.err.println("Error: no token password provided"); usage(options); } if (cmd.hasOption("d")) { db_dir = cmd.getOptionValue("d"); } if (cmd.hasOption("s")) { if (cmd.getOptionValue("s") != null && cmd.getOptionValue("s").equals("true")) { protocol = "https"; } } if (cmd.hasOption("c")) { String nick = cmd.getOptionValue("c"); if (nick != null && protocol.equals("https")) { clientCertNickname = nick; } } } catch (ParseException e) { System.err.println("Error in parsing command line options: " + e.getMessage()); usage(options); } // used for crypto operations byte iv[] = { 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1 }; try { iv = genIV(8); } catch (Exception e) { log("Can't generate initialization vector use default: " + e.toString()); } // used for wrapping to send data to DRM String transportCert = null; // Data to be archived SymmetricKey vek = null; String passphrase = null; // Session keys and passphrases for recovery SymmetricKey sessionKey = null; byte[] wrappedRecoveryKey = null; String recoveryPassphrase = null; byte[] wrappedRecoveryPassphrase = null; // retrieved data (should match archived data) byte[] encryptedData = null; String recoveredKey = null; // various ids used in recovery/archival operations KeyId keyId = null; String clientKeyId = null; RequestId recoveryRequestId = null; // Variables for data structures from calls KeyRequestResponse requestResponse = null; Key keyData = null; KeyInfo keyInfo = null; // Initialize token try { CryptoManager.initialize(db_dir); } catch (AlreadyInitializedException e) { // it is ok if it is already initialized } catch (Exception e) { log("INITIALIZATION ERROR: " + e.toString()); System.exit(1); } // Set base URI and get client KRAClient client; SystemCertClient systemCertClient; KeyClient keyClient; NSSCryptoProvider nssCrypto; try { ClientConfig config = new ClientConfig(); config.setServerURI(protocol + "://" + host + ":" + port + "/kra"); config.setCertNickname(clientCertNickname); config.setCertDatabase(db_dir); config.setCertPassword(token_pwd); nssCrypto = new NSSCryptoProvider(config); client = new KRAClient(new PKIClient(config, nssCrypto)); systemCertClient = (SystemCertClient) client.getClient("systemcert"); keyClient = (KeyClient) client.getClient("key"); } catch (Exception e) { e.printStackTrace(); return; } // Test 1: Get transport certificate from DRM transportCert = systemCertClient.getTransportCert().getEncoded(); transportCert = transportCert.substring(CertData.HEADER.length(), transportCert.indexOf(CertData.FOOTER)); keyClient.setTransportCert(transportCert); log("Transport Cert retrieved from DRM: " + transportCert); // Test 2: Get list of completed key archival requests log("\n\nList of completed archival requests"); KeyRequestInfoCollection list = keyClient.listRequests("complete", "securityDataEnrollment"); if (list.getTotal() == 0) { log("No requests found"); } else { Iterator<KeyRequestInfo> iter = list.getEntries().iterator(); while (iter.hasNext()) { KeyRequestInfo info = iter.next(); printRequestInfo(info); } } // Test 3: Get list of key recovery requests log("\n\nList of completed recovery requests"); KeyRequestInfoCollection list2 = keyClient.listRequests("complete", "securityDataRecovery"); if (list2.getTotal() == 0) { log("No requests found"); } else { Iterator<KeyRequestInfo> iter2 = list2.getEntries().iterator(); while (iter2.hasNext()) { KeyRequestInfo info = iter2.next(); printRequestInfo(info); } } // Test 4: Generate and archive a symmetric key log("Archiving symmetric key"); clientKeyId = "UUID: 123-45-6789 VEK " + Calendar.getInstance().getTime().toString(); try { vek = nssCrypto.generateSessionKey(); byte[] encoded = nssCrypto.createPKIArchiveOptions(transportCert, vek, null, KeyRequestResource.DES3_ALGORITHM, 0, iv); KeyRequestResponse info = keyClient.archivePKIOptions(clientKeyId, KeyRequestResource.SYMMETRIC_KEY_TYPE, KeyRequestResource.DES3_ALGORITHM, 0, encoded); log("Archival Results:"); printRequestInfo(info.getRequestInfo()); keyId = info.getKeyId(); } catch (Exception e) { log("Exception in archiving symmetric key:" + e.getMessage()); e.printStackTrace(); } //Test 5: Get keyId for active key with client ID log("Getting key ID for symmetric key"); keyInfo = keyClient.getActiveKeyInfo(clientKeyId); printKeyInfo(keyInfo); KeyId keyId2 = keyInfo.getKeyId(); if (keyId2 == null) { log("No archived key found"); } else { log("Archived Key found: " + keyId); } if (!keyId.equals(keyId2)) { log("Error: key ids from search and archival do not match"); } else { log("Success: keyids from search and archival match."); } // Test 6: Submit a recovery request for the symmetric key using a session key log("Submitting a recovery request for the symmetric key using session key"); try { sessionKey = nssCrypto.generateSessionKey(); wrappedRecoveryKey = CryptoUtil.wrapSymmetricKey(nssCrypto.getManager(), nssCrypto.getToken(), transportCert, sessionKey); keyData = keyClient.retrieveKey(keyId, wrappedRecoveryKey); } catch (Exception e) { log("Exception in recovering symmetric key using session key: " + e.getMessage()); } encryptedData = keyData.getEncryptedData(); try { recoveredKey = Utils.base64encode(nssCrypto.unwrapWithSessionKey(encryptedData, sessionKey, KeyRequestResource.DES3_ALGORITHM, keyData.getNonceData())); } catch (Exception e) { log("Exception in unwrapping key: " + e.toString()); e.printStackTrace(); } if (!recoveredKey.equals(Utils.base64encode(vek.getEncoded()))) { log("Error: recovered and archived keys do not match!"); } else { log("Success: recoverd and archived keys match!"); } // Test 7: Submit a recovery request for the symmetric key using a passphrase log("Submitting a recovery request for the symmetric key using a passphrase"); recoveryPassphrase = "Gimme me keys please"; try { sessionKey = nssCrypto.generateSessionKey(); wrappedRecoveryPassphrase = nssCrypto.wrapWithSessionKey(recoveryPassphrase, iv, sessionKey, KeyRequestResource.DES3_ALGORITHM); wrappedRecoveryKey = nssCrypto.wrapSessionKeyWithTransportCert(sessionKey, transportCert); keyData = keyClient.retrieveKeyUsingWrappedPassphrase(keyId, wrappedRecoveryKey, wrappedRecoveryPassphrase, iv); } catch (Exception e) { log("Exception in recovering symmetric key using passphrase" + e.toString()); e.printStackTrace(); } encryptedData = keyData.getEncryptedData(); try { recoveredKey = Utils.base64encode(nssCrypto.unwrapWithPassphrase(encryptedData, recoveryPassphrase)); } catch (Exception e) { log("Error: unable to unwrap key using passphrase"); e.printStackTrace(); } if (recoveredKey == null || !recoveredKey.equals(Utils.base64encode(vek.getEncoded()))) { log("Error: recovered and archived keys do not match!"); } else { log("Success: recovered and archived keys do match!"); } passphrase = "secret12345"; // Test 8: Generate and archive a passphrase clientKeyId = "UUID: 123-45-6789 RKEK " + Calendar.getInstance().getTime().toString(); try { requestResponse = keyClient.archivePassphrase(clientKeyId, passphrase); log("Archival Results:"); printRequestInfo(requestResponse.getRequestInfo()); keyId = requestResponse.getKeyId(); } catch (Exception e) { log("Exception in archiving symmetric key:" + e.toString()); e.printStackTrace(); } //Test 9: Get keyId for active passphrase with client ID log("Getting key ID for passphrase"); keyInfo = keyClient.getActiveKeyInfo(clientKeyId); printKeyInfo(keyInfo); keyId2 = keyInfo.getKeyId(); if (keyId2 == null) { log("No archived key found"); } else { log("Archived Key found: " + keyId); } if (!keyId.equals(keyId2)) { log("Error: key ids from search and archival do not match"); } else { log("Success: key ids from search and archival do match!"); } // Test 10: Submit a recovery request for the passphrase using a session key log("Submitting a recovery request for the passphrase using session key"); sessionKey = null; wrappedRecoveryKey = null; try { keyData = keyClient.retrieveKeyByPassphrase(keyId, recoveryPassphrase); } catch (Exception e) { log("Exception in recovering passphrase using session key: " + e.getMessage()); } encryptedData = keyData.getEncryptedData(); try { recoveredKey = new String(nssCrypto.unwrapWithPassphrase(encryptedData, recoveryPassphrase), "UTF-8"); } catch (Exception e) { log("Exception in unwrapping key: " + e.toString()); e.printStackTrace(); } if (recoveredKey == null || !recoveredKey.equals(passphrase)) { log("Error: recovered and archived passphrases do not match!"); } else { log("Success: recovered and archived passphrases do match!"); } // Test 11: Submit a recovery request for the passphrase using a passphrase try { sessionKey = nssCrypto.generateSessionKey(); wrappedRecoveryKey = nssCrypto.wrapSessionKeyWithTransportCert(sessionKey, transportCert); wrappedRecoveryPassphrase = nssCrypto.wrapWithSessionKey(recoveryPassphrase, iv, sessionKey, KeyRequestResource.DES3_ALGORITHM); keyData = keyClient.retrieveKeyUsingWrappedPassphrase(keyId, wrappedRecoveryKey, wrappedRecoveryPassphrase, iv); } catch (Exception e1) { e1.printStackTrace(); System.out.println("Test 17: " + e1.getMessage()); System.exit(-1); } encryptedData = keyData.getEncryptedData(); try { recoveredKey = new String(nssCrypto.unwrapWithPassphrase(encryptedData, recoveryPassphrase), "UTF-8"); } catch (Exception e) { log("Error: cannot unwrap key using passphrase"); e.printStackTrace(); } if (recoveredKey == null || !recoveredKey.equals(passphrase)) { log("Error: recovered and archived passphrases do not match!"); } else { log("Success: recovered and archived passphrases do match!"); } // Test 12: Get key log("Getting passphrase: " + keyId); try { keyData = keyClient.retrieveKeyByPassphrase(keyId, recoveryPassphrase); } catch (Exception e1) { e1.printStackTrace(); } encryptedData = keyData.getEncryptedData(); try { recoveredKey = new String(nssCrypto.unwrapWithPassphrase(encryptedData, recoveryPassphrase), "UTF-8"); } catch (Exception e) { log("Error: Can't unwrap recovered key using passphrase"); e.printStackTrace(); } if (recoveredKey == null || !recoveredKey.equals(passphrase)) { log("Error: recovered and archived passphrases do not match!"); } else { log("Success: recovered and archived passphrases do match!"); } // Test 13: Get non-existent request RequestId requestId = new RequestId("0xabcdef"); log("Getting non-existent request: " + requestId.toHexString()); try { keyClient.getRequestInfo(requestId); log("Error: getting non-existent request does not throw an exception"); } catch (RequestNotFoundException e) { log("Success: getting non-existent request throws an exception: " + e.getMessage() + " (" + e.getRequestId().toHexString() + ")"); } // Test 14: Request x509 key recovery // This test requires to retrieve keyId and matching certificate // from installed instances of CA and DRM String keyID = "1"; String b64Certificate = "MIIC+TCCAeGgAwIBAgIBDDANBgkqhkiG9w0BAQsFADBOMSswKQYDVQQKDCJ1c2Vy" + "c3lzLnJlZGhhdC5jb20gU2VjdXJpdHkgRG9tYWluMR8wHQYDVQQDDBZDQSBTaWdu" + "aW5nIENlcnRpZmljYXRlMB4XDTEzMTAyNTE5MzQwM1oXDTE0MDQyMzE5MzQwM1ow" + "EzERMA8GCgmSJomT8ixkAQEMAXgwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGB" + "ALhLfGmKvxFsKXPh49q1QsluXU3WlyS1XnpDLgOAhgTNgO4sG6CpPdv6hZYIvQBb" + "ZQ5bhuML+NXK+Q+EIiNk1cUTxgL3a30sPzy6QaFWxwM8i4uXm4nCBYv7T+n4V6/O" + "xHIM2Ch/dviAb3vz+M9trErv9t+d2H8jNXT3sHuDb/kvAgMBAAGjgaAwgZ0wHwYD" + "VR0jBBgwFoAUh1cxWFRY+nMsx4odQQI1GqyFxP8wSwYIKwYBBQUHAQEEPzA9MDsG" + "CCsGAQUFBzABhi9odHRwOi8vZG9ndGFnMjAudXNlcnN5cy5yZWRoYXQuY29tOjgw" + "ODAvY2Evb2NzcDAOBgNVHQ8BAf8EBAMCBSAwHQYDVR0lBBYwFAYIKwYBBQUHAwIG" + "CCsGAQUFBwMEMA0GCSqGSIb3DQEBCwUAA4IBAQCvmbUzQOouE2LgQQcKfmgwwJMJ" + "9tMrPwDUtyFdaIFoPL4uZaujSscaN4IWK2r5vIMJ65jwYCI7sI9En2ZfO28J9dQj" + "lpqu6TaJ+xtaMk7OvXpVB7lJk73HAttMGjETlkoq/6EjxcugmJsDqVD0b2tO7Vd0" + "hroBe2uPDHM2ASewZF415lUcRh0URtmxSazTInbyxpmy1wgSJQ0C6fMCeT+hUFlA" + "0P4k1TIprapGVq7FpKcqlhK2gTBfTSnoO7gmXG/9MxJiYpb/Aph8ptXq6quHz1Mj" + "greWr3xTsy6gF2yphUEkGHh4v22XvK+FLx9Jb6zloMWA2GG9gpUpvMnl1fH4"; log("Requesting X509 key recovery."); recoveryRequestId = keyClient.recoverKey(new KeyId(keyID), null, null, null, b64Certificate) .getRequestInfo().getRequestId(); log("Requesting X509 key recovery request: " + recoveryRequestId); // Test 55: Approve x509 key recovery log("Approving X509 key recovery request: " + recoveryRequestId); keyClient.approveRequest(recoveryRequestId); // Test 16: Recover x509 key log("Recovering X509 key based on request: " + recoveryRequestId); try { // KeyData recoveredX509Key = client.recoverKey(recoveryRequestId, "netscape"); // log("Success: X509Key recovered: "+ recoveredX509Key.getP12Data()); } catch (RequestNotFoundException e) { log("Error: recovering X509Key"); } // Test 1: Get transport certificate from DRM transportCert = systemCertClient.getTransportCert().getEncoded(); transportCert = transportCert.substring(CertData.HEADER.length(), transportCert.indexOf(CertData.FOOTER)); log("Transport Cert retrieved from DRM: " + transportCert); // Test 17: Get list of completed key archival requests log("\n\nList of completed archival requests"); list = keyClient.listRequests("complete", IRequest.SYMKEY_GENERATION_REQUEST); if (list.getTotal() == 0) { log("No requests found"); } else { Iterator<KeyRequestInfo> iter = list.getEntries().iterator(); while (iter.hasNext()) { KeyRequestInfo info = iter.next(); printRequestInfo(info); } } // test 18: Generate symmetric key clientKeyId = "Symmetric Key #1234f " + Calendar.getInstance().getTime().toString(); List<String> usages = new ArrayList<String>(); usages.add(SymKeyGenerationRequest.DECRYPT_USAGE); usages.add(SymKeyGenerationRequest.ENCRYPT_USAGE); KeyRequestResponse genKeyResponse = keyClient.generateSymmetricKey(clientKeyId, KeyRequestResource.AES_ALGORITHM, 128, usages, null); printRequestInfo(genKeyResponse.getRequestInfo()); keyId = genKeyResponse.getKeyId(); // test 19: Get keyId for active key with client ID log("Getting key ID for symmetric key"); keyInfo = keyClient.getActiveKeyInfo(clientKeyId); printKeyInfo(keyInfo); keyId2 = keyInfo.getKeyId(); if (keyId2 == null) { log("No archived key found"); } else { log("Archived Key found: " + keyId); } if (!keyId.equals(keyId2)) { log("Error: key ids from search and archival do not match"); } else { log("Success: keyids from search and archival match."); } // Test 20: Submit a recovery request for the symmetric key using a session key log("Submitting a recovery request for the symmetric key using session key"); try { sessionKey = nssCrypto.generateSessionKey(); wrappedRecoveryKey = nssCrypto.wrapSessionKeyWithTransportCert(sessionKey, transportCert); keyData = keyClient.retrieveKey(keyId, wrappedRecoveryKey); } catch (Exception e) { log("Exception in recovering symmetric key using session key: " + e.getMessage()); } encryptedData = keyData.getEncryptedData(); try { recoveredKey = new String(nssCrypto.unwrapWithSessionKey(encryptedData, sessionKey, KeyRequestResource.DES3_ALGORITHM, keyData.getNonceData())); } catch (Exception e) { log("Exception in unwrapping key: " + e.toString()); e.printStackTrace(); } // test 21: Generate symmetric key - invalid algorithm try { genKeyResponse = keyClient.generateSymmetricKey("Symmetric Key #1235", "AFS", 128, usages, null); } catch (Exception e) { log("Exception: " + e); } // test 22: Generate symmetric key - invalid key size try { genKeyResponse = keyClient.generateSymmetricKey("Symmetric Key #1236", "AES", 0, usages, null); } catch (Exception e) { log("Exception: " + e); } // test 23: Generate symmetric key - usages not defined try { genKeyResponse = keyClient.generateSymmetricKey("Symmetric Key #1236", "DES", 56, null, null); } catch (Exception e) { log("Exception: " + e); } // Test 24: Generate and archive a symmetric key of type AES log("Archiving symmetric key"); clientKeyId = "UUID: 123-45-6789 VEK " + Calendar.getInstance().getTime().toString(); try { vek = nssCrypto.generateSymmetricKey(KeyRequestResource.AES_ALGORITHM, 128); byte[] encoded = CryptoUtil.createPKIArchiveOptions(nssCrypto.getManager(), nssCrypto.getToken(), transportCert, vek, null, KeyGenAlgorithm.DES3, 0, new IVParameterSpec(iv)); KeyRequestResponse response = keyClient.archivePKIOptions(clientKeyId, KeyRequestResource.SYMMETRIC_KEY_TYPE, KeyRequestResource.AES_ALGORITHM, 128, encoded); log("Archival Results:"); printRequestInfo(response.getRequestInfo()); keyId = response.getKeyId(); } catch (Exception e) { log("Exception in archiving symmetric key:" + e.getMessage()); e.printStackTrace(); } //Test 25: Get keyId for active key with client ID log("Getting key ID for symmetric key"); keyInfo = keyClient.getActiveKeyInfo(clientKeyId); printKeyInfo(keyInfo); keyId2 = keyInfo.getKeyId(); if (keyId2 == null) { log("No archived key found"); } else { log("Archived Key found: " + keyId); } if (!keyId.equals(keyId2)) { log("Error: key ids from search and archival do not match"); } else { log("Success: keyids from search and archival match."); } // Test 26: Submit a recovery request for the symmetric key log("Submitting a recovery request for the symmetric key without using session key"); try { keyData = keyClient.retrieveKey(keyId); } catch (Exception e) { log("Exception in recovering symmetric key using session key: " + e.getMessage()); } // Since no session key is provided externally, the retrieveKey method // generates a session key, wraps it with transport cert and completes the request. // The encrypted data is then unwrapped using the temporary session key and set to // the attribute privateData. recoveredKey = Utils.base64encode(keyData.getData()); if (!recoveredKey.equals(Utils.base64encode(vek.getEncoded()))) { log("Error: recovered and archived keys do not match!"); } else { log("Success: recoverd and archived keys match!"); } // Test 27: Get key info log("getting key info for existing key"); printKeyInfo(keyClient.getKeyInfo(keyId)); //Test 28: Modify status log("modify the key status"); keyClient.modifyKeyStatus(keyId, KeyResource.KEY_STATUS_INACTIVE); keyInfo = keyClient.getKeyInfo(keyId); printKeyInfo(keyInfo); //Test 29: Confirm no more active keys with this ID log("look for active keys with this id"); clientKeyId = keyInfo.getClientKeyID(); try { keyInfo = keyClient.getActiveKeyInfo(clientKeyId); printKeyInfo(keyInfo); } catch (ResourceNotFoundException e) { log("Success: ResourceNotFound exception thrown: " + e); } // Test asymmetric key generation. String[] algs = { "RSA", "DSA" }; for (int i = 0; i < algs.length; i++) { // Test 30: Generate Asymmetric keys - RSA key System.out.println("\nTesting asymmetric key generation for algorithm " + algs[i]); clientKeyId = "AsymKey #" + Calendar.getInstance().getTimeInMillis(); usages.clear(); usages.add(AsymKeyGenerationRequest.SIGN); usages.add(AsymKeyGenerationRequest.VERIFY); KeyRequestResponse response = keyClient.generateAsymmetricKey(clientKeyId, algs[i], 1024, usages, null); printRequestInfo(response.getRequestInfo()); System.out.println(); // Test 31: Get information of the newly generated asymmetric keys System.out.println("Fetch information of the newly generated asymmetric keys."); System.out.println(); KeyInfo info = keyClient.getKeyInfo(response.getKeyId()); printKeyInfo(info); System.out.println(); // Test 32: Retrieve private key data System.out.println("Retrieving and verifying the generated private key."); try { keyData = keyClient.retrieveKey(response.getKeyId()); } catch (Exception e) { log("Exception retrieving the private key data."); e.printStackTrace(); } // Test 33: Verify the generated key pair. if (isKeyPairValid(algs[i], keyData.getData(), info.getPublicKey())) { log("The key pair generated using " + algs[i] + " algorithm is valid."); } else { log("The key pair generated using " + algs[i] + " algorithm is invalid."); } System.out.println(); } // Test 34: }
From source file:net.sf.extjwnl.cli.ewn.java
public static void main(String[] args) throws IOException, JWNLException { if (args.length < 1) { System.out.println(USAGE); System.exit(0);//from w w w. j a va 2 s .c o m } //find dictionary Dictionary d = null; File config = new File(defaultConfig); if (!config.exists()) { if (System.getenv().containsKey("WNHOME")) { String wnHomePath = System.getenv().get("WNHOME"); File wnHome = new File(wnHomePath); if (wnHome.exists()) { d = Dictionary.getFileBackedInstance(wnHomePath); } else { log.error("Cannot find dictionary. Make sure " + defaultConfig + " is available or WNHOME variable is set."); } } } else { d = Dictionary.getInstance(new FileInputStream(config)); } if (null != d) { //parse and execute command line if ((-1 < args[0].indexOf('%') && -1 < args[0].indexOf(':')) || "-script".equals(args[0]) || (-1 < args[0].indexOf('#'))) { d.edit(); //edit if ("-script".equals(args[0])) { if (args.length < 2) { log.error("Filename missing for -script command"); System.exit(1); } else { File script = new File(args[1]); if (script.exists()) { //load into args ArrayList<String> newArgs = new ArrayList<String>(); BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(script), "UTF-8")); try { String str; while ((str = in.readLine()) != null) { String[] bits = str.split(" "); StringBuilder tempArg = null; for (String bit : bits) { int quoteCnt = 0; for (int j = 0; j < bit.length(); j++) { if ('"' == bit.charAt(j)) { quoteCnt++; } } if (null != tempArg) { if (0 == quoteCnt) { tempArg.append(" ").append(bit); } else { tempArg.append(" ").append(bit.replaceAll("\"\"", "\"")); if (1 == (quoteCnt % 2)) { newArgs.add( tempArg.toString().substring(1, tempArg.length() - 1)); tempArg = null; } } } else { if (0 == quoteCnt) { newArgs.add(bit); } else { if (1 == (quoteCnt % 2)) { tempArg = new StringBuilder(bit.replaceAll("\"\"", "\"")); } else { newArgs.add(bit.replaceAll("\"\"", "\"")); } } } } if (null != tempArg) { newArgs.add(tempArg.toString()); } } } finally { try { in.close(); } catch (IOException e) { //nop } } args = newArgs.toArray(args); } } } Word workWord = null; String key = null; String lemma = null; int lexFileNum = -1; int lexId = -1; // String headLemma = null; // int headLexId = -1; POS pos = null; String derivation = null; for (int i = 0; i < args.length; i++) { if (null == key && '-' != args[i].charAt(0) && ((-1 < args[i].indexOf('%') && -1 < args[i].indexOf(':')))) { key = args[i]; log.info("Searching " + key + "..."); if (null != key) { workWord = d.getWordBySenseKey(key); } if (null == workWord) { //parse sensekey lemma = key.substring(0, key.indexOf('%')).replace('_', ' '); String posId = key.substring(key.indexOf('%') + 1, key.indexOf(':')); if ("1".equals(posId) || "2".equals(posId) || "3".equals(posId) || "4".equals(posId) || "5".equals(posId)) { pos = POS.getPOSForId(Integer.parseInt(posId)); String lexFileString = key.substring(key.indexOf(':') + 1); if (-1 < lexFileString.indexOf(':')) { lexFileNum = Integer .parseInt(lexFileString.substring(0, lexFileString.indexOf(':'))); if (lexFileString.indexOf(':') + 1 < lexFileString.length()) { String lexIdString = lexFileString .substring(lexFileString.indexOf(':') + 1); if (-1 < lexIdString.indexOf(':')) { lexId = Integer .parseInt(lexIdString.substring(0, lexIdString.indexOf(':'))); // if (lexIdString.indexOf(':') + 1 < lexIdString.length()) { // headLemma = lexIdString.substring(lexIdString.indexOf(':') + 1); // if (-1 < headLemma.indexOf(':')) { // headLemma = headLemma.substring(0, headLemma.indexOf(':')); // if (null != headLemma && !"".equals(headLemma) && lexIdString.lastIndexOf(':') + 1 < lexIdString.length()) { // headLexId = Integer.parseInt(lexIdString.substring(lexIdString.lastIndexOf(':') + 1)); // } // } else { // log.error("Malformed sensekey " + key); // System.exit(1); // } // } } else { log.error("Malformed sensekey " + key); System.exit(1); } } else { log.error("Malformed sensekey " + key); System.exit(1); } } else { log.error("Malformed sensekey " + key); System.exit(1); } } else { log.error("Malformed sensekey " + key); System.exit(1); } } } else if (-1 < args[i].indexOf('#')) { if (2 < args[i].length()) { derivation = args[i].substring(2); if (null == derivation) { log.error("Missing derivation"); System.exit(1); } else { pos = POS.getPOSForKey(args[i].substring(0, 1)); if (null == pos) { log.error("POS " + args[i] + " is not recognized for derivation " + derivation); System.exit(1); } } } } if ("-add".equals(args[i])) { if (null == key) { log.error("Missing sensekey"); System.exit(1); } if (null != workWord) { log.error("Duplicate sensekey " + workWord.getSenseKey()); System.exit(1); } log.info("Creating " + pos.getLabel() + " synset..."); Synset tempSynset = d.createSynset(pos); log.info("Creating word " + lemma + "..."); workWord = new Word(d, tempSynset, 1, lemma); workWord.setLexId(lexId); tempSynset.getWords().add(workWord); tempSynset.setLexFileNum(lexFileNum); key = null; } if ("-remove".equals(args[i])) { if (null == workWord) { log.error("Missing sensekey"); System.exit(1); } else { d.removeSynset(workWord.getSynset()); workWord = null; key = null; } } if ("-addword".equals(args[i])) { if (null == workWord) { log.error("Missing sensekey"); System.exit(1); } else { i++; if (i < args.length && '-' != args[i].charAt(0)) { Word tempWord = new Word(d, workWord.getSynset(), workWord.getSynset().getWords().size() + 1, args[i]); workWord.getSynset().getWords().add(tempWord); key = null; } else { log.error( "Missing word for addword command for sensekey " + workWord.getSenseKey()); System.exit(1); } } } if ("-removeword".equals(args[i])) { if (null == workWord) { log.error("Missing sensekey"); System.exit(1); } else { workWord.getSynset().getWords().remove(workWord); key = null; } } if ("-setgloss".equals(args[i])) { if (null == workWord) { log.error("Missing sensekey"); System.exit(1); } else { i++; if (i < args.length && '-' != args[i].charAt(0)) { workWord.getSynset().setGloss(args[i]); key = null; } else { log.error("Missing gloss for setgloss command for sensekey " + workWord.getSenseKey()); System.exit(1); } } } if ("-setadjclus".equals(args[i])) { if (null == workWord) { log.error("Missing sensekey"); System.exit(1); } else { i++; if (i < args.length && '-' != args[i].charAt(0)) { workWord.getSynset().setIsAdjectiveCluster(Boolean.parseBoolean(args[i])); key = null; } else { log.error("Missing flag for setadjclus command for sensekey " + workWord.getSenseKey()); System.exit(1); } } } if ("-setverbframe".equals(args[i])) { if (null == workWord) { log.error("Missing sensekey"); System.exit(1); } else { i++; if (i < args.length) { if (workWord instanceof Verb) { Verb verb = (Verb) workWord; if ('-' == args[i].charAt(0)) { verb.getVerbFrameFlags().clear(Integer.parseInt(args[i].substring(1))); } else { verb.getVerbFrameFlags().set(Integer.parseInt(args[i])); } } else { log.error("Word at " + workWord.getSenseKey() + " should be verb"); System.exit(1); } key = null; } else { log.error("Missing index for setverbframe command for sensekey " + workWord.getSenseKey()); System.exit(1); } } } if ("-setverbframeall".equals(args[i])) { if (null == workWord) { log.error("Missing sensekey"); System.exit(1); } else { i++; if (i < args.length) { if (workWord.getSynset() instanceof VerbSynset) { if ('-' == args[i].charAt(0)) { workWord.getSynset().getVerbFrameFlags() .clear(Integer.parseInt(args[i].substring(1))); } else { workWord.getSynset().getVerbFrameFlags().set(Integer.parseInt(args[i])); } } else { log.error("Synset at " + workWord.getSenseKey() + " should be verb"); System.exit(1); } key = null; } else { log.error("Missing index for setverbframeall command for sensekey " + workWord.getSenseKey()); System.exit(1); } } } if ("-setlexfile".equals(args[i])) { if (null == workWord) { log.error("Missing sensekey"); System.exit(1); } else { i++; if (i < args.length && '-' != args[i].charAt(0)) { if (-1 < args[i].indexOf('.')) { workWord.getSynset() .setLexFileNum(LexFileNameLexFileIdMap.getMap().get(args[i])); } else { workWord.getSynset().setLexFileNum(Integer.parseInt(args[i])); } } else { log.error("Missing file number or name for setlexfile command for sensekey " + workWord.getSenseKey()); System.exit(1); } } } if ("-addptr".equals(args[i])) { if (null == workWord) { log.error("Missing sensekey"); System.exit(1); } else { i++; if (i < args.length) { Word targetWord = d.getWordBySenseKey(args[i]); if (null != targetWord) { i++; if (i < args.length) { PointerType pt = PointerType.getPointerTypeForKey(args[i]); if (null != pt) { Pointer p; if (pt.isLexical()) { p = new Pointer(pt, workWord, targetWord); } else { p = new Pointer(pt, workWord.getSynset(), targetWord.getSynset()); } if (!workWord.getSynset().getPointers().contains(p)) { workWord.getSynset().getPointers().add(p); } else { log.error("Duplicate pointer of type " + pt + " to " + targetWord.getSenseKey() + " in addptr command for sensekey " + workWord.getSenseKey()); System.exit(1); } } else { log.error("Invalid pointer type at " + args[i] + " in addptr command for sensekey " + workWord.getSenseKey()); System.exit(1); } } else { log.error("Missing pointer type at " + args[i] + " in addptr command for sensekey " + workWord.getSenseKey()); System.exit(1); } } else { log.error("Missing target at " + args[i] + " in addptr command for sensekey " + workWord.getSenseKey()); System.exit(1); } key = null; } else { log.error("Missing sensekey for addptr command for sensekey " + workWord.getSenseKey()); System.exit(1); } } } if ("-removeptr".equals(args[i])) { if (null == workWord) { log.error("Missing sensekey"); System.exit(1); } else { i++; if (i < args.length) { Word targetWord = d.getWordBySenseKey(args[i]); if (null != targetWord) { i++; if (i < args.length) { PointerType pt = PointerType.getPointerTypeForKey(args[i]); if (null != pt) { Pointer p; if (pt.isLexical()) { p = new Pointer(pt, workWord, targetWord); } else { p = new Pointer(pt, workWord.getSynset(), targetWord.getSynset()); } if (workWord.getSynset().getPointers().contains(p)) { workWord.getSynset().getPointers().remove(p); } else { log.error("Missing pointer of type " + pt + " to " + targetWord.getSenseKey() + " in removeptr command for sensekey " + workWord.getSenseKey()); System.exit(1); } } else { log.error("Invalid pointer type at " + args[i] + " in removeptr command for sensekey " + workWord.getSenseKey()); System.exit(1); } } else { log.error("Missing pointer type at " + args[i] + " in removeptr command for sensekey " + workWord.getSenseKey()); System.exit(1); } } else { log.error("Missing target at " + args[i] + " in removeptr command for sensekey " + workWord.getSenseKey()); System.exit(1); } key = null; } else { log.error("Missing sensekey for removeptr command for sensekey " + workWord.getSenseKey()); System.exit(1); } } } if ("-setlexid".equals(args[i])) { if (null == workWord) { log.error("Missing sensekey"); System.exit(1); } else { i++; if (i < args.length && '-' != args[i].charAt(0)) { workWord.setLexId(Integer.parseInt(args[i])); key = null; } else { log.error("Missing lexid for setlexid command for sensekey " + workWord.getSenseKey()); System.exit(1); } } } if ("-setusecount".equals(args[i])) { if (null == workWord) { log.error("Missing sensekey"); System.exit(1); } else { i++; if (i < args.length && '-' != args[i].charAt(0)) { workWord.setUseCount(Integer.parseInt(args[i])); key = null; } else { log.error("Missing count for setusecount command for sensekey " + workWord.getSenseKey()); System.exit(1); } } } if ("-addexc".equals(args[i])) { i++; if (i < args.length && '-' != args[i].charAt(0)) { String baseform = args[i]; Exc e = d.getException(pos, derivation); if (null != e) { if (null != e.getExceptions()) { if (!e.getExceptions().contains(baseform)) { e.getExceptions().add(baseform); } } } else { ArrayList<String> list = new ArrayList<String>(1); list.add(baseform); d.createException(pos, derivation, list); } derivation = null; } else { log.error("Missing baseform for addexc command for derivation " + derivation); System.exit(1); } } if ("-removeexc".equals(args[i])) { Exc e = d.getException(pos, derivation); if (null != e) { i++; if (i < args.length && '-' != args[i].charAt(0)) { String baseform = args[i]; if (null != e.getExceptions()) { if (e.getExceptions().contains(baseform)) { e.getExceptions().remove(baseform); } if (0 == e.getExceptions().size()) { d.removeException(e); } } } else { d.removeException(e); } } else { log.error("Missing derivation " + derivation); System.exit(1); } derivation = null; } } d.save(); } else { //browse String key = args[0]; if (1 == args.length) { for (POS pos : POS.getAllPOS()) { IndexWord iw = d.getIndexWord(pos, key); if (null == iw) { System.out.println("\nNo information available for " + pos.getLabel() + " " + key); } else { System.out.println( "\nInformation available for " + iw.getPOS().getLabel() + " " + iw.getLemma()); printAvailableInfo(iw); } if (null != d.getMorphologicalProcessor()) { List<String> forms = d.getMorphologicalProcessor().lookupAllBaseForms(pos, key); if (null != forms) { for (String form : forms) { if (!key.equals(form)) { iw = d.getIndexWord(pos, form); if (null != iw) { System.out.println("\nInformation available for " + iw.getPOS().getLabel() + " " + iw.getLemma()); printAvailableInfo(iw); } } } } } } } else { boolean needHelp = false; boolean needGloss = false; boolean needLex = false; boolean needOffset = false; boolean needSenseNum = false; boolean needSenseKeys = false; int needSense = 0; for (String arg : args) { if ("-h".equals(arg)) { needHelp = true; } if ("-g".equals(arg)) { needGloss = true; } if ("-a".equals(arg)) { needLex = true; } if ("-o".equals(arg)) { needOffset = true; } if ("-s".equals(arg)) { needSenseNum = true; } if ("-k".equals(arg)) { needSenseKeys = true; } if (arg.startsWith("-n") && 2 < arg.length()) { needSense = Integer.parseInt(arg.substring(2)); } } for (String arg : args) { if (arg.startsWith("-ants") && 6 == arg.length()) { if (needHelp) { System.out.println( "Display synsets containing direct antonyms of the search string.\n" + "\n" + "Direct antonyms are a pair of words between which there is an\n" + "associative bond built up by co-occurrences.\n" + "\n" + "Antonym synsets are preceded by \"=>\"."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nAntonyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.ANTONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //ants if (arg.startsWith("-hype") && 6 == arg.length()) { if (needHelp) { System.out.println( "Recursively display hypernym (superordinate) tree for the search\n" + "string.\n" + "\n" + "Hypernym is the generic term used to designate a whole class of\n" + "specific instances. Y is a hypernym of X if X is a (kind of) Y.\n" + "\n" + "Hypernym synsets are preceded by \"=>\", and are indented from\n" + "the left according to their level in the hierarchy."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nHypernyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.HYPERNYM, PointerUtils.INFINITY, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //hype if (arg.startsWith("-hypo") && 6 == arg.length()) { if (needHelp) { System.out.println( "Display immediate hyponyms (subordinates) for the search string.\n" + "\n" + "Hyponym is the generic term used to designate a member of a class.\n" + "X is a hyponym of Y if X is a (kind of) Y.\n" + "\n" + "Hyponym synsets are preceded by \"=>\"."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nHyponyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.HYPONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //hypo if (arg.startsWith("-tree") && 6 == arg.length()) { if (needHelp) { System.out.println( "Display hyponym (subordinate) tree for the search string. This is\n" + "a recursive search that finds the hyponyms of each hyponym. \n" + "\n" + "Hyponym is the generic term used to designate a member of a class.\n" + "X is a hyponym of Y if X is a (kind of) Y. \n" + "\n" + "Hyponym synsets are preceded by \"=>\", and are indented from the left\n" + "according to their level in the hierarchy."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nHyponyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.HYPONYM, PointerUtils.INFINITY, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //tree if (arg.startsWith("-enta") && 6 == arg.length()) { if (needHelp) { System.out.println( "Recursively display entailment relations of the search string.\n" + "\n" + "The action represented by the verb X entails Y if X cannot be done\n" + "unless Y is, or has been, done.\n" + "\n" + "Entailment synsets are preceded by \"=>\", and are indented from the left\n" + "according to their level in the hierarchy."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nEntailment of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.ENTAILMENT, PointerUtils.INFINITY, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //enta if (arg.startsWith("-syns") && 6 == arg.length()) { POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nSynonyms of " + p.getLabel() + " " + iw.getLemma()); if (POS.ADJECTIVE == p) { if (needHelp) { System.out.println( "Display synonyms and synsets related to synsets containing\n" + "the search string. If the search string is in a head synset\n" + "the 'cluster's' satellite synsets are displayed. If the search\n" + "string is in a satellite synset, its head synset is displayed.\n" + "If the search string is a pertainym the word or synset that it\n" + "pertains to is displayed.\n" + "\n" + "A cluster is a group of adjective synsets that are organized around\n" + "antonymous pairs or triplets. An adjective cluster contains two or more\n" + "head synsets that contan antonyms. Each head synset has one or more\n" + "satellite synsets.\n" + "\n" + "A head synset contains at least one word that has a direct antonym\n" + "in another head synset of the same cluster.\n" + "\n" + "A satellite synset represents a concept that is similar in meaning to\n" + "the concept represented by its head synset.\n" + "\n" + "Direct antonyms are a pair of words between which there is an\n" + "associative bond built up by co-occurrences.\n" + "\n" + "Direct antonyms are printed in parentheses following the adjective.\n" + "The position of an adjective in relation to the noun may be restricted\n" + "to the prenominal, postnominal or predicative position. Where present\n" + "these restrictions are noted in parentheses.\n" + "\n" + "A pertainym is a relational adjective, usually defined by such phrases\n" + "as \"of or pertaining to\" and that does not have an antonym. It pertains\n" + "to a noun or another pertainym.\n" + "\n" + "Senses contained in head synsets are displayed above the satellites,\n" + "which are indented and preceded by \"=>\". Senses contained in\n" + "satellite synsets are displayed with the head synset below. The head\n" + "synset is preceded by \"=>\".\n" + "\n" + "Pertainym senses display the word or synsets that the search string\n" + "pertains to."); } tracePointers(iw, PointerType.SIMILAR_TO, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); tracePointers(iw, PointerType.PARTICIPLE_OF, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } if (POS.ADVERB == p) { if (needHelp) { System.out.println( "Display synonyms and synsets related to synsets containing\n" + "the search string. If the search string is a pertainym the word\n" + "or synset that it pertains to is displayed.\n" + "\n" + "A pertainym is a relational adverb that is derived from an adjective.\n" + "\n" + "Pertainym senses display the word that the search string is derived from\n" + "and the adjective synset that contains the word. If the adjective synset\n" + "is a satellite synset, its head synset is also displayed."); } tracePointers(iw, PointerType.PERTAINYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } if (POS.NOUN == p || POS.VERB == p) { if (needHelp) { System.out.println( "Recursively display hypernym (superordinate) tree for the search\n" + "string.\n" + "\n" + "Hypernym is the generic term used to designate a whole class of\n" + "specific instances. Y is a hypernym of X if X is a (kind of) Y.\n" + "\n" + "Hypernym synsets are preceded by \"=>\", and are indented from\n" + "the left according to their level in the hierarchy."); } tracePointers(iw, PointerType.HYPERNYM, PointerUtils.INFINITY, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } } //syns if (arg.startsWith("-smem") && 6 == arg.length()) { if (needHelp) { System.out.println("Display all holonyms of the search string.\n" + "\n" + "A holonym is the name of the whole of which the 'meronym' names a part.\n" + "Y is a holonym of X if X is a part of Y.\n" + "\n" + "A meronym is the name of a constituent part, the substance of, or a\n" + "member of something. X is a meronym of Y if X is a part of Y."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nMember Holonyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.MEMBER_HOLONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //smem if (arg.startsWith("-ssub") && 6 == arg.length()) { if (needHelp) { System.out.println("Display all holonyms of the search string.\n" + "\n" + "A holonym is the name of the whole of which the 'meronym' names a part.\n" + "Y is a holonym of X if X is a part of Y.\n" + "\n" + "A meronym is the name of a constituent part, the substance of, or a\n" + "member of something. X is a meronym of Y if X is a part of Y."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nSubstance Holonyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.SUBSTANCE_HOLONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //ssub if (arg.startsWith("-sprt") && 6 == arg.length()) { if (needHelp) { System.out.println("Display all holonyms of the search string.\n" + "\n" + "A holonym is the name of the whole of which the 'meronym' names a part.\n" + "Y is a holonym of X if X is a part of Y.\n" + "\n" + "A meronym is the name of a constituent part, the substance of, or a\n" + "member of something. X is a meronym of Y if X is a part of Y."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nPart Holonyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.PART_HOLONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //sprt if (arg.startsWith("-memb") && 6 == arg.length()) { if (needHelp) { System.out.println("Display all meronyms of the search string. \n" + "\n" + "A meronym is the name of a constituent part, the substance of, or a\n" + "member of something. X is a meronym of Y if X is a part of Y.\n" + "\n" + "A holonym is the name of the whole of which the meronym names a part.\n" + "Y is a holonym of X if X is a part of Y."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nMember Meronyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.MEMBER_MERONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //memb if (arg.startsWith("-subs") && 6 == arg.length()) { if (needHelp) { System.out.println("Display all meronyms of the search string. \n" + "\n" + "A meronym is the name of a constituent part, the substance of, or a\n" + "member of something. X is a meronym of Y if X is a part of Y.\n" + "\n" + "A holonym is the name of the whole of which the meronym names a part.\n" + "Y is a holonym of X if X is a part of Y."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nSubstance Meronyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.SUBSTANCE_MERONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //subs if (arg.startsWith("-part") && 6 == arg.length()) { if (needHelp) { System.out.println("Display all meronyms of the search string. \n" + "\n" + "A meronym is the name of a constituent part, the substance of, or a\n" + "member of something. X is a meronym of Y if X is a part of Y.\n" + "\n" + "A holonym is the name of the whole of which the meronym names a part.\n" + "Y is a holonym of X if X is a part of Y."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nPart Meronyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.PART_MERONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //part if (arg.startsWith("-mero") && 6 == arg.length()) { if (needHelp) { System.out.println("Display all meronyms of the search string. \n" + "\n" + "A meronym is the name of a constituent part, the substance of, or a\n" + "member of something. X is a meronym of Y if X is a part of Y.\n" + "\n" + "A holonym is the name of the whole of which the meronym names a part.\n" + "Y is a holonym of X if X is a part of Y."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nMeronyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.MEMBER_MERONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); tracePointers(iw, PointerType.SUBSTANCE_MERONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); tracePointers(iw, PointerType.PART_MERONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //mero if (arg.startsWith("-holo") && 6 == arg.length()) { if (needHelp) { System.out.println("Display all holonyms of the search string.\n" + "\n" + "A holonym is the name of the whole of which the 'meronym' names a part.\n" + "Y is a holonym of X if X is a part of Y.\n" + "\n" + "A meronym is the name of a constituent part, the substance of, or a\n" + "member of something. X is a meronym of Y if X is a part of Y."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nHolonyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.MEMBER_HOLONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); tracePointers(iw, PointerType.SUBSTANCE_HOLONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); tracePointers(iw, PointerType.PART_HOLONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //holo if (arg.startsWith("-caus") && 6 == arg.length()) { if (needHelp) { System.out.println("Recursively display CAUSE TO relations of the search string.\n" + "\n" + "The action represented by the verb X causes the action represented by\n" + "the verb Y.\n" + "\n" + "CAUSE TO synsets are preceded by \"=>\", and are indented from the left\n" + "according to their level in the hierarchy."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\n'Cause to' of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.CAUSE, PointerUtils.INFINITY, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //caus if (arg.startsWith("-pert") && 6 == arg.length()) { POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nPertainyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.PERTAINYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //pert if (arg.startsWith("-attr") && 6 == arg.length()) { POS p = POS.getPOSForKey(arg.substring(5)); if (needHelp) { if (POS.NOUN == p) { System.out .println("Display adjectives for which search string is an attribute."); } if (POS.ADJECTIVE == p) { System.out.println("Display nouns that are attributes of search string."); } } IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nAttributes of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.ATTRIBUTE, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //attr if (arg.startsWith("-deri") && 6 == arg.length()) { if (needHelp) { System.out.println( "Display derived forms - nouns and verbs that are related morphologically.\n" + "Each related synset is preceeded by its part of speech. Each word in the\n" + "synset is followed by its sense number."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nDerived forms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.NOMINALIZATION, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //deri if (arg.startsWith("-domn") && 6 == arg.length()) { if (needHelp) { System.out.println("Display domain to which this synset belongs."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nDomain of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.CATEGORY, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); tracePointers(iw, PointerType.USAGE, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); tracePointers(iw, PointerType.REGION, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //domn if (arg.startsWith("-domt") && 6 == arg.length()) { if (needHelp) { System.out.println("Display all synsets belonging to the domain."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nDomain of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.CATEGORY_MEMBER, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); tracePointers(iw, PointerType.USAGE_MEMBER, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); tracePointers(iw, PointerType.REGION_MEMBER, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //domt if (arg.startsWith("-faml") && 6 == arg.length()) { if (needHelp) { System.out.println( "Display familiarity and polysemy information for the search string.\n" + "The polysemy count is the number of senses in WordNet."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { String[] freqs = { "extremely rare", "very rare", "rare", "uncommon", "common", "familiar", "very familiar", "extremely familiar" }; String[] pos = { "a noun", "a verb", "an adjective", "an adverb" }; int cnt = iw.getSenses().size(); int familiar = 0; if (cnt == 0) { familiar = 0; } if (cnt == 1) { familiar = 1; } if (cnt == 2) { familiar = 2; } if (cnt >= 3 && cnt <= 4) { familiar = 3; } if (cnt >= 5 && cnt <= 8) { familiar = 4; } if (cnt >= 9 && cnt <= 16) { familiar = 5; } if (cnt >= 17 && cnt <= 32) { familiar = 6; } if (cnt > 32) { familiar = 7; } System.out.println("\n" + iw.getLemma() + " used as " + pos[p.getId() - 1] + " is " + freqs[familiar] + " (polysemy count = " + cnt + ")"); } } //faml if (arg.startsWith("-fram") && 6 == arg.length()) { if (needHelp) { System.out.println( "Display applicable verb sentence frames for the search string.\n" + "\n" + "A frame is a sentence template illustrating the usage of a verb.\n" + "\n" + "Verb sentence frames are preceded with the string \"*>\" if a sentence\n" + "frame is acceptable for all of the words in the synset, and with \"=>\"\n" + "if a sentence frame is acceptable for the search string only."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nVerb frames of " + p.getLabel() + " " + iw.getLemma()); for (int i = 0; i < iw.getSenses().size(); i++) { Synset synset = iw.getSenses().get(i); for (String vf : synset.getVerbFrames()) { System.out.println("\t*> " + vf); } for (Word word : synset.getWords()) { if (iw.getLemma().equalsIgnoreCase(word.getLemma())) { if (word instanceof Verb) { Verb verb = (Verb) word; for (String vf : verb.getVerbFrames()) { System.out.println("\t=> " + vf); } } } } } } } //fram if (arg.startsWith("-hmer") && 6 == arg.length()) { if (needHelp) { System.out.println( "Display meronyms for search string tree. This is a recursive search\n" + "the prints all the meronyms of the search string and all of its\n" + "hypernyms. \n" + "\n" + "A meronym is the name of a constituent part, the substance of, or a\n" + "member of something. X is a meronym of Y if X is a part of Y."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nMeronyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.MEMBER_MERONYM, PointerUtils.INFINITY, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); tracePointers(iw, PointerType.SUBSTANCE_MERONYM, PointerUtils.INFINITY, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); tracePointers(iw, PointerType.PART_MERONYM, PointerUtils.INFINITY, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //hmer if (arg.startsWith("-hhol") && 6 == arg.length()) { if (needHelp) { System.out.println( "\"Display holonyms for search string tree. This is a recursive search\n" + "that prints all the holonyms of the search string and all of the\n" + "holonym's holonyms.\n" + "\n" + "A holonym is the name of the whole of which the meronym names a part.\n" + "Y is a holonym of X if X is a part of Y."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nHolonyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.MEMBER_HOLONYM, PointerUtils.INFINITY, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); tracePointers(iw, PointerType.SUBSTANCE_HOLONYM, PointerUtils.INFINITY, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); tracePointers(iw, PointerType.PART_HOLONYM, PointerUtils.INFINITY, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //hhol if (arg.startsWith("-mero") && 6 == arg.length()) { if (needHelp) { System.out.println("Display all meronyms of the search string. \n" + "\n" + "A meronym is the name of a constituent part, the substance of, or a\n" + "member of something. X is a meronym of Y if X is a part of Y.\n" + "\n" + "A holonym is the name of the whole of which the meronym names a part.\n" + "Y is a holonym of X if X is a part of Y."); } POS p = POS.getPOSForKey(arg.substring(5)); IndexWord iw = d.lookupIndexWord(p, key); if (null != iw) { System.out.println("\nMeronyms of " + p.getLabel() + " " + iw.getLemma()); tracePointers(iw, PointerType.MEMBER_MERONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); tracePointers(iw, PointerType.SUBSTANCE_MERONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); tracePointers(iw, PointerType.PART_MERONYM, 1, needSense, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } //mero if (arg.startsWith("-grep") && 6 == arg.length()) { if (needHelp) { System.out .println("Print all strings in the database containing the search string\n" + "as an individual word, or as the first or last string in a word or\n" + "collocation."); } POS p = POS.getPOSForKey(arg.substring(5)); System.out.println("\nGrep of " + p.getLabel() + " " + key); Iterator<IndexWord> ii = d.getIndexWordIterator(p, key); while (ii.hasNext()) { System.out.println(ii.next().getLemma()); } } //grep if ("-over".equals(arg)) { for (POS pos : POS.getAllPOS()) { if (null != d.getMorphologicalProcessor()) { IndexWord iw = d.getIndexWord(pos, key); //for plurals like species, glasses if (null != iw && key.equals(iw.getLemma())) { printOverview(pos, iw, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } List<String> forms = d.getMorphologicalProcessor().lookupAllBaseForms(pos, key); if (null != forms) { for (String form : forms) { if (!form.equals(key)) { iw = d.getIndexWord(pos, form); if (null != iw) { printOverview(pos, iw, needGloss, needLex, needOffset, needSenseNum, needSenseKeys); } } } } } } } //over } } } } }
From source file:edu.nyu.vida.data_polygamy.feature_identification.IndexCreation.java
/** * @param args//from w w w .j a v a 2 s. co m */ @SuppressWarnings({ "deprecation" }) public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException { Options options = new Options(); Option forceOption = new Option("f", "force", false, "force the computation of the index and events " + "even if files already exist"); forceOption.setRequired(false); options.addOption(forceOption); Option thresholdOption = new Option("t", "use-custom-thresholds", false, "use custom thresholds for regular and rare events, defined in HDFS_HOME/" + FrameworkUtils.thresholdDir + " file"); thresholdOption.setRequired(false); options.addOption(thresholdOption); Option gOption = new Option("g", "group", true, "set group of datasets for which the indices and events" + " will be computed"); gOption.setRequired(true); gOption.setArgName("GROUP"); gOption.setArgs(Option.UNLIMITED_VALUES); options.addOption(gOption); Option machineOption = new Option("m", "machine", true, "machine identifier"); machineOption.setRequired(true); machineOption.setArgName("MACHINE"); machineOption.setArgs(1); options.addOption(machineOption); Option nodesOption = new Option("n", "nodes", true, "number of nodes"); nodesOption.setRequired(true); nodesOption.setArgName("NODES"); nodesOption.setArgs(1); options.addOption(nodesOption); Option s3Option = new Option("s3", "s3", false, "data on Amazon S3"); s3Option.setRequired(false); options.addOption(s3Option); Option awsAccessKeyIdOption = new Option("aws_id", "aws-id", true, "aws access key id; " + "this is required if the execution is on aws"); awsAccessKeyIdOption.setRequired(false); awsAccessKeyIdOption.setArgName("AWS-ACCESS-KEY-ID"); awsAccessKeyIdOption.setArgs(1); options.addOption(awsAccessKeyIdOption); Option awsSecretAccessKeyOption = new Option("aws_key", "aws-id", true, "aws secrect access key; " + "this is required if the execution is on aws"); awsSecretAccessKeyOption.setRequired(false); awsSecretAccessKeyOption.setArgName("AWS-SECRET-ACCESS-KEY"); awsSecretAccessKeyOption.setArgs(1); options.addOption(awsSecretAccessKeyOption); Option bucketOption = new Option("b", "s3-bucket", true, "bucket on s3; " + "this is required if the execution is on aws"); bucketOption.setRequired(false); bucketOption.setArgName("S3-BUCKET"); bucketOption.setArgs(1); options.addOption(bucketOption); Option helpOption = new Option("h", "help", false, "display this message"); helpOption.setRequired(false); options.addOption(helpOption); HelpFormatter formatter = new HelpFormatter(); CommandLineParser parser = new PosixParser(); CommandLine cmd = null; try { cmd = parser.parse(options, args); } catch (ParseException e) { formatter.printHelp("hadoop jar data-polygamy.jar " + "edu.nyu.vida.data_polygamy.feature_identification.IndexCreation", options, true); System.exit(0); } if (cmd.hasOption("h")) { formatter.printHelp("hadoop jar data-polygamy.jar " + "edu.nyu.vida.data_polygamy.feature_identification.IndexCreation", options, true); System.exit(0); } boolean s3 = cmd.hasOption("s3"); String s3bucket = ""; String awsAccessKeyId = ""; String awsSecretAccessKey = ""; if (s3) { if ((!cmd.hasOption("aws_id")) || (!cmd.hasOption("aws_key")) || (!cmd.hasOption("b"))) { System.out.println( "Arguments 'aws_id', 'aws_key', and 'b'" + " are mandatory if execution is on AWS."); formatter.printHelp("hadoop jar data-polygamy.jar " + "edu.nyu.vida.data_polygamy.feature_identification.IndexCreation", options, true); System.exit(0); } s3bucket = cmd.getOptionValue("b"); awsAccessKeyId = cmd.getOptionValue("aws_id"); awsSecretAccessKey = cmd.getOptionValue("aws_key"); } boolean snappyCompression = false; boolean bzip2Compression = false; String machine = cmd.getOptionValue("m"); int nbNodes = Integer.parseInt(cmd.getOptionValue("n")); Configuration s3conf = new Configuration(); if (s3) { s3conf.set("fs.s3.awsAccessKeyId", awsAccessKeyId); s3conf.set("fs.s3.awsSecretAccessKey", awsSecretAccessKey); s3conf.set("bucket", s3bucket); } String datasetNames = ""; String datasetIds = ""; ArrayList<String> shortDataset = new ArrayList<String>(); ArrayList<String> shortDatasetIndex = new ArrayList<String>(); HashMap<String, String> datasetAgg = new HashMap<String, String>(); HashMap<String, String> datasetId = new HashMap<String, String>(); HashMap<String, HashMap<Integer, Double>> datasetRegThreshold = new HashMap<String, HashMap<Integer, Double>>(); HashMap<String, HashMap<Integer, Double>> datasetRareThreshold = new HashMap<String, HashMap<Integer, Double>>(); Path path = null; FileSystem fs = FileSystem.get(new Configuration()); BufferedReader br; boolean removeExistingFiles = cmd.hasOption("f"); boolean isThresholdUserDefined = cmd.hasOption("t"); for (String dataset : cmd.getOptionValues("g")) { // getting aggregates String[] aggregate = FrameworkUtils.searchAggregates(dataset, s3conf, s3); if (aggregate.length == 0) { System.out.println("No aggregates found for " + dataset + "."); continue; } // getting aggregates header String aggregatesHeaderFileName = FrameworkUtils.searchAggregatesHeader(dataset, s3conf, s3); if (aggregatesHeaderFileName == null) { System.out.println("No aggregate header for " + dataset); continue; } String aggregatesHeader = s3bucket + FrameworkUtils.preProcessingDir + "/" + aggregatesHeaderFileName; shortDataset.add(dataset); datasetId.put(dataset, null); if (s3) { path = new Path(aggregatesHeader); fs = FileSystem.get(path.toUri(), s3conf); } else { path = new Path(fs.getHomeDirectory() + "/" + aggregatesHeader); } br = new BufferedReader(new InputStreamReader(fs.open(path))); datasetAgg.put(dataset, br.readLine().split("\t")[1]); br.close(); if (s3) fs.close(); } if (shortDataset.size() == 0) { System.out.println("No datasets to process."); System.exit(0); } // getting dataset id if (s3) { path = new Path(s3bucket + FrameworkUtils.datasetsIndexDir); fs = FileSystem.get(path.toUri(), s3conf); } else { path = new Path(fs.getHomeDirectory() + "/" + FrameworkUtils.datasetsIndexDir); } br = new BufferedReader(new InputStreamReader(fs.open(path))); String line = br.readLine(); while (line != null) { String[] dt = line.split("\t"); if (datasetId.containsKey(dt[0])) { datasetId.put(dt[0], dt[1]); datasetNames += dt[0] + ","; datasetIds += dt[1] + ","; } line = br.readLine(); } br.close(); datasetNames = datasetNames.substring(0, datasetNames.length() - 1); datasetIds = datasetIds.substring(0, datasetIds.length() - 1); Iterator<String> it = shortDataset.iterator(); while (it.hasNext()) { String dataset = it.next(); if (datasetId.get(dataset) == null) { System.out.println("No dataset id for " + dataset); System.exit(0); } } // getting user defined thresholds if (isThresholdUserDefined) { if (s3) { path = new Path(s3bucket + FrameworkUtils.thresholdDir); fs = FileSystem.get(path.toUri(), s3conf); } else { path = new Path(fs.getHomeDirectory() + "/" + FrameworkUtils.thresholdDir); } br = new BufferedReader(new InputStreamReader(fs.open(path))); line = br.readLine(); while (line != null) { // getting dataset name String dataset = line.trim(); HashMap<Integer, Double> regThresholds = new HashMap<Integer, Double>(); HashMap<Integer, Double> rareThresholds = new HashMap<Integer, Double>(); line = br.readLine(); while ((line != null) && (line.split("\t").length > 1)) { // getting attribute ids and thresholds String[] keyVals = line.trim().split("\t"); int att = Integer.parseInt(keyVals[0].trim()); regThresholds.put(att, Double.parseDouble(keyVals[1].trim())); rareThresholds.put(att, Double.parseDouble(keyVals[2].trim())); line = br.readLine(); } datasetRegThreshold.put(dataset, regThresholds); datasetRareThreshold.put(dataset, rareThresholds); } br.close(); } if (s3) fs.close(); // datasets that will use existing merge tree ArrayList<String> useMergeTree = new ArrayList<String>(); // creating index for each spatio-temporal resolution FrameworkUtils.createDir(s3bucket + FrameworkUtils.indexDir, s3conf, s3); HashSet<String> input = new HashSet<String>(); for (String dataset : shortDataset) { String indexCreationOutputFileName = s3bucket + FrameworkUtils.indexDir + "/" + dataset + "/"; String mergeTreeFileName = s3bucket + FrameworkUtils.mergeTreeDir + "/" + dataset + "/"; if (removeExistingFiles) { FrameworkUtils.removeFile(indexCreationOutputFileName, s3conf, s3); FrameworkUtils.removeFile(mergeTreeFileName, s3conf, s3); FrameworkUtils.createDir(mergeTreeFileName, s3conf, s3); } else if (datasetRegThreshold.containsKey(dataset)) { FrameworkUtils.removeFile(indexCreationOutputFileName, s3conf, s3); if (FrameworkUtils.fileExists(mergeTreeFileName, s3conf, s3)) { useMergeTree.add(dataset); } } if (!FrameworkUtils.fileExists(indexCreationOutputFileName, s3conf, s3)) { input.add(s3bucket + FrameworkUtils.aggregatesDir + "/" + dataset); shortDatasetIndex.add(dataset); } } if (input.isEmpty()) { System.out.println("All the input datasets have indices."); System.out.println("Use -f in the beginning of the command line to force the computation."); System.exit(0); } String aggregateDatasets = ""; it = input.iterator(); while (it.hasNext()) { aggregateDatasets += it.next() + ","; } Job icJob = null; Configuration icConf = new Configuration(); Machine machineConf = new Machine(machine, nbNodes); String jobName = "index"; String indexOutputDir = s3bucket + FrameworkUtils.indexDir + "/tmp/"; FrameworkUtils.removeFile(indexOutputDir, s3conf, s3); icConf.set("dataset-name", datasetNames); icConf.set("dataset-id", datasetIds); if (!useMergeTree.isEmpty()) { String useMergeTreeStr = ""; for (String dt : useMergeTree) { useMergeTreeStr += dt + ","; } icConf.set("use-merge-tree", useMergeTreeStr.substring(0, useMergeTreeStr.length() - 1)); } for (int i = 0; i < shortDataset.size(); i++) { String dataset = shortDataset.get(i); String id = datasetId.get(dataset); icConf.set("dataset-" + id + "-aggregates", datasetAgg.get(dataset)); if (datasetRegThreshold.containsKey(dataset)) { HashMap<Integer, Double> regThresholds = datasetRegThreshold.get(dataset); String thresholds = ""; for (int att : regThresholds.keySet()) { thresholds += String.valueOf(att) + "-" + String.valueOf(regThresholds.get(att)) + ","; } icConf.set("regular-" + id, thresholds.substring(0, thresholds.length() - 1)); } if (datasetRareThreshold.containsKey(dataset)) { HashMap<Integer, Double> rareThresholds = datasetRareThreshold.get(dataset); String thresholds = ""; for (int att : rareThresholds.keySet()) { thresholds += String.valueOf(att) + "-" + String.valueOf(rareThresholds.get(att)) + ","; } icConf.set("rare-" + id, thresholds.substring(0, thresholds.length() - 1)); } } icConf.set("mapreduce.tasktracker.map.tasks.maximum", String.valueOf(machineConf.getMaximumTasks())); icConf.set("mapreduce.tasktracker.reduce.tasks.maximum", String.valueOf(machineConf.getMaximumTasks())); icConf.set("mapreduce.jobtracker.maxtasks.perjob", "-1"); icConf.set("mapreduce.reduce.shuffle.parallelcopies", "20"); icConf.set("mapreduce.input.fileinputformat.split.minsize", "0"); icConf.set("mapreduce.task.io.sort.mb", "200"); icConf.set("mapreduce.task.io.sort.factor", "100"); //icConf.set("mapreduce.task.timeout", "1800000"); machineConf.setMachineConfiguration(icConf); if (s3) { machineConf.setMachineConfiguration(icConf); icConf.set("fs.s3.awsAccessKeyId", awsAccessKeyId); icConf.set("fs.s3.awsSecretAccessKey", awsSecretAccessKey); icConf.set("bucket", s3bucket); } if (snappyCompression) { icConf.set("mapreduce.map.output.compress", "true"); icConf.set("mapreduce.map.output.compress.codec", "org.apache.hadoop.io.compress.SnappyCodec"); //icConf.set("mapreduce.output.fileoutputformat.compress.codec", "org.apache.hadoop.io.compress.SnappyCodec"); } if (bzip2Compression) { icConf.set("mapreduce.map.output.compress", "true"); icConf.set("mapreduce.map.output.compress.codec", "org.apache.hadoop.io.compress.BZip2Codec"); //icConf.set("mapreduce.output.fileoutputformat.compress.codec", "org.apache.hadoop.io.compress.BZip2Codec"); } icJob = new Job(icConf); icJob.setJobName(jobName); icJob.setMapOutputKeyClass(AttributeResolutionWritable.class); icJob.setMapOutputValueClass(SpatioTemporalFloatWritable.class); icJob.setOutputKeyClass(AttributeResolutionWritable.class); icJob.setOutputValueClass(TopologyTimeSeriesWritable.class); //icJob.setOutputKeyClass(Text.class); //icJob.setOutputValueClass(Text.class); icJob.setMapperClass(IndexCreationMapper.class); icJob.setReducerClass(IndexCreationReducer.class); icJob.setNumReduceTasks(machineConf.getNumberReduces()); icJob.setInputFormatClass(SequenceFileInputFormat.class); //icJob.setOutputFormatClass(SequenceFileOutputFormat.class); LazyOutputFormat.setOutputFormatClass(icJob, SequenceFileOutputFormat.class); //LazyOutputFormat.setOutputFormatClass(icJob, TextOutputFormat.class); SequenceFileOutputFormat.setCompressOutput(icJob, true); SequenceFileOutputFormat.setOutputCompressionType(icJob, CompressionType.BLOCK); FileInputFormat.setInputDirRecursive(icJob, true); FileInputFormat.setInputPaths(icJob, aggregateDatasets.substring(0, aggregateDatasets.length() - 1)); FileOutputFormat.setOutputPath(icJob, new Path(indexOutputDir)); icJob.setJarByClass(IndexCreation.class); long start = System.currentTimeMillis(); icJob.submit(); icJob.waitForCompletion(true); System.out.println(jobName + "\t" + (System.currentTimeMillis() - start)); // moving files to right place for (String dataset : shortDatasetIndex) { String from = s3bucket + FrameworkUtils.indexDir + "/tmp/" + dataset + "/"; String to = s3bucket + FrameworkUtils.indexDir + "/" + dataset + "/"; FrameworkUtils.renameFile(from, to, s3conf, s3); } }
From source file:au.com.jwatmuff.eventmanager.Main.java
/** * Main method./* w w w. j a v a 2 s .c om*/ */ public static void main(String args[]) { LogUtils.setupUncaughtExceptionHandler(); /* Set timeout for RMI connections - TODO: move to external file */ System.setProperty("sun.rmi.transport.tcp.handshakeTimeout", "2000"); updateRmiHostName(); /* * Set up menu bar for Mac */ System.setProperty("apple.laf.useScreenMenuBar", "true"); System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Event Manager"); /* * Set look and feel to 'system' style */ try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { log.info("Failed to set system look and feel"); } /* * Set workingDir to a writable folder for storing competitions, settings etc. */ String applicationData = System.getenv("APPDATA"); if (applicationData != null) { workingDir = new File(applicationData, "EventManager"); if (workingDir.exists() || workingDir.mkdirs()) { // redirect logging to writable folder LogUtils.reconfigureFileAppenders("log4j.properties", workingDir); } else { workingDir = new File("."); } } // log version for debugging log.info("Running version: " + VISIBLE_VERSION + " (Internal: " + VERSION + ")"); /* * Copy license if necessary */ File license1 = new File("license.lic"); File license2 = new File(workingDir, "license.lic"); if (license1.exists() && !license2.exists()) { try { FileUtils.copyFile(license1, license2); } catch (IOException e) { log.warn("Failed to copy license from " + license1 + " to " + license2, e); } } if (license1.exists() && license2.exists()) { if (license1.lastModified() > license2.lastModified()) { try { FileUtils.copyFile(license1, license2); } catch (IOException e) { log.warn("Failed to copy license from " + license1 + " to " + license2, e); } } } /* * Check if run lock exists, if so ask user if it is ok to continue */ if (!obtainRunLock(false)) { int response = JOptionPane.showConfirmDialog(null, "Unable to obtain run-lock.\nPlease ensure that no other instances of EventManager are running before continuing.\nDo you wish to continue?", "Run-lock detected", JOptionPane.YES_NO_OPTION); if (response == JOptionPane.YES_OPTION) obtainRunLock(true); else System.exit(0); } try { LoadWindow loadWindow = new LoadWindow(); loadWindow.setVisible(true); loadWindow.addMessage("Reading settings.."); /* * Read properties from file */ final Properties props = new Properties(); try { Properties defaultProps = PropertiesLoaderUtils .loadProperties(new ClassPathResource("eventmanager.properties")); props.putAll(defaultProps); } catch (IOException ex) { log.error(ex); } props.putAll(System.getProperties()); File databaseStore = new File(workingDir, "comps"); int rmiPort = Integer.parseInt(props.getProperty("eventmanager.rmi.port")); loadWindow.addMessage("Loading Peer Manager.."); log.info("Loading Peer Manager"); ManualDiscoveryService manualDiscoveryService = new ManualDiscoveryService(); JmDNSRMIPeerManager peerManager = new JmDNSRMIPeerManager(rmiPort, new File(workingDir, "peerid.dat")); peerManager.addDiscoveryService(manualDiscoveryService); monitorNetworkInterfaceChanges(peerManager); loadWindow.addMessage("Loading Database Manager.."); log.info("Loading Database Manager"); DatabaseManager databaseManager = new SQLiteDatabaseManager(databaseStore, peerManager); LicenseManager licenseManager = new LicenseManager(workingDir); loadWindow.addMessage("Loading Load Competition Dialog.."); log.info("Loading Load Competition Dialog"); LoadCompetitionWindow loadCompetitionWindow = new LoadCompetitionWindow(databaseManager, licenseManager, peerManager); loadCompetitionWindow.setTitle(WINDOW_TITLE); loadWindow.dispose(); log.info("Starting Load Competition Dialog"); while (true) { // reset permission checker to use our license licenseManager.updatePermissionChecker(); GUIUtils.runModalJFrame(loadCompetitionWindow); if (loadCompetitionWindow.getSuccess()) { DatabaseInfo info = loadCompetitionWindow.getSelectedDatabaseInfo(); if (!databaseManager.checkLock(info.id)) { String message = "EventManager did not shut down correctly the last time this competition was open. To avoid potential data corruption, you are advised to take the following steps:\n" + "1) Do NOT open this competition\n" + "2) Create a backup of this competition\n" + "3) Delete the competition from this computer\n" + "4) If possible, reload this competition from another computer on the network\n" + "5) Alternatively, you may manually load the backup onto all computers on the network, ensuring the 'Preserve competition ID' option is checked."; String title = "WARNING: Potential Data Corruption Detected"; int status = JOptionPane.showOptionDialog(null, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, new Object[] { "Cancel (recommended)", "Open anyway" }, "Cancel (recommended)"); if (status == 0) continue; // return to load competition window } SynchronizingWindow syncWindow = new SynchronizingWindow(); syncWindow.setVisible(true); long t = System.nanoTime(); DistributedDatabase database = databaseManager.activateDatabase(info.id, info.passwordHash); long dt = System.nanoTime() - t; log.debug(String.format("Initial sync in %dms", TimeUnit.MILLISECONDS.convert(dt, TimeUnit.NANOSECONDS))); syncWindow.dispose(); if (loadCompetitionWindow.isNewDatabase()) { GregorianCalendar calendar = new GregorianCalendar(); Date today = calendar.getTime(); calendar.set(Calendar.MONTH, Calendar.DECEMBER); calendar.set(Calendar.DAY_OF_MONTH, 31); Date endOfYear = new java.sql.Date(calendar.getTimeInMillis()); CompetitionInfo ci = new CompetitionInfo(); ci.setName(info.name); ci.setStartDate(today); ci.setEndDate(today); ci.setAgeThresholdDate(endOfYear); //ci.setPasswordHash(info.passwordHash); License license = licenseManager.getLicense(); if (license != null) { ci.setLicenseName(license.getName()); ci.setLicenseType(license.getType().toString()); ci.setLicenseContact(license.getContactPhoneNumber()); } database.add(ci); } // Set PermissionChecker to use database's license type String competitionLicenseType = database.get(CompetitionInfo.class, null).getLicenseType(); PermissionChecker.setLicenseType(LicenseType.valueOf(competitionLicenseType)); TransactionNotifier notifier = new TransactionNotifier(); database.setListener(notifier); MainWindow mainWindow = new MainWindow(); mainWindow.setDatabase(database); mainWindow.setNotifier(notifier); mainWindow.setPeerManager(peerManager); mainWindow.setLicenseManager(licenseManager); mainWindow.setManualDiscoveryService(manualDiscoveryService); mainWindow.setTitle(WINDOW_TITLE); mainWindow.afterPropertiesSet(); TestUtil.setActivatedDatabase(database); // show main window (modally) GUIUtils.runModalJFrame(mainWindow); // shutdown procedures // System.exit(); database.shutdown(); databaseManager.deactivateDatabase(1500); if (mainWindow.getDeleteOnExit()) { for (File file : info.localDirectory.listFiles()) if (!file.isDirectory()) file.delete(); info.localDirectory.deleteOnExit(); } } else { // This can cause an RuntimeException - Peer is disconnected peerManager.stop(); System.exit(0); } } } catch (Throwable e) { log.error("Error in main function", e); String message = e.getMessage(); if (message == null) message = ""; if (message.length() > 100) message = message.substring(0, 97) + "..."; GUIUtils.displayError(null, "An unexpected error has occured.\n\n" + e.getClass().getSimpleName() + "\n" + message); System.exit(0); } }