Example usage for java.lang String substring

List of usage examples for java.lang String substring

Introduction

In this page you can find the example usage for java.lang String substring.

Prototype

public String substring(int beginIndex, int endIndex) 

Source Link

Document

Returns a string that is a substring of this string.

Usage

From source file:de.mpg.escidoc.services.edoc.BatchUpdate.java

/**
 * @param args/*ww  w  .j  a  va 2  s  . c o m*/
 */
public static void main(String[] args) throws Exception {
    CORESERVICES_URL = PropertyReader.getProperty("escidoc.framework_access.framework.url");

    String userHandle = AdminHelper.loginUser("import_user", "");

    logger.info("Querying core-services...");
    HttpClient httpClient = new HttpClient();
    String filter = "<param><filter name=\"http://escidoc.de/core/01/structural-relations/context\">"
            + IMPORT_CONTEXT
            + "</filter><order-by>http://escidoc.de/core/01/properties/creation-date</order-by><limit>0</limit></param>";

    logger.info("Filter: " + filter);

    PostMethod postMethod = new PostMethod(CORESERVICES_URL + "/ir/items/filter");

    postMethod.setRequestBody(filter);

    ProxyHelper.executeMethod(httpClient, postMethod);

    //        GetMethod getMethod = new GetMethod(CORESERVICES_URL + "/ir/item/escidoc:100220");
    //        getMethod.setRequestHeader("Cookie", "escidocCookie=" + userHandle)ProxyHelper.executeMethod(httpClient, getMethod)hod(httpClient, getMethod);

    String response = postMethod.getResponseBodyAsString();
    logger.info("...done!");

    System.out.println(response);

    while (response.contains("<escidocItem:item")) {

        int startPos = response.indexOf("<escidocItem:item");
        int endPos = response.indexOf("</escidocItem:item>");

        String item = response.substring(startPos, endPos + 19);

        response = response.substring(endPos + 19);

        startPos = item.indexOf("xlink:href=\"");
        endPos = item.indexOf("\"", startPos + 12);

        String objId = item.substring(startPos + 12, endPos);

        System.out.print(objId);

        if (item.contains("escidoc:22019")) {
            item = item.replaceAll("escidoc:22019", "escidoc:55222");

            PutMethod putMethod = new PutMethod(CORESERVICES_URL + objId);

            putMethod.setRequestHeader("Cookie", "escidocCookie=" + userHandle);
            putMethod.setRequestEntity(new StringRequestEntity(item));
            ProxyHelper.executeMethod(httpClient, putMethod);

            String result = putMethod.getResponseBodyAsString();

            //System.out.println(item);

            startPos = result.indexOf("last-modification-date=\"");
            endPos = result.indexOf("\"", startPos + 24);
            String modDate = result.substring(startPos + 24, endPos);
            //System.out.println("modDate: " + modDate);
            String param = "<param last-modification-date=\"" + modDate
                    + "\"><url>http://pubman.mpdl.mpg.de/pubman/item/" + objId.substring(4) + "</url></param>";
            postMethod = new PostMethod(CORESERVICES_URL + objId + "/assign-version-pid");
            postMethod.setRequestHeader("Cookie", "escidocCookie=" + userHandle);
            postMethod.setRequestEntity(new StringRequestEntity(param));
            ProxyHelper.executeMethod(httpClient, postMethod);
            result = postMethod.getResponseBodyAsString();
            //System.out.println("Result: " + result);

            startPos = result.indexOf("last-modification-date=\"");
            endPos = result.indexOf("\"", startPos + 24);
            modDate = result.substring(startPos + 24, endPos);
            //System.out.println("modDate: " + modDate);
            param = "<param last-modification-date=\"" + modDate
                    + "\"><comment>Batch repair of organizational unit identifier</comment></param>";
            postMethod = new PostMethod(CORESERVICES_URL + objId + "/submit");
            postMethod.setRequestHeader("Cookie", "escidocCookie=" + userHandle);
            postMethod.setRequestEntity(new StringRequestEntity(param));
            ProxyHelper.executeMethod(httpClient, postMethod);
            result = postMethod.getResponseBodyAsString();
            //System.out.println("Result: " + result);

            startPos = result.indexOf("last-modification-date=\"");
            endPos = result.indexOf("\"", startPos + 24);
            modDate = result.substring(startPos + 24, endPos);
            //System.out.println("modDate: " + modDate);
            param = "<param last-modification-date=\"" + modDate
                    + "\"><comment>Batch repair of organizational unit identifier</comment></param>";
            postMethod = new PostMethod(CORESERVICES_URL + objId + "/release");
            postMethod.setRequestHeader("Cookie", "escidocCookie=" + userHandle);
            postMethod.setRequestEntity(new StringRequestEntity(param));
            ProxyHelper.executeMethod(httpClient, postMethod);
            result = postMethod.getResponseBodyAsString();
            //System.out.println("Result: " + result);
            System.out.println("...changed");
        } else {
            System.out.println("...not affected");
        }
    }

}

From source file:de.dfki.dmas.owls2wsdl.core.OWLS2WSDL.java

public static void main(String[] args) {
    // http://jakarta.apache.org/commons/cli/usage.html
    System.out.println("ARG COUNT: " + args.length);

    OWLS2WSDLSettings.getInstance();/*from w w  w  .  ja v a  2 s  . c  o m*/

    Options options = new Options();
    Option help = new Option("help", "print help message");
    options.addOption(help);

    // create the parser
    CommandLineParser parser = new GnuParser();
    CommandLine cmdLine = null;

    for (int i = 0; i < args.length; i++) {
        System.out.println("ARG: " + args[i].toString());
    }

    if (args.length > 0) {
        if (args[args.length - 1].toString().endsWith(".owl")) {
            // -kbdir d:\tmp\KB http://127.0.0.1/ontology/ActorDefault.owl
            Option test = new Option("test", "parse only, don't save");
            @SuppressWarnings("static-access")
            Option kbdir = OptionBuilder.withArgName("dir").hasArg()
                    .withDescription("knowledgebase directory; necessary").create("kbdir");
            options.addOption(test);
            options.addOption(kbdir);

            try {
                cmdLine = parser.parse(options, args);
                if (cmdLine.hasOption("help")) {
                    HelpFormatter formatter = new HelpFormatter();
                    formatter.printHelp("owls2wsdl [options] FILE.owl", options);
                    System.exit(0);
                }
            } catch (ParseException exp) {
                // oops, something went wrong
                System.out.println("Error: Parsing failed, reason: " + exp.getMessage());
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp("owls2wsdl", options, true);
            }

            if (args.length == 1) {
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp("owls2wsdl [options] FILE.owl", options);
                System.out.println("Error: Option -kbdir is missing.");
                System.exit(0);
            } else {
                String ontURI = args[args.length - 1].toString();
                String persistentName = "KB_"
                        + ontURI.substring(ontURI.lastIndexOf("/") + 1, ontURI.lastIndexOf(".")) + "-MAP.xml";

                try {
                    if (cmdLine.hasOption("kbdir")) {
                        String kbdirValue = cmdLine.getOptionValue("kbdir");
                        if (new File(kbdirValue).isDirectory()) {
                            DatatypeParser p = new DatatypeParser();
                            p.parse(args[args.length - 1].toString());
                            p.getAbstractDatatypeKBData();
                            if (!cmdLine.hasOption("test")) {
                                System.out.println("AUSGABE: " + kbdirValue + File.separator + persistentName);
                                FileOutputStream ausgabeStream = new FileOutputStream(
                                        kbdirValue + File.separator + persistentName);
                                AbstractDatatypeKB.getInstance().marshallAsXML(ausgabeStream, true); // System.out);
                            }
                        }
                    }
                } catch (java.net.MalformedURLException murle) {
                    System.err.println("MalformedURLException: " + murle.getMessage());
                    System.err.println("Try something like: http://127.0.0.1/ontology/my_ontology.owl");
                } catch (Exception e) {
                    System.err.println("Exception: " + e.toString());
                }
            }
        } else if (args[args.length - 1].toString().endsWith(".xml")) {
            // -owlclass http://127.0.0.1/ontology/Student.owl#HTWStudent
            // -xsd -d 1 -h D:\tmp\KB\KB_Student-MAP.xml
            Option xsd = new Option("xsd", "generate XML Schema");
            Option info = new Option("info", "print datatype information");
            @SuppressWarnings("static-access")
            Option owlclass = OptionBuilder.withArgName("class").hasArg()
                    .withDescription("owl class to translate; necessary").create("owlclass");
            Option keys = new Option("keys", "list all owlclass keys");
            options.addOption(keys);
            options.addOption(owlclass);
            options.addOption(info);
            options.addOption(xsd);
            options.addOption("h", "hierarchy", false, "use hierarchy pattern");
            options.addOption("d", "depth", true, "set recursion depth");
            options.addOption("b", "behavior", true, "set inheritance bevavior");
            options.addOption("p", "primitive", true, "set default primitive type");

            try {
                cmdLine = parser.parse(options, args);
                if (cmdLine.hasOption("help")) {
                    HelpFormatter formatter = new HelpFormatter();
                    formatter.printHelp("owls2wsdl [options] FILE.xml", options);
                    System.exit(0);
                }
            } catch (ParseException exp) {
                // oops, something went wrong
                System.out.println("Error: Parsing failed, reason: " + exp.getMessage());
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp("owls2wsdl", options, true);
            }

            if (args.length == 1) {
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp("owls2wsdl [options] FILE.xml", options);
                System.exit(0);
            } else if (cmdLine.hasOption("keys")) {
                File f = new File(args[args.length - 1].toString());
                try {
                    AbstractDatatypeMapper.getInstance().loadAbstractDatatypeKB(f);
                } catch (Exception e) {
                    System.err.println("Error: " + e.getMessage());
                    System.exit(1);
                }
                AbstractDatatypeKB.getInstance().getAbstractDatatypeKBData().printRegisteredDatatypes();
                System.exit(0);
            } else if (!cmdLine.hasOption("owlclass")) {
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp("owls2wsdl [options] FILE.xml", "", options,
                        "Info: owl class not set.\n e.g. -owlclass http://127.0.0.1/ontology/Student.owl#Student");
                System.exit(0);
            } else {
                File f = new File(args[args.length - 1].toString());
                try {
                    AbstractDatatypeMapper.getInstance().loadAbstractDatatypeKB(f);
                } catch (Exception e) {
                    System.err.println("Error: " + e.getMessage());
                    System.exit(1);
                }

                String owlclassString = cmdLine.getOptionValue("owlclass");
                if (!AbstractDatatypeKB.getInstance().getAbstractDatatypeKBData().containsKey(owlclassString)) {
                    System.err.println("Error: Key " + owlclassString + " not in knowledgebase.");
                    System.exit(1);
                }

                if (cmdLine.hasOption("info")) {
                    AbstractDatatypeKB.getInstance().getAbstractDatatypeKBData().get(owlclassString)
                            .printDatatype();
                }
                if (cmdLine.hasOption("xsd")) {
                    boolean hierachy = false;
                    int depth = 0;
                    String inheritanceBehavior = AbstractDatatype.InheritanceByNone;
                    String defaultPrimitiveType = "http://www.w3.org/2001/XMLSchema#string";

                    if (cmdLine.hasOption("h")) {
                        hierachy = true;
                    }
                    if (cmdLine.hasOption("d")) {
                        depth = Integer.parseInt(cmdLine.getOptionValue("d"));
                    }
                    if (cmdLine.hasOption("b")) {
                        System.out.print("Set inheritance behaviour to: ");
                        if (cmdLine.getOptionValue("b")
                                .equals(AbstractDatatype.InheritanceByParentsFirstRDFTypeSecond)) {
                            System.out.println(AbstractDatatype.InheritanceByParentsFirstRDFTypeSecond);
                            inheritanceBehavior = AbstractDatatype.InheritanceByNone;
                        } else if (cmdLine.getOptionValue("b")
                                .equals(AbstractDatatype.InheritanceByRDFTypeFirstParentsSecond)) {
                            System.out.println(AbstractDatatype.InheritanceByRDFTypeFirstParentsSecond);
                            inheritanceBehavior = AbstractDatatype.InheritanceByRDFTypeFirstParentsSecond;
                        } else if (cmdLine.getOptionValue("b")
                                .equals(AbstractDatatype.InheritanceByRDFTypeOnly)) {
                            System.out.println(AbstractDatatype.InheritanceByRDFTypeOnly);
                            inheritanceBehavior = AbstractDatatype.InheritanceByRDFTypeOnly;
                        } else if (cmdLine.getOptionValue("b")
                                .equals(AbstractDatatype.InheritanceBySuperClassOnly)) {
                            System.out.println(AbstractDatatype.InheritanceBySuperClassOnly);
                            inheritanceBehavior = AbstractDatatype.InheritanceBySuperClassOnly;
                        } else {
                            System.out.println(AbstractDatatype.InheritanceByNone);
                            inheritanceBehavior = AbstractDatatype.InheritanceByNone;
                        }
                    }
                    if (cmdLine.hasOption("p")) {
                        defaultPrimitiveType = cmdLine.getOptionValue("p");
                        if (defaultPrimitiveType.split("#")[0].equals("http://www.w3.org/2001/XMLSchema")) {
                            System.err.println("Error: Primitive Type not valid: " + defaultPrimitiveType);
                            System.exit(1);
                        }
                    }
                    XsdSchemaGenerator xsdgen = new XsdSchemaGenerator("SCHEMATYPE", hierachy, depth,
                            inheritanceBehavior, defaultPrimitiveType);

                    try {
                        AbstractDatatypeKB.getInstance().toXSD(owlclassString, xsdgen, System.out);
                    } catch (Exception e) {
                        System.err.println("Error: " + e.getMessage());
                    }
                }

            }
        } else {
            try {
                cmdLine = parser.parse(options, args);
                if (cmdLine.hasOption("help")) {
                    printDefaultHelpMessage();
                }
            } catch (ParseException exp) {
                // oops, something went wrong
                System.out.println("Error: Parsing failed, reason: " + exp.getMessage());
                printDefaultHelpMessage();
            }
        }
    } else {
        OWLS2WSDLGui.createAndShowGUI();
    }

    // for(Iterator it=options.getOptions().iterator(); it.hasNext(); ) {
    // String optString = ((Option)it.next()).getOpt();
    // if(cmdLine.hasOption(optString)) {
    // System.out.println("Option set: "+optString);
    // }
    // }

}

From source file:net.itransformers.idiscover.discoverylisteners.XmlTopologyDeviceLogger.java

public static void main(String[] args) throws IOException, JAXBException {
    String path = "devices_and_models\\lab\\undirected";
    File dir = new File(path);
    String[] files = dir.list(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".hml"));
        }/*from w w  w. ja  v  a 2 s.c  om*/
    });
    Map<String, String> params = new HashMap<String, String>();
    params.put("path", path);
    params.put("conf/xslt", "iDiscover\\conf\\xslt\\transformator3.xslt");
    XmlTopologyDeviceLogger logger = new XmlTopologyDeviceLogger(params, null, null);

    for (String fileName : files) {
        FileInputStream is = new FileInputStream(path + File.separator + fileName);
        DiscoveredDeviceData discoveredDeviceData;
        try {
            discoveredDeviceData = JaxbMarshalar.unmarshal(DiscoveredDeviceData.class, is);
        } finally {
            is.close();
        }
        String deviceName = fileName.substring("device-".length(), fileName.length() - ".xml".length());
        logger.handleDevice(deviceName, null, discoveredDeviceData, null);
    }

}

From source file:cht.Parser.java

public static void main(String[] args) throws IOException {

    // TODO get from google drive
    boolean isUnicode = false;
    boolean isRemoveInputFileOnComplete = false;
    int rowNum;//from ww w.  jav a  2 s .c o m
    int colNum;
    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    Properties prop = new Properties();

    try {
        prop.load(new FileInputStream("config.txt"));
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    String inputFilePath = prop.getProperty("inputFile");
    String outputDirectory = prop.getProperty("outputDirectory");
    System.out.println(outputDirectory);
    // optional
    String unicode = prop.getProperty("unicode");
    String removeInputFileOnComplete = prop.getProperty("removeInputFileOnComplete");

    inputFilePath = inputFilePath.trim();
    outputDirectory = outputDirectory.trim();

    if (unicode != null) {
        isUnicode = Boolean.parseBoolean(unicode.trim());
    }
    if (removeInputFileOnComplete != null) {
        isRemoveInputFileOnComplete = Boolean.parseBoolean(removeInputFileOnComplete.trim());
    }

    Writer out = null;
    FileInputStream in = null;
    final String newLine = System.getProperty("line.separator").toString();
    final String separator = File.separator;
    try {
        in = new FileInputStream(inputFilePath);

        Workbook workbook = new XSSFWorkbook(in);

        Sheet sheet = workbook.getSheetAt(0);

        rowNum = sheet.getLastRowNum() + 1;
        colNum = sheet.getRow(0).getPhysicalNumberOfCells();

        for (int j = 1; j < colNum; ++j) {
            String outputFilename = sheet.getRow(0).getCell(j).getStringCellValue();
            // guess directory
            int slash = outputFilename.indexOf('/');
            if (slash != -1) { // has directory
                outputFilename = outputFilename.substring(0, slash) + separator
                        + outputFilename.substring(slash + 1);
            }

            String outputPath = FilenameUtils.concat(outputDirectory, outputFilename);
            System.out.println("--Writing " + outputPath);
            out = new OutputStreamWriter(new FileOutputStream(outputPath), "UTF-8");
            TreeMap<String, Object> map = new TreeMap<String, Object>();
            for (int i = 1; i < rowNum; i++) {
                try {
                    String key = sheet.getRow(i).getCell(0).getStringCellValue();
                    //String value = "";
                    Cell tmp = sheet.getRow(i).getCell(j);
                    if (tmp != null) {
                        // not empty string!
                        value = sheet.getRow(i).getCell(j).getStringCellValue();
                    }
                    if (!key.equals("") && !key.startsWith("#") && !key.startsWith(".")) {
                        value = isUnicode ? StringEscapeUtils.escapeJava(value) : value;

                        int firstdot = key.indexOf(".");
                        String keyName, keyAttribute;
                        if (firstdot > 0) {// a.b.c.d 
                            keyName = key.substring(0, firstdot); // a
                            keyAttribute = key.substring(firstdot + 1); // b.c.d
                            TreeMap oldhash = null;
                            Object old = null;
                            if (map.get(keyName) != null) {
                                old = map.get(keyName);
                                if (old instanceof TreeMap == false) {
                                    System.out.println("different type of key:" + key);
                                    continue;
                                }
                                oldhash = (TreeMap) old;
                            } else {
                                oldhash = new TreeMap();
                            }

                            int firstdot2 = keyAttribute.indexOf(".");
                            String rootName, childName;
                            if (firstdot2 > 0) {// c, d.f --> d, f
                                rootName = keyAttribute.substring(0, firstdot2);
                                childName = keyAttribute.substring(firstdot2 + 1);
                            } else {// c, d  -> d, null
                                rootName = keyAttribute;
                                childName = null;
                            }

                            TreeMap<String, Object> object = myPut(oldhash, rootName, childName);
                            map.put(keyName, object);

                        } else {// c, d  -> d, null
                            keyName = key;
                            keyAttribute = null;
                            // simple string mode
                            map.put(key, value);
                        }

                    }

                } catch (Exception e) {
                    // just ingore empty rows
                }

            }
            String json = gson.toJson(map);
            // output json
            out.write(json + newLine);
            out.close();
        }
        in.close();

        System.out.println("\n---Complete!---");
        System.out.println("Read input file from " + inputFilePath);
        System.out.println(colNum - 1 + " output files ate generated at " + outputDirectory);
        System.out.println(rowNum + " records are generated for each output file.");
        System.out.println("output file is ecoded as unicode? " + (isUnicode ? "yes" : "no"));
        if (isRemoveInputFileOnComplete) {
            File input = new File(inputFilePath);
            input.deleteOnExit();
            System.out.println("Deleted " + inputFilePath);
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            in.close();
        }
    }

}

From source file:com.joliciel.frenchTreebank.FrenchTreebank.java

/**
 * @param args/*from   w ww.j av  a 2s  . co  m*/
 */
public static void main(String[] args) throws Exception {
    String command = args[0];

    String outFilePath = "";
    String outDirPath = "";
    String treebankPath = "";
    String ftbFileName = "";
    String rawTextDir = "";
    String queryPath = "";
    String sentenceNumber = null;
    boolean firstArg = true;
    for (String arg : args) {
        if (firstArg) {
            firstArg = false;
            continue;
        }
        int equalsPos = arg.indexOf('=');
        String argName = arg.substring(0, equalsPos);
        String argValue = arg.substring(equalsPos + 1);
        if (argName.equals("outfile"))
            outFilePath = argValue;
        else if (argName.equals("outdir"))
            outDirPath = argValue;
        else if (argName.equals("ftbFileName"))
            ftbFileName = argValue;
        else if (argName.equals("treebank"))
            treebankPath = argValue;
        else if (argName.equals("sentence"))
            sentenceNumber = argValue;
        else if (argName.equals("query"))
            queryPath = argValue;
        else if (argName.equals("rawTextDir"))
            rawTextDir = argValue;
        else
            throw new RuntimeException("Unknown argument: " + argName);
    }

    TalismaneServiceLocator talismaneServiceLocator = TalismaneServiceLocator.getInstance();

    TreebankServiceLocator locator = TreebankServiceLocator.getInstance(talismaneServiceLocator);

    if (treebankPath.length() == 0)
        locator.setDataSourcePropertiesFile("jdbc-live.properties");

    if (command.equals("search")) {
        final SearchService searchService = locator.getSearchService();
        final XmlPatternSearch search = searchService.newXmlPatternSearch();
        search.setXmlPatternFile(queryPath);
        List<SearchResult> searchResults = search.perform();

        FileWriter fileWriter = new FileWriter(outFilePath);
        for (SearchResult searchResult : searchResults) {
            String lineToWrite = "";
            Sentence sentence = searchResult.getSentence();
            Phrase phrase = searchResult.getPhrase();
            lineToWrite += sentence.getFile().getFileName() + "|";
            lineToWrite += sentence.getSentenceNumber() + "|";
            List<PhraseUnit> phraseUnits = searchResult.getPhraseUnits();
            LOG.debug("Phrase: " + phrase.getId());
            for (PhraseUnit phraseUnit : phraseUnits)
                lineToWrite += phraseUnit.getLemma().getText() + "|";
            lineToWrite += phrase.getText();
            fileWriter.write(lineToWrite + "\n");
        }
        fileWriter.flush();
        fileWriter.close();
    } else if (command.equals("load")) {
        final TreebankService treebankService = locator.getTreebankService();
        final TreebankSAXParser parser = new TreebankSAXParser();
        parser.setTreebankService(treebankService);
        parser.parseDocument(treebankPath);
    } else if (command.equals("loadAll")) {
        final TreebankService treebankService = locator.getTreebankService();

        File dir = new File(treebankPath);

        String firstFile = null;
        if (args.length > 2)
            firstFile = args[2];
        String[] files = dir.list();
        if (files == null) {
            throw new RuntimeException("Not a directory or no children: " + treebankPath);
        } else {
            boolean startProcessing = true;
            if (firstFile != null)
                startProcessing = false;
            for (int i = 0; i < files.length; i++) {
                if (!startProcessing && files[i].equals(firstFile))
                    startProcessing = true;
                if (startProcessing) {
                    String filePath = args[1] + "/" + files[i];
                    LOG.debug(filePath);
                    final TreebankSAXParser parser = new TreebankSAXParser();
                    parser.setTreebankService(treebankService);
                    parser.parseDocument(filePath);
                }
            }
        }
    } else if (command.equals("loadRawText")) {
        final TreebankService treebankService = locator.getTreebankService();
        final TreebankRawTextAssigner assigner = new TreebankRawTextAssigner();
        assigner.setTreebankService(treebankService);
        assigner.setRawTextDirectory(rawTextDir);
        assigner.loadRawText();
    } else if (command.equals("tokenize")) {
        Writer csvFileWriter = null;
        if (outFilePath != null && outFilePath.length() > 0) {
            if (outFilePath.lastIndexOf("/") > 0) {
                String outputDirPath = outFilePath.substring(0, outFilePath.lastIndexOf("/"));
                File outputDir = new File(outputDirPath);
                outputDir.mkdirs();
            }

            File csvFile = new File(outFilePath);
            csvFile.delete();
            csvFile.createNewFile();
            csvFileWriter = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(csvFile, false), "UTF8"));
        }
        try {

            final TreebankService treebankService = locator.getTreebankService();
            TreebankExportService treebankExportService = locator.getTreebankExportServiceLocator()
                    .getTreebankExportService();
            TreebankUploadService treebankUploadService = locator.getTreebankUploadServiceLocator()
                    .getTreebankUploadService();
            TreebankReader treebankReader = null;

            if (treebankPath.length() > 0) {
                File treebankFile = new File(treebankPath);
                if (sentenceNumber != null)
                    treebankReader = treebankUploadService.getXmlReader(treebankFile, sentenceNumber);
                else
                    treebankReader = treebankUploadService.getXmlReader(treebankFile);

            } else {
                treebankReader = treebankService.getDatabaseReader(TreebankSubSet.ALL, 0);
            }

            TokeniserAnnotatedCorpusReader reader = treebankExportService
                    .getTokeniserAnnotatedCorpusReader(treebankReader, csvFileWriter);

            while (reader.hasNextTokenSequence()) {
                TokenSequence tokenSequence = reader.nextTokenSequence();
                List<Integer> tokenSplits = tokenSequence.getTokenSplits();
                String sentence = tokenSequence.getText();
                LOG.debug(sentence);
                int currentPos = 0;
                StringBuilder sb = new StringBuilder();
                for (int split : tokenSplits) {
                    if (split == 0)
                        continue;
                    String token = sentence.substring(currentPos, split);
                    sb.append('|');
                    sb.append(token);
                    currentPos = split;
                }
                LOG.debug(sb.toString());
            }
        } finally {
            csvFileWriter.flush();
            csvFileWriter.close();
        }
    } else if (command.equals("export")) {
        if (outDirPath.length() == 0)
            throw new RuntimeException("Parameter required: outdir");
        File outDir = new File(outDirPath);
        outDir.mkdirs();

        final TreebankService treebankService = locator.getTreebankService();
        FrenchTreebankXmlWriter xmlWriter = new FrenchTreebankXmlWriter();
        xmlWriter.setTreebankService(treebankService);

        if (ftbFileName.length() == 0) {
            xmlWriter.write(outDir);
        } else {
            TreebankFile ftbFile = treebankService.loadTreebankFile(ftbFileName);
            String fileName = ftbFileName.substring(ftbFileName.lastIndexOf('/') + 1);
            File xmlFile = new File(outDir, fileName);
            xmlFile.delete();
            xmlFile.createNewFile();

            Writer xmlFileWriter = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(xmlFile, false), "UTF8"));
            xmlWriter.write(xmlFileWriter, ftbFile);
            xmlFileWriter.flush();
            xmlFileWriter.close();
        }
    } else {
        throw new RuntimeException("Unknown command: " + command);
    }
    LOG.debug("========== END ============");
}

From source file:edu.msu.cme.rdp.kmer.KmerFilter.java

public static void main(String[] args) throws Exception {
    final KmerTrie kmerTrie;
    final SeqReader queryReader;
    final SequenceType querySeqType;
    final File queryFile;
    final KmerStartsWriter out;
    final boolean translQuery;
    final int wordSize;
    final int translTable;
    final boolean alignedSeqs;
    final List<String> refLabels = new ArrayList();
    final int maxThreads;

    try {//from   w  ww  .ja  v a 2 s . co  m
        CommandLine cmdLine = new PosixParser().parse(options, args);
        args = cmdLine.getArgs();

        if (args.length < 3) {
            throw new Exception("Unexpected number of arguments");
        }

        if (cmdLine.hasOption("out")) {
            out = new KmerStartsWriter(cmdLine.getOptionValue("out"));
        } else {
            out = new KmerStartsWriter(System.out);
        }

        if (cmdLine.hasOption("aligned")) {
            alignedSeqs = true;
        } else {
            alignedSeqs = false;
        }

        if (cmdLine.hasOption("transl-table")) {
            translTable = Integer.valueOf(cmdLine.getOptionValue("transl-table"));
        } else {
            translTable = 11;
        }

        if (cmdLine.hasOption("threads")) {
            maxThreads = Integer.valueOf(cmdLine.getOptionValue("threads"));
        } else {
            maxThreads = Runtime.getRuntime().availableProcessors();
        }

        queryFile = new File(args[1]);
        wordSize = Integer.valueOf(args[0]);
        SequenceType refSeqType = null;

        querySeqType = SeqUtils.guessSequenceType(queryFile);
        queryReader = new SequenceReader(queryFile);

        if (querySeqType == SequenceType.Protein) {
            throw new Exception("Expected nucl query sequences");
        }

        refSeqType = SeqUtils
                .guessSequenceType(new File(args[2].contains("=") ? args[2].split("=")[1] : args[2]));

        translQuery = refSeqType == SequenceType.Protein;

        if (translQuery && wordSize % 3 != 0) {
            throw new Exception("Word size must be a multiple of 3 for nucl ref seqs");
        }

        int trieWordSize;
        if (translQuery) {
            trieWordSize = wordSize / 3;
        } else {
            trieWordSize = wordSize;
        }
        kmerTrie = new KmerTrie(trieWordSize, translQuery);

        for (int index = 2; index < args.length; index++) {
            String refName;
            String refFileName = args[index];
            if (refFileName.contains("=")) {
                String[] lexemes = refFileName.split("=");
                refName = lexemes[0];
                refFileName = lexemes[1];
            } else {
                String tmpName = new File(refFileName).getName();
                if (tmpName.contains(".")) {
                    refName = tmpName.substring(0, tmpName.lastIndexOf("."));
                } else {
                    refName = tmpName;
                }
            }

            File refFile = new File(refFileName);

            if (refSeqType != SeqUtils.guessSequenceType(refFile)) {
                throw new Exception(
                        "Reference file " + refFile + " contains " + SeqUtils.guessFileFormat(refFile)
                                + " sequences but expected " + refSeqType + " sequences");
            }

            SequenceReader seqReader = new SequenceReader(refFile);
            Sequence seq;

            while ((seq = seqReader.readNextSequence()) != null) {
                if (seq.getSeqName().startsWith("#")) {
                    continue;
                }
                if (alignedSeqs) {
                    kmerTrie.addModelSequence(seq, refLabels.size());
                } else {
                    kmerTrie.addSequence(seq, refLabels.size());
                }
            }
            seqReader.close();

            refLabels.add(refName);
        }

    } catch (Exception e) {
        new HelpFormatter().printHelp("KmerSearch <word_size> <query_file> [name=]<ref_file> ...", options);
        System.err.println(e.getMessage());
        e.printStackTrace();
        System.exit(1);
        throw new RuntimeException("Stupid jvm"); //While this will never get thrown it is required to make sure javac doesn't get confused about uninitialized variables
    }

    long startTime = System.currentTimeMillis();
    long seqCount = 0;
    final int maxTasks = 25000;

    /*
     * if (args.length == 4) { maxThreads = Integer.valueOf(args[3]); } else {
     */

    //}

    System.err.println("Starting kmer mapping at " + new Date());
    System.err.println("*  Number of threads:       " + maxThreads);
    System.err.println("*  References:              " + refLabels);
    System.err.println("*  Reads file:              " + queryFile);
    System.err.println("*  Kmer length:             " + kmerTrie.getWordSize());

    final AtomicInteger processed = new AtomicInteger();
    final AtomicInteger outstandingTasks = new AtomicInteger();

    ExecutorService service = Executors.newFixedThreadPool(maxThreads);

    Sequence querySeq;

    while ((querySeq = queryReader.readNextSequence()) != null) {
        seqCount++;

        String seqString = querySeq.getSeqString();

        if (seqString.length() < 3) {
            System.err.println("Sequence " + querySeq.getSeqName() + "'s length is less than 3");
            continue;
        }

        final Sequence threadSeq = querySeq;

        Runnable r = new Runnable() {

            public void run() {
                try {
                    processSeq(threadSeq, refLabels, kmerTrie, out, wordSize, translQuery, translTable, false);
                    processSeq(threadSeq, refLabels, kmerTrie, out, wordSize, translQuery, translTable, true);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

                processed.incrementAndGet();
                outstandingTasks.decrementAndGet();
            }
        };

        outstandingTasks.incrementAndGet();
        service.submit(r);

        while (outstandingTasks.get() >= maxTasks)
            ;

        if ((processed.get() + 1) % 1000000 == 0) {
            System.err.println("Processed " + processed + " sequences in "
                    + (System.currentTimeMillis() - startTime) + " ms");
        }
    }

    service.shutdown();
    service.awaitTermination(1, TimeUnit.DAYS);

    System.err.println("Finished Processed " + processed + " sequences in "
            + (System.currentTimeMillis() - startTime) + " ms");

    out.close();
}

From source file:net.morphbank.webclient.ProcessFiles.java

/**
 * @param args/* w  w w  .j a  v a2 s.  c om*/
 *            args[0] directory including terminal '/' 
 *            args[1] prefix of request files 
 *            args[2] number of digits in file index value
 *            args[3] number of files 
 *            args[4] index of first file (default 0) 
 *            args[5] prefix of service 
 *            args[6] prefix of response files (default args[1] + "Resp")
 */
public static void main(String[] args) {
    ProcessFiles fileProcessor = new ProcessFiles();
    // restTest.processRequest(URL, UPLOAD_FILE);

    String zeros = "0000000";

    if (args.length < 4) {
        System.out.println("Too few parameters");
    } else {
        try {
            // get parameters
            String reqDir = args[0];
            String reqPrefix = args[1];
            int numDigits = Integer.valueOf(args[2]);
            if (numDigits > zeros.length())
                numDigits = zeros.length();
            NumberFormat intFormat = new DecimalFormat(zeros.substring(0, numDigits));
            int numFiles = Integer.valueOf(args[3]);
            int firstFile = 0;

            BufferedReader fileIn = new BufferedReader(new FileReader(FILE_IN_PATH));
            String line = fileIn.readLine();
            fileIn.close();
            firstFile = Integer.valueOf(line);
            // firstFile = 189;
            // numFiles = 1;
            int lastFile = firstFile + numFiles - 1;
            String url = URL;
            String respPrefix = reqPrefix + "Resp";
            if (args.length > 5)
                respPrefix = args[5];
            if (args.length > 6)
                url = args[6];

            // process files
            for (int i = firstFile; i <= lastFile; i++) {
                String xmlOutputFile = null;
                String requestFile = reqDir + reqPrefix + intFormat.format(i) + ".xml";
                System.out.println("Processing request file " + requestFile);
                String responseFile = reqDir + respPrefix + intFormat.format(i) + ".xml";
                System.out.println("Response file " + responseFile);
                // restTest.processRequest(URL, UPLOAD_FILE);
                fileProcessor.processRequest(url, requestFile, responseFile);
                Writer fileOut = new FileWriter(FILE_IN_PATH, false);
                fileOut.append(Integer.toString(i + 1));
                fileOut.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:ca.uqac.dim.net.verify.NetworkChecker.java

/**
 * Main loop. Processes command line arguments, loads the network, builds and
 * sends a file to NuSMV and handles its response.
 * @param args Command-line arguments//from   w ww .j  av a2 s  . c om
 */
public static void main(String[] args) {
    // Configuration variables
    boolean opt_show_file = false, opt_show_messages = true, opt_show_stats = false;

    // Create and parse command line options
    Option opt = null;
    Options options = new Options();
    options.addOption("f", "file", false, "Output NuSMV file to stdout");
    options.addOption("s", "time", false, "Display statistics");
    options.addOption("h", "help", false, "Show usage information");
    options.addOption("q", "quiet", false, "Do not display any message or explanation");
    opt = new Option("i", "file", true, "Input directory");
    opt.setRequired(true);
    options.addOption(opt);
    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        System.err.println("Error parsing command line arguments");
        showUsage(options);
        System.exit(1);
    }
    if (cmd == null) {
        System.err.println("Error parsing command line arguments");
        showUsage(options);
        System.exit(1);
    }

    // Get options
    assert cmd != null;
    opt_show_file = cmd.hasOption("f");
    opt_show_messages = !cmd.hasOption("q");
    opt_show_stats = cmd.hasOption("s");

    if (opt_show_messages)
        System.err.println(
                "Distributed anomaly verifier\n(C) 2012 Sylvain Hall, Universit du Qubec  Chicoutimi\n");

    // Get input directory and blob
    assert cmd.hasOption("i");
    String slash = System.getProperty("file.separator");
    String input_dir = cmd.getOptionValue("i");
    int loc = input_dir.lastIndexOf(slash);
    String dir_name = "", blob = "";
    if (loc == -1) {
        dir_name = "";
        blob = input_dir;
    } else {
        dir_name = input_dir.substring(0, loc);
        blob = input_dir.substring(loc + 1);
    }

    // Load network
    Network net = null;
    try {
        net = loadNetworkFromDirectory(dir_name, blob, slash);
    } catch (FileNotFoundException e) {
        System.err.print("Error reading ");
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }

    // Build NuSMV file
    assert net != null;
    StringBuilder sb = new StringBuilder();
    if (opt_show_file) {
        sb.append("-- File auto-generated by DistributedChecker\n");
        sb.append("-- (C) 2012  Sylvain Hall, Universit du Qubec  Chicoutimi\n\n");
    }
    sb.append(net.toSmv(opt_show_file));
    // Simple shadowing
    String f_shadowing = "G (frozen -> !(interval_l <= rule_interval_l & interval_r >= rule_interval_r & decision != rule_decision))";
    sb.append("\n\nLTLSPEC\n").append(f_shadowing);

    // Run NuSMV and parse its return value
    if (opt_show_file) {
        System.out.println(sb);
        System.exit(0);
    }
    RuntimeInfos ri = null;
    try {
        ri = runNuSMV(sb.toString());
    } catch (IOException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (InterruptedException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    assert ri != null;

    // If requested, compute and show an explanation from NuSMV's returned contents
    if (opt_show_messages) {
        ExplanationTrace t = null;
        try {
            t = net.explain(ri.m_return_contents);
        } catch (NuSmvParseException e) {
            System.err.println("Error reading NuSMV's answer");
            System.exit(1);
        }
        if (t == null) {
            // No explanation => no anomaly found
            System.out.println("No anomaly found");
        } else
            System.out.println(t);
    }

    // If requested, show runtime statistics
    if (opt_show_stats) {
        StringBuilder out = new StringBuilder();
        out.append(net.getNodeSize()).append(",");
        out.append(net.getFirewallRuleSize()).append(",");
        out.append(net.getRoutingTableSize()).append(",");
        out.append(ri.m_runtime);
        System.out.println(out);
    }
    System.exit(0);
}

From source file:ReaderIter.java

public static void main(String[] args) throws IOException {
    // The RE pattern
    Pattern patt = Pattern.compile("[A-Za-z][a-z]+");
    // A FileReader (see the I/O chapter)
    BufferedReader r = new BufferedReader(new FileReader("ReaderIter.java"));

    // For each line of input, try matching in it.
    String line;
    while ((line = r.readLine()) != null) {
        // For each match in the line, extract and print it.
        Matcher m = patt.matcher(line);
        while (m.find()) {
            // Simplest method:
            // System.out.println(m.group(0));

            // Get the starting position of the text
            int start = m.start(0);
            // Get ending position
            int end = m.end(0);
            // Print whatever matched.
            // Use CharacterIterator.substring(offset, end);
            System.out.println(line.substring(start, end));
        }/*from w ww.  j a va 2 s  .  c om*/
    }
}

From source file:edu.msu.cme.rdp.kmer.cli.FastKmerFilter.java

public static void main(String[] args) throws Exception {
    final KmerSet<Set<RefKmer>> kmerSet;
    final SeqReader queryReader;
    final SequenceType querySeqType;
    final File queryFile;
    final KmerStartsWriter out;
    final boolean translQuery;
    final int wordSize;
    final int translTable;
    final boolean alignedSeqs;
    final List<String> refLabels = new ArrayList();
    final int maxThreads;
    final int trieWordSize;

    try {//from   ww w .  jav a 2  s . co  m
        CommandLine cmdLine = new PosixParser().parse(options, args);
        args = cmdLine.getArgs();

        if (args.length < 3) {
            throw new Exception("Unexpected number of arguments");
        }

        if (cmdLine.hasOption("out")) {
            out = new KmerStartsWriter(cmdLine.getOptionValue("out"));
        } else {
            out = new KmerStartsWriter(System.out);
        }

        if (cmdLine.hasOption("aligned")) {
            alignedSeqs = true;
        } else {
            alignedSeqs = false;
        }

        if (cmdLine.hasOption("transl-table")) {
            translTable = Integer.valueOf(cmdLine.getOptionValue("transl-table"));
        } else {
            translTable = 11;
        }

        if (cmdLine.hasOption("threads")) {
            maxThreads = Integer.valueOf(cmdLine.getOptionValue("threads"));
        } else {
            maxThreads = Runtime.getRuntime().availableProcessors();
        }

        queryFile = new File(args[1]);
        wordSize = Integer.valueOf(args[0]);
        SequenceType refSeqType = null;

        querySeqType = SeqUtils.guessSequenceType(queryFile);
        queryReader = new SequenceReader(queryFile);

        if (querySeqType == SequenceType.Protein) {
            throw new Exception("Expected nucl query sequences");
        }

        refSeqType = SeqUtils
                .guessSequenceType(new File(args[2].contains("=") ? args[2].split("=")[1] : args[2]));

        translQuery = refSeqType == SequenceType.Protein;

        if (translQuery && wordSize % 3 != 0) {
            throw new Exception("Word size must be a multiple of 3 for nucl ref seqs");
        }

        if (translQuery) {
            trieWordSize = wordSize / 3;
        } else {
            trieWordSize = wordSize;
        }
        kmerSet = new KmerSet<Set<RefKmer>>();//new KmerTrie(trieWordSize, translQuery);

        for (int index = 2; index < args.length; index++) {
            String refName;
            String refFileName = args[index];
            if (refFileName.contains("=")) {
                String[] lexemes = refFileName.split("=");
                refName = lexemes[0];
                refFileName = lexemes[1];
            } else {
                String tmpName = new File(refFileName).getName();
                if (tmpName.contains(".")) {
                    refName = tmpName.substring(0, tmpName.lastIndexOf("."));
                } else {
                    refName = tmpName;
                }
            }

            File refFile = new File(refFileName);

            if (refSeqType != SeqUtils.guessSequenceType(refFile)) {
                throw new Exception(
                        "Reference file " + refFile + " contains " + SeqUtils.guessFileFormat(refFile)
                                + " sequences but expected " + refSeqType + " sequences");
            }

            SequenceReader seqReader = new SequenceReader(refFile);
            Sequence seq;

            while ((seq = seqReader.readNextSequence()) != null) {
                if (seq.getSeqName().startsWith("#")) {
                    continue;
                }

                KmerGenerator kmers;
                try {
                    if (translQuery) { //protein ref
                        kmers = new ProtKmerGenerator(seq.getSeqString(), trieWordSize, alignedSeqs);
                    } else {
                        kmers = new NuclKmerGenerator(seq.getSeqString(), trieWordSize, alignedSeqs);
                    }
                    while (kmers.hasNext()) {
                        Kmer temp = kmers.next();
                        long[] next = temp.getLongKmers();
                        Set<RefKmer> refKmers = kmerSet.get(next);
                        if (refKmers == null) {
                            refKmers = new HashSet();
                            kmerSet.add(next, refKmers);
                        }

                        RefKmer kmerRef = new RefKmer();
                        kmerRef.modelPos = kmers.getPosition();
                        kmerRef.refFileIndex = refLabels.size();
                        kmerRef.refSeqid = seq.getSeqName();
                        refKmers.add(kmerRef);
                    }
                } catch (IllegalArgumentException ex) {
                    //System.err.println(seq.getSeqName()+ " " + ex.getMessage());
                }
            }
            seqReader.close();

            refLabels.add(refName);
        }

    } catch (Exception e) {
        new HelpFormatter().printHelp(
                "KmerSearch <kmerSize> <query_file> [name=]<ref_file> ...\nkmerSize should be multiple of 3, (recommend 45, minimum 30, maximum 63) ",
                options);
        e.printStackTrace();
        System.exit(1);
        throw new RuntimeException("Stupid jvm"); //While this will never get thrown it is required to make sure javac doesn't get confused about uninitialized variables
    }

    long startTime = System.currentTimeMillis();
    long seqCount = 0;
    final int maxTasks = 25000;

    System.err.println("Starting kmer mapping at " + new Date());
    System.err.println("*  Number of threads:       " + maxThreads);
    System.err.println("*  References:              " + refLabels);
    System.err.println("*  Reads file:              " + queryFile);
    System.err.println("*  Kmer length:             " + trieWordSize);
    System.err.println("*  Kmer Refset Size:        " + kmerSet.size());

    final AtomicInteger processed = new AtomicInteger();
    final AtomicInteger outstandingTasks = new AtomicInteger();

    ExecutorService service = Executors.newFixedThreadPool(maxThreads);

    Sequence querySeq;

    while ((querySeq = queryReader.readNextSequence()) != null) {
        seqCount++;

        String seqString = querySeq.getSeqString();

        if ((!translQuery && seqString.length() < wordSize)
                || (translQuery && seqString.length() < wordSize + 2)) {
            //System.err.println(querySeq.getSeqName() + "\t" + seqString.length());
            continue;
        }

        final Sequence threadSeq = querySeq;

        Runnable r = new Runnable() {

            public void run() {
                try {
                    processSeq(threadSeq, refLabels, kmerSet, out, wordSize, translQuery, translTable, false);
                    processSeq(threadSeq, refLabels, kmerSet, out, wordSize, translQuery, translTable, true);

                    processed.incrementAndGet();
                    outstandingTasks.decrementAndGet();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        outstandingTasks.incrementAndGet();
        service.submit(r);

        while (outstandingTasks.get() >= maxTasks)
            ;

        if ((processed.get() + 1) % 1000000 == 0) {
            System.err.println("Processed " + processed + " sequences in "
                    + (System.currentTimeMillis() - startTime) + " ms");
        }
    }

    service.shutdown();
    service.awaitTermination(1, TimeUnit.DAYS);

    System.err.println("Finished Processed " + processed + " sequences in "
            + (System.currentTimeMillis() - startTime) + " ms");

    out.close();
}