Example usage for java.io ByteArrayOutputStream ByteArrayOutputStream

List of usage examples for java.io ByteArrayOutputStream ByteArrayOutputStream

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream ByteArrayOutputStream.

Prototype

public ByteArrayOutputStream() 

Source Link

Document

Creates a new ByteArrayOutputStream .

Usage

From source file:net.itransformers.bgpPeeringMap.BgpPeeringMapFromFile.java

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

    Map<String, String> params = CmdLineParser.parseCmdLine(args);
    logger.info("input params" + params.toString());
    final String settingsFile = params.get("-s");
    if (settingsFile == null) {
        printUsage("bgpPeeringMap.properties");
        return;/*from  w  w w.  j a  va2 s  .  c om*/
    }

    Map<String, String> settings = loadProperties(new File(System.getProperty("base.dir"), settingsFile));
    logger.info("Settings" + settings.toString());

    File outputDir = new File(System.getProperty("base.dir"), settings.get("output.dir"));
    System.out.println(outputDir.getAbsolutePath());
    boolean result = outputDir.mkdir();
    //        if (!result) {
    //            System.out.println("result:"+result);
    //            System.out.println("Unable to create dir: "+outputDir);
    //            return;
    //        }

    File graphmlDir = new File(outputDir, settings.get("output.dir.graphml"));
    result = outputDir.mkdir();
    //        if (!result) {
    //            System.out.println("Unable to create dir: "+graphmlDir);
    //            return;
    //        }

    XsltTransformer transformer = new XsltTransformer();
    byte[] rawData = readRawDataFile(settings.get("raw-data-file"));
    logger.info("First-transformation has started with xsltTransformator " + settings.get("xsltFileName1"));

    ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
    File xsltFileName1 = new File(System.getProperty("base.dir"), settings.get("xsltFileName1"));
    ByteArrayInputStream inputStream1 = new ByteArrayInputStream(rawData);
    // transformer.transformXML(inputStream1, xsltFileName1, outputStream1, settings);
    logger.info("First transformation finished");
    File outputFile1 = new File(graphmlDir, "bgpPeeringMap-intermediate.xml");

    FileUtils.writeStringToFile(outputFile1, new String(outputStream1.toByteArray()));

    logger.info("Second transformation started with xsltTransformator " + settings.get("xsltFileName2"));

    ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
    File xsltFileName2 = new File(System.getProperty("base.dir"), settings.get("xsltFileName2"));
    ByteArrayInputStream inputStream2 = new ByteArrayInputStream(outputStream1.toByteArray());
    // transformer.transformXML(inputStream2, xsltFileName2, outputStream2, settings);
    logger.info("Second transformation finished");
    File outputFile = new File(graphmlDir, "bgpPeeringMap.graphml");
    File nodesFileListFile = new File(graphmlDir, "nodes-file-list.txt");
    FileUtils.writeStringToFile(outputFile, new String(outputStream2.toByteArray()));
    logger.info("Output Graphml saved in a file in" + graphmlDir);

    FileUtils.writeStringToFile(nodesFileListFile, "bgpPeeringMap.graphml");

    ByteArrayInputStream inputStream3 = new ByteArrayInputStream(outputStream2.toByteArray());
    ByteArrayOutputStream outputStream3 = new ByteArrayOutputStream();
    File xsltFileName3 = new File(System.getProperty("base.dir"), settings.get("xsltFileName3"));
    // transformer.transformXML(inputStream3, xsltFileName3, outputStream3);

}

From source file:ObfuscatingStream.java

/**
 * Obfuscates or unobfuscates the second command-line argument, depending on
 * whether the first argument starts with "o" or "u"
 * //from  w w w .  ja  va 2 s  .co m
 * @param args
 *            Command-line arguments
 * @throws IOException
 *             If an error occurs obfuscating or unobfuscating
 */
public static void main(String[] args) throws IOException {
    InputStream input = new ByteArrayInputStream(args[1].getBytes());
    StringBuilder toPrint = new StringBuilder();
    if (args[0].startsWith("o")) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        OutputStream out = obfuscate(bytes);
        int read = input.read();
        while (read >= 0) {
            out.write(read);
            read = input.read();
        }
        byte[] receiptBytes = bytes.toByteArray();
        for (int b = 0; b < receiptBytes.length; b++) {
            int chr = (receiptBytes[b] + 256) % 256;
            toPrint.append(HEX_CHARS[chr >>> 4]);
            toPrint.append(HEX_CHARS[chr & 0xf]);
        }
    } else if (args[0].startsWith("u")) {
        input = unobfuscate(input);
        InputStreamReader reader = new InputStreamReader(input);

        int read = reader.read();
        while (read >= 0) {
            toPrint.append((char) read);
            read = reader.read();
        }
    } else
        throw new IllegalArgumentException("First argument must start with o or u");
    System.out.println(toPrint.toString());
}

From source file:at.tuwien.ifs.somtoolbox.doc.RunnablesReferenceCreator.java

public static void main(String[] args) {
    ArrayList<Class<? extends SOMToolboxApp>> runnables = SubClassFinder.findSubclassesOf(SOMToolboxApp.class,
            true);// w  ww  .java 2s . com
    Collections.sort(runnables, SOMToolboxApp.TYPE_GROUPED_COMPARATOR);

    StringBuilder sbIndex = new StringBuilder(runnables.size() * 50);
    StringBuilder sbDetails = new StringBuilder(runnables.size() * 200);

    sbIndex.append("\n<table border=\"0\">\n");

    Type lastType = null;

    for (Class<? extends SOMToolboxApp> c : runnables) {
        try {
            // Ignore abstract classes and interfaces
            if (Modifier.isAbstract(c.getModifiers()) || Modifier.isInterface(c.getModifiers())) {
                continue;
            }

            Type type = Type.getType(c);
            if (type != lastType) {
                sbIndex.append("  <tr> <td colspan=\"2\"> <h5> " + type + " Applications </h5> </td> </tr>\n");
                sbDetails.append("<h2> " + type + " Applications </h2>\n");
                lastType = type;
            }
            String descr = "N/A";
            try {
                descr = (String) c.getDeclaredField("DESCRIPTION").get(null);
            } catch (Exception e) {
            }
            String longDescr = "descr";
            try {
                longDescr = (String) c.getDeclaredField("LONG_DESCRIPTION").get(null);
            } catch (Exception e) {
            }

            sbIndex.append("  <tr>\n");
            sbIndex.append("    <td> <a href=\"#").append(c.getSimpleName()).append("\">")
                    .append(c.getSimpleName()).append("</a> </td>\n");
            sbIndex.append("    <td> ").append(descr).append(" </td>\n");
            sbIndex.append("  </tr>\n");

            sbDetails.append("<h3 id=\"").append(c.getSimpleName()).append("\">").append(c.getSimpleName())
                    .append("</h3>\n");
            sbDetails.append("<p>").append(longDescr).append("</p>\n");

            try {
                Parameter[] options = (Parameter[]) c.getField("OPTIONS").get(null);
                JSAP jsap = AbstractOptionFactory.registerOptions(options);
                final ByteArrayOutputStream os = new ByteArrayOutputStream();
                PrintStream ps = new PrintStream(os);
                AbstractOptionFactory.printHelp(jsap, c.getName(), ps);
                sbDetails.append("<pre>").append(StringEscapeUtils.escapeHtml(os.toString())).append("</pre>");
            } catch (Exception e1) { // we didn't find the options => let the class be invoked ...
            }

        } catch (SecurityException e) {
            // Should not happen - no Security
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }
    sbIndex.append("</table>\n\n");
    System.out.println(sbIndex);
    System.out.println(sbDetails);
}

From source file:de.uniko.west.winter.test.basics.JenaTests.java

public static void main(String[] args) {

    Model newModel = ModelFactory.createDefaultModel();
    //      //from   w ww.j a  v a2s  .  c o  m
    Model newModel2 = ModelFactory.createModelForGraph(ModelFactory.createMemModelMaker().getGraphMaker()
            .createGraph("http://www.defaultgraph.de/graph1"));
    StringBuilder updateString = new StringBuilder();
    updateString.append("PREFIX dc: <http://purl.org/dc/elements/1.1/>");
    updateString.append("PREFIX xsd: <http://bla.org/dc/elements/1.1/>");
    updateString.append("INSERT { ");
    updateString.append(
            "<http://example/egbook1> dc:title  <http://example/egbook1/#Title1>, <http://example/egbook1/#Title2>. ");
    //updateString.append("<http://example/egbook1> dc:title  \"Title1.1\". ");
    //updateString.append("<http://example/egbook1> dc:title  \"Title1.2\". ");
    updateString.append("<http://example/egbook21> dc:title  \"Title2\"; ");
    updateString.append("dc:title  \"2.0\"^^xsd:double. ");
    updateString.append("<http://example/egbook3> dc:title  \"Title3\". ");
    updateString.append("<http://example/egbook4> dc:title  \"Title4\". ");
    updateString.append("<http://example/egbook5> dc:title  \"Title5\". ");
    updateString.append("<http://example/egbook6> dc:title  \"Title6\" ");
    updateString.append("}");

    UpdateRequest update = UpdateFactory.create(updateString.toString());
    UpdateAction.execute(update, newModel);

    StmtIterator iter = newModel.listStatements();
    System.out.println("After add");
    while (iter.hasNext()) {
        System.out.println(iter.next().toString());

    }

    StringBuilder constructQueryString = new StringBuilder();
    constructQueryString.append("PREFIX dc: <http://purl.org/dc/elements/1.1/>");
    constructQueryString.append("CONSTRUCT {?sub dc:title <http://example/egbook1/#Title1>}");
    constructQueryString.append("WHERE {");
    constructQueryString.append("?sub dc:title <http://example/egbook1/#Title1>");
    constructQueryString.append("}");

    StringBuilder askQueryString = new StringBuilder();
    askQueryString.append("PREFIX dc: <http://purl.org/dc/elements/1.1/>");
    askQueryString.append("ASK {");
    askQueryString.append("?sub dc:title <http://example/egbook1/#Title1>");
    askQueryString.append("}");

    StringBuilder selectQueryString = new StringBuilder();
    selectQueryString.append("PREFIX dc: <http://purl.org/dc/elements/1.1/>");
    selectQueryString.append("SELECT * ");
    selectQueryString.append("WHERE {");
    selectQueryString.append("?sub ?pred ?obj");
    selectQueryString.append("}");

    Query cquery = QueryFactory.create(constructQueryString.toString());
    System.out.println(cquery.getQueryType());
    Query aquery = QueryFactory.create(askQueryString.toString());
    System.out.println(aquery.getQueryType());
    Query query = QueryFactory.create(selectQueryString.toString());
    System.out.println(query.getQueryType());
    QueryExecution queryExecution = QueryExecutionFactory.create(query, newModel);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    URI test = null;
    try {
        test = new URI("http://bla.org/dc/elements/1.1/double");
    } catch (URISyntaxException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    System.out.println(test.getPath().toString().substring(test.getPath().lastIndexOf("/") + 1));
    System.out.println("java.lang."
            + test.getPath().toString().substring(test.getPath().lastIndexOf("/") + 1).substring(0, 1)
                    .toUpperCase()
            + test.getPath().toString().substring(test.getPath().lastIndexOf("/") + 1).substring(1));

    String typ = "java.lang.Boolean";
    String val = "true";

    try {
        Object typedLiteral = Class.forName(typ, true, ClassLoader.getSystemClassLoader())
                .getConstructor(String.class).newInstance(val);

        System.out.println("Type: " + typedLiteral.getClass().getName() + " Value: " + typedLiteral);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        System.out.println("Query...");
        com.hp.hpl.jena.query.ResultSet results = queryExecution.execSelect();
        System.out.println("RESULT:");
        ResultSetFormatter.output(System.out, results, ResultSetFormat.syntaxJSON);
        //         
        //         
        //         ResultSetFormatter.outputAsJSON(baos, results);
        //         System.out.println(baos.toString());
        //         System.out.println("JsonTest: ");
        //         JSONObject result = new JSONObject(baos.toString("ISO-8859-1"));
        //         for (Iterator key = result.keys(); result.keys().hasNext(); ){
        //            System.out.println(key.next());
        //            
        //            for (Iterator key2 = ((JSONObject)result.getJSONObject("head")).keys(); key2.hasNext(); ){
        //               System.out.println(key2.next());
        //            }
        //         }

        //         Model results = queryExecution.execConstruct();

        //         results.write(System.out, "TURTLE");
        //         for ( ; results.hasNext() ; ){
        //            QuerySolution soln = results.nextSolution() ;
        //            RDFNode x = soln.get("sub") ;   
        //            System.out.println("result: "+soln.get("sub")+" hasTitle "+soln.get("obj"));
        //             Resource r = soln.getResource("VarR") ; 
        //             Literal l = soln.getLiteral("VarL") ; 
        //
        //         }

    } catch (Exception e) {
        // TODO: handle exception
    }

    //      StringBuilder updateString2 = new StringBuilder();
    //      updateString2.append("PREFIX dc: <http://purl.org/dc/elements/1.1/>");
    //      updateString2.append("DELETE DATA { ");
    //      updateString2.append("<http://example/egbook3> dc:title  \"Title3\" ");
    //      updateString2.append("}");
    //
    //      UpdateAction.parseExecute(updateString2.toString(), newModel);
    //      
    //      iter = newModel.listStatements();
    //      System.out.println("After delete");
    //      while (iter.hasNext()) {
    //         System.out.println(iter.next().toString());
    //            
    //      }
    //      
    //      StringBuilder updateString3 = new StringBuilder();
    //      updateString3.append("PREFIX dc: <http://purl.org/dc/elements/1.1/>");
    //      updateString3.append("DELETE DATA { ");
    //      updateString3.append("<http://example/egbook6> dc:title  \"Title6\" ");
    //      updateString3.append("}");
    //      updateString3.append("INSERT { ");
    //      updateString3.append("<http://example/egbook6> dc:title  \"New Title6\" ");
    //      updateString3.append("}");
    //   
    //      UpdateAction.parseExecute(updateString3.toString(), newModel);

    //      UpdateAction.parseExecute(   "prefix exp: <http://www.example.de>"+
    //                           "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
    //                           "INSERT { graph <http://www.defaultgraph.de/graph1> {"+
    //                           "   <http://www.test.de#substructure1> <exp:has_relation3> <http://www.test.de#substructure2> ."+ 
    //                           "   <http://www.test.de#substructure1> <rdf:type> <http://www.test.de#substructuretype1> ."+ 
    //                           "   <http://www.test.de#substructure2> <rdf:type> <http://www.test.de#substructuretype2> ."+
    //                           "}}", newModel2);
    //      
    //      iter = newModel.listStatements();
    //      System.out.println("After update");
    //      while (iter.hasNext()) {
    //         System.out.println(iter.next().toString());
    //            
    //      }
}

From source file:com.payne.test.StringTest.java

public static void main(String[] args) throws IOException {
    //        String a = "160504185452148809-1";
    //        a = a.split("-")[0];
    //        System.out.println(a);
    //        Random r = new Random();
    //        long a = 0l;
    //        for(int i=0;i<10000;i++){
    //            a = r.nextInt(5001) + 5000;
    //            if(a==5000){
    //                System.out.println(a);
    //            }
    //        }/* ww  w  . j a  va 2s. c o m*/

    /**
     * ????
     */
    //        List<Integer> numberList = new ArrayList<>();
    //        numberList.add(1);
    //        numberList.add(2);
    //        Integer[] numbers = numberList.toArray(new Integer[numberList.size()]);
    ////        int[] numbers = new int[]{1,2};
    //        
    //        System.out.println(new Integer[]{}.length==0?0:1);
    //        
    //        Student s =  new Student();
    //        s.sumUp(new Integer[]{}.length==0?numbers:new Integer[]{1});
    //        s.sumUp(numbers);
    //        Parent p = null;
    //        Parent p2 = new Parent(new Student(5));
    //
    //        Student s = new Student();
    //        p = s.print(p);

    //        System.out.println(p==null?0:1);
    //        System.out.println(p.getAge());
    //        System.out.println(p.getStudent().getAge());
    //        int ai = 0;
    //        for(int i=0;i<2;i++){
    //            int b = 0;
    //            int b_grow = 0;
    //            for(int j=0;j<5;j++){
    //                b += new Random().nextInt(5);
    //            }
    //            
    //        }
    //        
    //        
    //        System.out.println(UUID.randomUUID().toString());

    //        int a = 1;
    //        a = doAdd(a);
    //        System.out.println(a);
    Pattern p = Pattern.compile("^\\d{1,9}(.\\d{1,2})?$");
    Matcher m = p.matcher("666666541.13");
    boolean b = m.matches();
    System.out.println(b);
    //          System.out.println(-2>>4);
    //        BigDecimal b = new BigDecimal(100.50);
    //        System.out.println(b.toString());
    // indexOf   ??
    //        String a = "?";
    //        
    //        String[] split = a.split("?");
    //        if(a.indexOf("?")>-1){
    //            System.out.println("111");
    //        }
    //        for(String s: split){
    //            System.out.println(s);
    //        }
    //        MapTestObject mto = new MapTestObject();
    //        mto.setOrderType(OrderType.TWO);
    //        
    //        String str = "\":\"";
    //        System.out.println(str);
    //        String a = ",";
    ////
    //        String[] splits = a.split(".");
    //        List<String> asList = Arrays.asList(splits);

    //        String last = "";
    //        String last = "";
    //        String last = "";
    //        if (!asList.isEmpty()) {
    //            if (asList.indexOf(last) > -1) {
    //                int indexOf = asList.indexOf(last);
    //                if (indexOf + 1 >= asList.size()) {
    //                    System.out.println("?");
    //                }else{
    //                    String next = asList.get(indexOf + 1);
    //                    if (OTHERStringUtils.isEmpty(next)) {
    //                        System.out.println("?");
    //                    } else {
    //                        System.out.println("?" + next);
    //                    }
    //                }
    //            } else {
    //                System.out.println("?");
    //            }
    //        } else {
    //            System.out.println("??");
    //        }

    //           System.out.println("?\",\""); 
    //           String a ="1123";
    //           a = StringUtils.substring(a, 0, a.length()-1);
    //           System.out.println("a="+a);
    //           
    int a = 0x12345678;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.writeInt(a);
    System.out.println(Integer.toHexString(a));
    byte[] c = baos.toByteArray();
    for (int i = 0; i < 4; i++) {
        System.out.println(Integer.toHexString(c[i]));
    }
}

From source file:com.openteach.diamond.network.waverider.command.Command.java

public static void main(String[] args) {

    ByteArrayOutputStream bout = null;
    ObjectOutputStream objOutputStream = null;

    try {/*from w  w  w.j  a  v  a  2  s.c o  m*/
        bout = new ByteArrayOutputStream();
        objOutputStream = new ObjectOutputStream(bout);
        SlaveState slaveState = new SlaveState();
        slaveState.setId(1L);
        slaveState.setIsMasterCandidate(false);
        objOutputStream.writeObject(slaveState);
        objOutputStream.flush();
        Command command = CommandFactory.createHeartbeatCommand(ByteBuffer.wrap(bout.toByteArray()));

        ByteBuffer buffer = command.marshall();
        Command cmd = Command.unmarshall(buffer);
        SlaveState ss = SlaveState.fromByteBuffer(cmd.getPayLoad());
        System.out.println(cmd.toString());
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (objOutputStream != null) {
                objOutputStream.close();
            }
            if (bout != null) {
                bout.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.kolich.http.BlockingTest.java

public static void main(String[] args) {

    final HttpClient client = getNewInstanceWithProxySelector("foobar");

    final Either<Integer, String> result = new HttpClient4Closure<Integer, String>(client) {
        @Override//from   w ww  . j a  v a2  s  . c o m
        public void before(final HttpRequestBase request) {
            request.addHeader("Authorization", "super-secret-password");
        }

        @Override
        public String success(final HttpSuccess success) throws Exception {
            return EntityUtils.toString(success.getResponse().getEntity(), UTF_8);
        }

        @Override
        public Integer failure(final HttpFailure failure) {
            return failure.getStatusCode();
        }
    }.get("http://google.com");
    if (result.success()) {
        System.out.println(result.right());
    } else {
        System.out.println(result.left());
    }

    final Either<Void, Header[]> hResult = new HttpClient4Closure<Void, Header[]>(client) {
        @Override
        public Header[] success(final HttpSuccess success) throws Exception {
            return success.getResponse().getAllHeaders();
        }
    }.head("http://example.com");
    if (hResult.success()) {
        System.out.println("Fetched " + hResult.right().length + " request headers.");
    }

    final Either<Void, String> sResult = new StringOrNullClosure(client).get("http://mark.koli.ch");
    if (sResult.success()) {
        System.out.println(sResult.right());
    } else {
        System.out.println(sResult.left());
    }

    final Either<Exception, String> eResult = new HttpClient4Closure<Exception, String>(client) {
        @Override
        public String success(final HttpSuccess success) throws Exception {
            return EntityUtils.toString(success.getResponse().getEntity(), UTF_8);
        }

        @Override
        public Exception failure(final HttpFailure failure) {
            return failure.getCause();
        }
    }.put("http://lskdjflksdfjslkf.jfjkfhddfgsdfsdf.com");
    if (!eResult.success()) {
        System.out.println(eResult.left());
    }

    // Custom check for "success".
    final Either<Exception, String> cResult = new HttpClient4Closure<Exception, String>(client) {
        @Override
        public boolean check(final HttpResponse response, final HttpContext context) {
            return (response.getStatusLine().getStatusCode() == 405);
        }

        @Override
        public String success(final HttpSuccess success) throws Exception {
            return EntityUtils.toString(success.getResponse().getEntity(), UTF_8);
        }
    }.put("http://google.com");
    if (cResult.success()) {
        System.out.println(cResult.right());
    }

    final Either<Exception, OutputStream> bResult = new HttpClient4Closure<Exception, OutputStream>(client) {
        @Override
        public OutputStream success(final HttpSuccess success) throws Exception {
            final OutputStream os = new ByteArrayOutputStream();
            IOUtils.copy(success.getResponse().getEntity().getContent(), os);
            return os;
        }

        @Override
        public Exception failure(final HttpFailure failure) {
            return failure.getCause();
        }
    }.get("http://google.com");
    if (bResult.success()) {
        System.out.println("Loaded bytes into output stream!");
    }

    final OutputStream os = new ByteArrayOutputStream();
    final Either<Exception, Integer> stResult = new HttpClient4Closure<Exception, Integer>(client) {
        @Override
        public Integer success(final HttpSuccess success) throws Exception {
            return IOUtils.copy(success.getResponse().getEntity().getContent(), os);
        }
        /*
        @Override
        public Exception failure(final HttpFailure failure) {
           return failure.getCause();
        }
        */
    }.get("http://mark.koli.ch");
    if (stResult.success()) {
        System.out.println("Loaded " + stResult.right() + " bytes.");
    }

    /*
    final HttpContext context = new BasicHttpContext();
    // Setup a basic cookie store so that the response can fetch
    // any cookies returned by the server in the response.
    context.setAttribute(COOKIE_STORE, new BasicCookieStore());
    final Either<Void,String> cookieResult =
       new HttpClientClosureExpectString(client)
    .get(new HttpGet("http://google.com"), context);
    if(cookieResult.success()) {
       // List out all cookies that came back from Google in the response.
       final CookieStore cookies = (CookieStore)context.getAttribute(COOKIE_STORE);
       for(final Cookie c : cookies.getCookies()) {
    System.out.println(c.getName() + " -> " + c.getValue());
       }
    }*/

    final Either<Integer, List<Cookie>> mmmmm = new HttpClient4Closure<Integer, List<Cookie>>(client) {
        @Override
        public void before(final HttpRequestBase request, final HttpContext context) {
            context.setAttribute(COOKIE_STORE, new BasicCookieStore());
        }

        @Override
        public List<Cookie> success(final HttpSuccess success) {
            // Extract a list of cookies from the request.
            // Might be empty.
            return ((CookieStore) success.getContext().getAttribute(COOKIE_STORE)).getCookies();
        }

        @Override
        public Integer failure(final HttpFailure failure) {
            return failure.getStatusCode();
        }
    }.get("http://google.com");
    final List<Cookie> cookies;
    if ((cookies = mmmmm.right()) != null) {
        for (final Cookie c : cookies) {
            System.out.println(c.getName() + " -> " + c.getValue());
        }
    } else {
        System.out.println("Failed miserably: " + mmmmm.left());
    }

    final Either<Void, Header[]> haResult = new HttpClient4Closure<Void, Header[]>(client) {
        @Override
        public Header[] success(final HttpSuccess success) {
            return success.getResponse().getAllHeaders();
        }
    }.head("http://java.com");
    final Header[] headers = haResult.right();
    if (headers != null) {
        for (final Header h : headers) {
            System.out.println(h.getName() + ": " + h.getValue());
        }
    }

}

From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step5LinguisticPreprocessing.java

public static void main(String[] args) throws Exception {
    // input dir - list of xml query containers
    // step4-boiler-plate/
    File inputDir = new File(args[0]);

    // output dir
    File outputDir = new File(args[1]);
    if (!outputDir.exists()) {
        outputDir.mkdirs();//www  .  j  a  v  a  2s.c o  m
    }

    // iterate over query containers
    for (File f : FileUtils.listFiles(inputDir, new String[] { "xml" }, false)) {
        QueryResultContainer queryResultContainer = QueryResultContainer
                .fromXML(FileUtils.readFileToString(f, "utf-8"));

        for (QueryResultContainer.SingleRankedResult rankedResults : queryResultContainer.rankedResults) {
            //                System.out.println(rankedResults.plainText);

            if (rankedResults.plainText != null) {
                String[] lines = StringUtils.split(rankedResults.plainText, "\n");

                // collecting all cleaned lines
                List<String> cleanLines = new ArrayList<>(lines.length);
                // collecting line tags
                List<String> lineTags = new ArrayList<>(lines.length);

                for (String line : lines) {
                    // get the tag
                    String tag = null;
                    Matcher m = OPENING_TAG_PATTERN.matcher(line);

                    if (m.find()) {
                        tag = m.group(1);
                    }

                    if (tag == null) {
                        throw new IllegalArgumentException("No html tag found for line:\n" + line);
                    }

                    // replace the tag at the beginning and the end
                    String noTagText = line.replaceAll("^<\\S+>", "").replaceAll("</\\S+>$", "");

                    // do some html cleaning
                    noTagText = noTagText.replaceAll("&nbsp;", " ");

                    noTagText = noTagText.trim();

                    // add to the output
                    if (!noTagText.isEmpty()) {
                        cleanLines.add(noTagText);
                        lineTags.add(tag);
                    }
                }

                if (cleanLines.isEmpty()) {
                    // the document is empty
                    System.err.println("Document " + rankedResults.clueWebID + " in query "
                            + queryResultContainer.qID + " is empty");
                } else {
                    // now join them back to paragraphs
                    String text = StringUtils.join(cleanLines, "\n");

                    // create JCas
                    JCas jCas = JCasFactory.createJCas();
                    jCas.setDocumentText(text);
                    jCas.setDocumentLanguage("en");

                    // annotate WebParagraph
                    SimplePipeline.runPipeline(jCas,
                            AnalysisEngineFactory.createEngineDescription(WebParagraphAnnotator.class));

                    // fill the original tag information
                    List<WebParagraph> webParagraphs = new ArrayList<>(
                            JCasUtil.select(jCas, WebParagraph.class));

                    // they must be the same size as original ones
                    if (webParagraphs.size() != lineTags.size()) {
                        throw new IllegalStateException(
                                "Different size of annotated paragraphs and original lines");
                    }

                    for (int i = 0; i < webParagraphs.size(); i++) {
                        WebParagraph p = webParagraphs.get(i);
                        // get tag
                        String tag = lineTags.get(i);

                        p.setOriginalHtmlTag(tag);
                    }

                    SimplePipeline.runPipeline(jCas,
                            AnalysisEngineFactory.createEngineDescription(StanfordSegmenter.class,
                                    // only on existing WebParagraph annotations
                                    StanfordSegmenter.PARAM_ZONE_TYPES, WebParagraph.class.getCanonicalName()));

                    // now convert to XMI
                    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
                    XmiCasSerializer.serialize(jCas.getCas(), byteOutputStream);

                    // encode to base64
                    String encoded = new BASE64Encoder().encode(byteOutputStream.toByteArray());

                    rankedResults.originalXmi = encoded;
                }
            }
        }

        // and save the query to output dir
        File outputFile = new File(outputDir, queryResultContainer.qID + ".xml");
        FileUtils.writeStringToFile(outputFile, queryResultContainer.toXML(), "utf-8");
        System.out.println("Finished " + outputFile);
    }

}

From source file:net.padaf.xmpbox.parser.XMLPropertiesDescriptionManager.java

/**
 * Sample of using to write/read information
 * /*from  w  ww  .ja  v  a 2s.c o m*/
 * @param args
 *            Not used
 * @throws BuildPDFAExtensionSchemaDescriptionException
 *             When errors during building/reading xml file
 */
public static void main(String[] args) throws BuildPDFAExtensionSchemaDescriptionException {
    XMLPropertiesDescriptionManager ptMaker = new XMLPropertiesDescriptionManager();

    // add Descriptions
    for (int i = 0; i < 3; i++) {
        ptMaker.addPropertyDescription("name" + i, "description" + i);

    }

    // Display XML conversion
    System.out.println("Display XML Result:");
    ptMaker.toXML(System.out);

    // Sample to show how to build object from XML file
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ptMaker.toXML(bos);
    IOUtils.closeQuietly(bos);

    // emulate a new reading
    InputStream is = new ByteArrayInputStream(bos.toByteArray());
    ptMaker = new XMLPropertiesDescriptionManager();
    ptMaker.loadListFromXML(is);
    List<PropertyDescription> result = ptMaker.getPropertiesDescriptionList();
    System.out.println();
    System.out.println();
    System.out.println("Result of XML Loading :");
    for (PropertyDescription propertyDescription : result) {
        System.out.println(propertyDescription.getPropertyName() + " :" + propertyDescription.getDescription());
    }
}

From source file:net.padaf.xmpbox.parser.XMLValueTypeDescriptionManager.java

/**
 * Sample of using to write/read information
 * //  ww w .  jav a 2 s .  c  o  m
 * @param args
 *            not used
 * @throws BuildPDFAExtensionSchemaDescriptionException
 *             When errors during building/reading xml file
 */
public static void main(String[] args) throws BuildPDFAExtensionSchemaDescriptionException {
    XMLValueTypeDescriptionManager vtMaker = new XMLValueTypeDescriptionManager();

    // add Descriptions
    for (int i = 0; i < 3; i++) {
        vtMaker.addValueTypeDescription("testType" + i, "nsURI" + i, "prefix" + i, "description" + i);

    }
    List<FieldDescription> fieldSample = new ArrayList<FieldDescription>();
    for (int i = 0; i < 2; i++) {
        fieldSample.add(new FieldDescription("fieldName" + i, "valueType" + i, "description" + i));
    }
    vtMaker.addValueTypeDescription("testTypeField", "http://test.withfield.com/vt/", "prefTest",
            " value type description", fieldSample);

    // Display XML conversion
    System.out.println("Display XML Result:");
    vtMaker.toXML(System.out);

    // Sample to show how to build object from XML file
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    vtMaker.toXML(bos);
    IOUtils.closeQuietly(bos);

    // emulate a new reading
    InputStream is = new ByteArrayInputStream(bos.toByteArray());
    vtMaker = new XMLValueTypeDescriptionManager();
    vtMaker.loadListFromXML(is);
    List<ValueTypeDescription> result = vtMaker.getValueTypesDescriptionList();
    System.out.println();
    System.out.println();
    System.out.println("Result of XML Loading :");
    for (ValueTypeDescription propertyDescription : result) {
        System.out.println(propertyDescription.getType() + " :" + propertyDescription.getDescription());
        if (propertyDescription.getFields() != null) {
            Iterator<FieldDescription> fit = propertyDescription.getFields().iterator();
            FieldDescription field;
            while (fit.hasNext()) {
                field = fit.next();
                System.out.println("Field " + field.getName() + " :" + field.getValueType());
            }
        }
    }

}