Example usage for java.lang StringBuffer setLength

List of usage examples for java.lang StringBuffer setLength

Introduction

In this page you can find the example usage for java.lang StringBuffer setLength.

Prototype

@Override
public synchronized void setLength(int newLength) 

Source Link

Usage

From source file:com.ikanow.aleph2.analytics.hadoop.services.BeXmlParser.java

@Override
public Tuple2<Long, IBatchRecord> getNextRecord(long currentFileIndex, String fileName, InputStream inStream) {
    try {/*from w  ww .  jav  a2s . c om*/
        if (null == _mutable_reader) {
            _mutable_reader = _factory.createXMLStreamReader(inStream);
        }

        String mutable_building_json_from = null;
        boolean mutable_just_ignored = false;
        boolean mutable_hit_identifier = false;
        String mutable_hit_value = null;

        final StringBuffer full_text = new StringBuffer();
        final StringBuffer sb = new StringBuffer();

        while (_mutable_reader.hasNext()) {

            // (note that the newline separate format for XML doesn't work, not clear why)
            int eventCode = _mutable_reader.next();

            switch (eventCode) {
            case (XMLStreamReader.START_ELEMENT): {
                final String tag_name = Lambdas.get(() -> {
                    final String tmp = _mutable_reader.getLocalName();
                    if (_prefix_mode && (null != _mutable_reader.getPrefix())) {
                        return _mutable_reader.getPrefix() + ":" + tmp;
                    } else {
                        return tmp;
                    }
                });

                if (null == mutable_building_json_from) { //(looking for a start tag)
                    if (_xml.root_fields().isEmpty()) {
                        mutable_building_json_from = tag_name;
                        mutable_just_ignored = false;
                    } else if (_xml.root_fields().contains(tag_name)) {
                        mutable_building_json_from = tag_name;
                        mutable_just_ignored = false;
                    }
                } else {
                    if (_xml.ignore_fields().contains(tag_name)) {
                        mutable_just_ignored = true;
                    } else {
                        if (_xml.preserve_case()) {
                            sb.append("<").append(_mutable_reader.getLocalName()).append(">");
                        } else {
                            sb.append("<").append(_mutable_reader.getLocalName().toLowerCase()).append(">");
                        }
                        mutable_just_ignored = false;
                    }
                }
                if (null != mutable_building_json_from) { //(building an object

                    // Check for primary key
                    mutable_hit_identifier = tag_name.equalsIgnoreCase(_xml.primary_key_field());

                    if (null != _xml.xml_text_field()) {
                        full_text.append("<").append(_mutable_reader.getLocalName());
                        for (int ii = 0; ii < _mutable_reader.getAttributeCount(); ++ii) {
                            full_text.append(" ");
                            full_text.append(_mutable_reader.getAttributeLocalName(ii)).append("=\"")
                                    .append(_mutable_reader.getAttributeValue(ii)).append('"');
                        }
                        full_text.append(">");
                    }

                    if (!mutable_just_ignored && (null != _xml.attribute_prefix())) { // otherwise ignore attributes anyway
                        int nAttributes = _mutable_reader.getAttributeCount();
                        StringBuffer sb2 = new StringBuffer();
                        for (int i = 0; i < nAttributes; ++i) {
                            sb2.setLength(0);
                            sb.append('<');

                            sb2.append(_xml.attribute_prefix());
                            if (_xml.preserve_case()) {
                                sb2.append(_mutable_reader.getAttributeLocalName(i));
                            } else {
                                sb2.append(_mutable_reader.getAttributeLocalName(i).toLowerCase());
                            }
                            sb2.append('>');

                            sb.append(sb2);
                            sb.append("<![CDATA[").append(_mutable_reader.getAttributeValue(i).trim())
                                    .append("]]>");
                            sb.append("</").append(sb2);
                        }
                    }
                }
            }
                break;

            case (XMLStreamReader.CHARACTERS): {
                if (null != mutable_building_json_from) { //(ignore unless building object)

                    // Grab primary key
                    if ((mutable_hit_identifier) && (null == mutable_hit_value)) {
                        mutable_hit_value = _mutable_reader.getText().trim();
                    }

                    if (null != _xml.xml_text_field()) {
                        full_text.append(_mutable_reader.getText());
                    }

                    if (_mutable_reader.getText().trim().length() > 0 && mutable_just_ignored == false) {
                        sb.append("<![CDATA[").append(_mutable_reader.getText().trim()).append("]]>");
                    }
                }
            }
                break;

            case (XMLStreamReader.END_ELEMENT): {
                if (null != mutable_building_json_from) {
                    final String tag_name = Lambdas.get(() -> {
                        final String tmp = _mutable_reader.getLocalName();
                        if (_prefix_mode && (null != _mutable_reader.getPrefix())) {
                            return _mutable_reader.getPrefix() + ":" + tmp;
                        } else {
                            return tmp;
                        }
                    });

                    if (null != _xml.xml_text_field()) {
                        if (_xml.preserve_case()) {
                            full_text.append("</").append(_mutable_reader.getLocalName()).append(">");
                        } else {
                            full_text.append("</").append(_mutable_reader.getLocalName().toLowerCase())
                                    .append(">");
                        }
                    }

                    if (mutable_building_json_from.equals(tag_name)) {

                        // Create JSON

                        ObjectNode mutable_json = null;
                        try {
                            //(add misc tags)
                            final JsonNode tmp = _xml_mapper
                                    .readTree("<A1455574413045>" + sb.toString() + "</A1455574413045>");
                            //(originally i thought this went to an array, but apparently not)
                            //final ArrayNode json_container = (ArrayNode) tmp;
                            //if (json_container.has(0)) {
                            // mutable_json = (ObjectNode) json_container.get(0);
                            //}
                            mutable_json = (ObjectNode) tmp;
                        } catch (Exception e) {
                            //DEBUG
                            //e.printStackTrace();

                            return null; // (leave the mutable_reader intact since the XML outline appears to be valid)                     
                        }
                        if (null == mutable_json)
                            return null; //(as above)

                        // Add _id
                        final String id_field = _xml.id_field();
                        if (!_xml.set_id_from_content()) {
                            if (!Optional.ofNullable(mutable_hit_value).orElse("").isEmpty()) {
                                mutable_json.put(id_field, _xml.primary_key_prefix() + mutable_hit_value);
                            }
                        } else { // set if from content
                            mutable_json.put(id_field,
                                    UuidUtils.get().getContentBasedUuid(sb.toString().getBytes()));
                        }

                        // Add text field if requested
                        if (null != _xml.xml_text_field()) {
                            mutable_json.put(_xml.xml_text_field(), full_text.toString());
                        }
                        return Tuples._2T(0L, new BatchRecordUtils.JsonBatchRecord(mutable_json));
                    } else if (!_xml.ignore_fields().contains(tag_name)) {
                        if (_xml.preserve_case()) {
                            sb.append("</").append(_mutable_reader.getLocalName()).append(">");
                        } else {
                            sb.append("</").append(_mutable_reader.getLocalName().toLowerCase()).append(">");
                        }
                    }
                } // (end case)
            }
                break;
            } // (end switch)
        }
    } catch (Exception e) {
        //(carry on, will return EOF)

        //DEBUG
        //e.printStackTrace();
    }
    try {
        if (null != _mutable_reader) {
            _mutable_reader.close();
            _mutable_reader = null;
        }
    } catch (XMLStreamException e) {
        //(carry on, will return EOF)
    }
    return null; //(EOF)
}

From source file:org.geoserver.security.web.user.ConfirmRemovalUserPanel.java

@Override
protected String getConfirmationMessage(GeoServerUser object) throws Exception {
    StringBuffer buffer = new StringBuffer(BeanUtils.getProperty(object, "username"));
    if ((Boolean) getDefaultModelObject()) {
        SortedSet<GeoServerRole> roles = GeoServerApplication.get().getSecurityManager().getActiveRoleService()
                .getRolesForUser(object.getUsername());
        buffer.append(" [");
        for (GeoServerRole role : roles) {
            buffer.append(role.getAuthority());
            buffer.append(" ");
        }/*from w  ww.  j  a v  a  2  s.  co m*/
        if (buffer.length() > 0) { // remove last delimiter
            buffer.setLength(buffer.length() - 1);
        }
        buffer.append("]");
    }
    return buffer.toString();
}

From source file:Main.java

/**
 * /*ww  w.  j av  a 2 s  .  com*/
 * @param elementName
 * @param attributeValue
 * @param is
 * @return Collection
 * @throws XMLStreamException
 * @throws FactoryConfigurationError
 * @throws UnsupportedEncodingException
 */
public static Collection<String> getElementValues(final String elementName, final String attributeValue,
        final InputStream is)
        throws XMLStreamException, UnsupportedEncodingException, FactoryConfigurationError {
    final Collection<String> elementValues = new ArrayList<>();
    final XMLEventReader xmlEventReader = XMLInputFactory.newInstance()
            .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name()));
    final StringBuffer characters = new StringBuffer();
    boolean read = false;

    while (xmlEventReader.peek() != null) {
        final XMLEvent event = (XMLEvent) xmlEventReader.next();

        switch (event.getEventType()) {
        case XMLStreamConstants.START_DOCUMENT:
        case XMLStreamConstants.END_DOCUMENT: {
            // Ignore.
            break;
        }
        case XMLStreamConstants.START_ELEMENT: {
            read = elementName.equals(event.asStartElement().getName().getLocalPart());

            if (read && attributeValue != null) {
                read = false;

                for (Iterator<Attribute> iterator = event.asStartElement().getAttributes(); iterator
                        .hasNext();) {
                    Attribute attribute = iterator.next();

                    if (attribute.getValue().equals(attributeValue)) {
                        read = true;
                        break;
                    }
                }
            }

            break;
        }
        case XMLStreamConstants.CHARACTERS: {
            if (read) {
                characters.append(event.asCharacters().getData());
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (read) {
                elementValues.add(characters.toString());
                characters.setLength(0);
            }

            read = false;
            break;
        }
        default: {
            // Ignore
            break;
        }
        }
    }

    return elementValues;
}

From source file:de.tudarmstadt.lt.seg.app.Segmenter.java

public static void split_and_tokenize(Reader reader, String docid, ISentenceSplitter sentenceSplitter,
        ITokenizer tokenizer, int level_filter, int level_normalize, boolean merge_types, boolean merge_tokens,
        String separator_sentence, String separator_token, String separator_desc, PrintWriter writer) {
    try {//  w  w  w . j  av  a 2s . co  m
        final StringBuffer buf = new StringBuffer(); // used for checking of stream is empty; take care when not running sequentially but in parallel!
        sentenceSplitter.init(reader).stream().sequential().forEach(sentence_segment -> {
            if (DEBUG) {
                writer.format("%s%s", docid, separator_desc);
                writer.println(sentence_segment.toString());
                writer.print(separator_sentence);
            }
            if (sentence_segment.type != SegmentType.SENTENCE)
                return;
            tokenizer.init(sentence_segment.asString());
            Stream<String> tokens = null;
            if (DEBUG)
                tokens = tokenizer.stream().map(x -> x.toString() + separator_token);
            else
                tokens = StreamSupport.stream(tokenizer
                        .filteredAndNormalizedTokens(level_filter, level_normalize, merge_types, merge_tokens)
                        .spliterator(), false).map(x -> x + separator_token);
            Spliterator<String> spliterator = tokens.spliterator();
            tokens = StreamSupport.stream(spliterator, false);
            buf.setLength(0);
            boolean empty = !spliterator.tryAdvance(x -> {
                buf.append(x);
            });
            if (empty)
                return;
            synchronized (writer) {
                // writer.write(Thread.currentThread().getId() + "\t");
                writer.format("%s%s", docid, separator_desc);
                writer.print(buf);
                tokens.forEach(writer::print);
                writer.print(separator_sentence);
                writer.flush();
            }
        });
    } catch (Exception e) {
        Throwable t = e;
        while (t != null) {
            System.err.format("%s: %s%n", e.getClass(), e.getMessage());
            t = e.getCause();
        }
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.MailUsersServlet.java

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    VitroRequest vreq = new VitroRequest(request);

    String confirmpage = "/confirmUserMail.jsp";
    String errpage = "/contact_err.jsp";
    String status = null; // holds the error status

    if (!FreemarkerEmailFactory.isConfigured(vreq)) {
        status = "This application has not yet been configured to send mail. "
                + "Email properties must be specified in the configuration properties file.";
        response.sendRedirect("test?bodyJsp=" + errpage + "&ERR=" + status);
        return;/*from  w w w.  ja v a2 s.  c o  m*/
    }

    String SPAM_MESSAGE = "Your message was flagged as spam.";

    boolean probablySpam = false;
    String spamReason = "";

    String originalReferer = (String) request.getSession().getAttribute("commentsFormReferer");
    request.getSession().removeAttribute("commentsFormReferer");
    if (originalReferer == null) {
        originalReferer = "none";
        // (the following does not support cookie-less browsing:)
        // probablySpam = true;
        // status = SPAM_MESSAGE;
    } else {
        String referer = request.getHeader("Referer");
        //Review how spam works?
        /*if (referer.indexOf("comments")<0 && referer.indexOf("correction")<0) {
        probablySpam=true;
        status = SPAM_MESSAGE ;
        spamReason = "The form was not submitted from the Contact Us or Corrections page.";
        }*/
    }

    String formType = vreq.getParameter("DeliveryType");
    List<String> deliverToArray = null;
    int recipientCount = 0;
    String deliveryfrom = null;

    // get Individuals that the User mayEditAs
    deliverToArray = getEmailsForAllUserAccounts(vreq);

    //Removed all form type stuff b/c recipients pre-configured
    recipientCount = (deliverToArray == null) ? 0 : deliverToArray.size();

    if (recipientCount == 0) {
        //log.error("recipientCount is 0 when DeliveryType specified as \""+formType+"\"");
        throw new Error("To establish the Contact Us mail capability the system administrators must  "
                + "specify at least one email address in the current portal.");
    }

    // obtain passed in form data with a simple trim on the values
    String webusername = vreq.getParameter("webusername");// Null.trim(); will give you an exception
    String webuseremail = vreq.getParameter("webuseremail");//.trim();
    String comments = vreq.getParameter("s34gfd88p9x1"); //what does this string signify?
    //webusername = "hjk54";
    //webuseremail = "hjk54@cornell.edu";
    //comments = "following are comments";

    webusername = webusername.trim();
    deliveryfrom = webuseremail;
    comments = comments.trim();
    //Removed spam filtering code

    StringBuffer msgBuf = new StringBuffer(); // contains the intro copy for the body of the email message
    String lineSeparator = System.getProperty("line.separator"); // \r\n on windows, \n on unix
    // from MyLibrary
    msgBuf.setLength(0);
    //msgBuf.append("Content-Type: text/html; charset='us-ascii'" + lineSeparator);
    msgBuf.append("<html>" + lineSeparator);
    msgBuf.append("<head>" + lineSeparator);
    msgBuf.append("<style>a {text-decoration: none}</style>" + lineSeparator);
    msgBuf.append("<title>" + deliveryfrom + "</title>" + lineSeparator);
    msgBuf.append("</head>" + lineSeparator);
    msgBuf.append("<body>" + lineSeparator);
    msgBuf.append("<h4>" + deliveryfrom + "</h4>" + lineSeparator);
    msgBuf.append("<h4>From: " + webusername + " (" + webuseremail + ")" + " at IP address "
            + request.getRemoteAddr() + "</h4>" + lineSeparator);

    //Don't need any 'likely viewing page' portion to be emailed out to the others

    msgBuf.append(lineSeparator + "</i></p><h3>Comments:</h3>" + lineSeparator);
    if (comments == null || comments.equals("")) {
        msgBuf.append("<p>BLANK MESSAGE</p>");
    } else {
        msgBuf.append("<p>" + comments + "</p>");
    }
    msgBuf.append("</body>" + lineSeparator);
    msgBuf.append("</html>" + lineSeparator);

    String msgText = msgBuf.toString();

    Calendar cal = Calendar.getInstance();

    /* outFile.println("<hr/>");
     outFile.println();
     outFile.println("<p>"+cal.getTime()+"</p>");
     outFile.println();
     if (probablySpam) {
    outFile.println("<p>REJECTED - SPAM</p>");
    outFile.println("<p>"+spamReason+"</p>");
    outFile.println();
     }
     outFile.print( msgText );
     outFile.println();
     outFile.println();
     outFile.flush();
     // outFile.close();
    */

    Session s = FreemarkerEmailFactory.getEmailSession(vreq);
    //s.setDebug(true);
    try {
        // Construct the message
        MimeMessage msg = new MimeMessage(s);
        log.debug("trying to send message from servlet");

        // Set the from address
        msg.setFrom(new InternetAddress(webuseremail));

        // Set the recipient address

        if (recipientCount > 0) {
            InternetAddress[] address = new InternetAddress[recipientCount];
            for (int i = 0; i < recipientCount; i++) {
                address[i] = new InternetAddress(deliverToArray.get(i));
            }
            msg.setRecipients(Message.RecipientType.TO, address);
        }

        // Set the subject and text
        msg.setSubject(deliveryfrom);

        // add the multipart to the message
        msg.setContent(msgText, "text/html");

        // set the Date: header
        msg.setSentDate(new Date());

        log.debug("sending from servlet");

        //if (!probablySpam)
        Transport.send(msg); // try to send the message via smtp - catch error exceptions

    } catch (AddressException e) {
        status = "Please supply a valid email address.";
        log.debug("Error - status is " + status);
    } catch (SendFailedException e) {
        status = "The system was unable to deliver your mail.  Please try again later.  [SEND FAILED]";
        log.error("Error - status is " + status);
    } catch (MessagingException e) {
        status = "The system was unable to deliver your mail.  Please try again later.  [MESSAGING]";
        log.error("Error - status is " + status, e);
    }

    //outFile.flush();
    //outFile.close();

    // Redirect to the appropriate confirmation page
    if (status == null && !probablySpam) {
        // message was sent successfully
        response.sendRedirect("test?bodyJsp=" + confirmpage);
    } else {
        // exception occurred
        response.sendRedirect("test?bodyJsp=" + errpage + "&ERR=" + status);
    }

}

From source file:org.codehaus.plexus.tools.cli.AbstractCli.java

private static String[] cleanArgs(String[] args) {
    List cleaned = new ArrayList();

    StringBuffer currentArg = null;

    for (int i = 0; i < args.length; i++) {
        String arg = args[i];/*ww  w .jav a2s  .  c o  m*/

        boolean addedToBuffer = false;

        if (arg.startsWith("\"")) {
            // if we're in the process of building up another arg, push it and start over.
            // this is for the case: "-Dfoo=bar "-Dfoo2=bar two" (note the first unterminated quote)
            if (currentArg != null) {
                cleaned.add(currentArg.toString());
            }

            // start building an argument here.
            currentArg = new StringBuffer(arg.substring(1));

            addedToBuffer = true;
        }

        // this has to be a separate "if" statement, to capture the case of: "-Dfoo=bar"
        if (arg.endsWith("\"")) {
            String cleanArgPart = arg.substring(0, arg.length() - 1);

            // if we're building an argument, keep doing so.
            if (currentArg != null) {
                // if this is the case of "-Dfoo=bar", then we need to adjust the buffer.
                if (addedToBuffer) {
                    currentArg.setLength(currentArg.length() - 1);
                }
                // otherwise, we trim the trailing " and append to the buffer.
                else {
                    // TODO: introducing a space here...not sure what else to do but collapse whitespace
                    currentArg.append(' ').append(cleanArgPart);
                }

                // we're done with this argument, so add it.
                cleaned.add(currentArg.toString());
            } else {
                // this is a simple argument...just add it.
                cleaned.add(cleanArgPart);
            }

            // the currentArg MUST be finished when this completes.
            currentArg = null;

            continue;
        }

        // if we haven't added this arg to the buffer, and we ARE building an argument
        // buffer, then append it with a preceding space...again, not sure what else to
        // do other than collapse whitespace.
        // NOTE: The case of a trailing quote is handled by nullifying the arg buffer.
        if (!addedToBuffer) {
            // append to the argument we're building, collapsing whitespace to a single space.
            if (currentArg != null) {
                currentArg.append(' ').append(arg);
            }
            // this is a loner, just add it directly.
            else {
                cleaned.add(arg);
            }
        }
    }

    // clean up.
    if (currentArg != null) {
        cleaned.add(currentArg.toString());
    }

    int cleanedSz = cleaned.size();
    String[] cleanArgs = null;

    if (cleanedSz == 0) {
        // if we didn't have any arguments to clean, simply pass the original array through
        cleanArgs = args;
    } else {
        cleanArgs = (String[]) cleaned.toArray(new String[cleanedSz]);
    }

    return cleanArgs;
}

From source file:com.ikanow.aleph2.harvest.logstash.utils.TestLogstashConfigUtils.java

@Test
public void test_logstashConfigUtils() throws IOException {
    LogstashHarvesterConfigBean globals = BeanTemplateUtils.build(LogstashHarvesterConfigBean.class).done()
            .get(); //(all defaults)

    // 1) Errored sources - things that break the formatting
    StringBuffer errors = new StringBuffer();
    String testName;/*  w  ww  .j  a  v  a2s. co m*/
    // 1.1) {} mismatch 1
    //a
    errors.setLength(0);
    testName = "error_1_1a";
    if (null != LogstashConfigUtils.parseLogstashConfig(getTestFile(testName), errors)) {
        fail("**** FAIL " + testName);
    } else if (!errors.toString().startsWith("{} Mismatch (})")) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }
    //b
    errors.setLength(0);
    testName = "error_1_1b";
    if (null != LogstashConfigUtils.parseLogstashConfig(getTestFile(testName), errors)) {
        fail("**** FAIL " + testName);
    } else if (!errors.toString().startsWith("{} Mismatch (})")) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }
    //c
    errors.setLength(0);
    testName = "error_1_1c";
    if (null != LogstashConfigUtils.parseLogstashConfig(getTestFile(testName), errors)) {
        fail("**** FAIL " + testName);
    } else if (!errors.toString().startsWith("{} Mismatch (})")) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }

    // 1.2) {} mismatch 2

    //a
    errors.setLength(0);
    testName = "error_1_2a";
    if (null != LogstashConfigUtils.parseLogstashConfig(getTestFile(testName), errors)) {
        fail("**** FAIL " + testName);
    } else if (!errors.toString().startsWith("{} Mismatch ({)")) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }

    // 1.3) multiple input/filter blocks
    // 1.3a) input
    errors.setLength(0);
    testName = "error_1_3a";
    if (null != LogstashConfigUtils.parseLogstashConfig(getTestFile(testName), errors)) {
        fail("**** FAIL " + testName);
    } else if (!errors.toString().equals("Multiple input or filter blocks: input")) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }
    // 1.3b) filter
    errors.setLength(0);
    testName = "error_1_3b";
    if (null != LogstashConfigUtils.parseLogstashConfig(getTestFile(testName), errors)) {
        fail("**** FAIL " + testName);
    } else if (!errors.toString().equals("Multiple input or filter blocks: filter")) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }

    // 1.4) unrecognized blocks
    // a output - special case
    errors.setLength(0);
    testName = "error_1_4a";
    if (null != LogstashConfigUtils.parseLogstashConfig(getTestFile(testName), errors)) {
        fail("**** FAIL " + testName);
    } else if (!errors.toString()
            .equals("Not allowed output blocks - these are appended automatically by the logstash harvester")) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }
    // b
    errors.setLength(0);
    testName = "error_1_4b";
    if (null != LogstashConfigUtils.parseLogstashConfig(getTestFile(testName), errors)) {
        fail("**** FAIL " + testName);
    } else if (!errors.toString().equals("Unrecognized processing block: something_random")) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }

    // 1.5) fields/sub-elements that are not permitted
    // a ... sincedb_path
    errors.setLength(0);
    testName = "error_1_5a";
    if (null != LogstashConfigUtils.parseLogstashConfig(getTestFile(testName), errors)) {
        fail("**** FAIL " + testName);
    } else if (!errors.toString().equals("Not allowed sincedb_path in input.* block")) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }
    // b ... filter as sub-path of input
    errors.setLength(0);
    testName = "error_1_5b";
    if (null != LogstashConfigUtils.parseLogstashConfig(getTestFile(testName), errors)) {
        fail("**** FAIL " + testName);
    } else if (!errors.toString().equals("Not allowed sub-elements of input called 'filter' (1)")) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }
    // c ... filter as sub-path of sub-element of input
    errors.setLength(0);
    testName = "error_1_5c";
    if (null != LogstashConfigUtils.parseLogstashConfig(getTestFile(testName), errors)) {
        fail("**** FAIL " + testName);
    } else if (!errors.toString().equals("Not allowed sub-elements of input called 'filter' (2)")) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }

    // 2) Valid formatted source
    ObjectNode retVal;
    String output;
    String inputName; // (for re-using config files across text)
    //2.1)
    errors.setLength(0);
    testName = "success_2_1";
    if (null == (retVal = LogstashConfigUtils.parseLogstashConfig(getTestFile(testName), errors))) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    } else if (!retVal.toString().equals(
            "{ \"input\" : { \"file\" : [ { \"path\" : { } , \"start_position\" : { } , \"type\" : { } , \"codec.multiline\" : { }}]} , \"filter\" : { \"csv\" : [ { \"columns\" : { }}] , \"drop\" : [ { }] , \"mutate\" : [ { \"convert\" : { }} , { \"add_fields\" : { }} , { \"rename\" : { }}] , \"date\" : [ { \"timezone\" : { } , \"match\" : { }}] , \"geoip\" : [ { \"source\" : { } , \"fields\" : { }}]}}"
                    .replace(" ", ""))) {
        fail("**** FAIL " + testName + ": " + retVal.toString());
    }
    //System.out.println("(val="+retVal+")");

    // 2.2
    errors.setLength(0);
    testName = "success_2_2";
    if (null == (retVal = LogstashConfigUtils.parseLogstashConfig(getTestFile(testName), errors))) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }
    if (null == JsonUtils.getProperty("filter.geoip.fields", retVal)) {
        fail("**** FAIL " + testName + ": " + retVal);
    }
    //System.out.println(retVal);

    //2.3)   - check that the sincedb is added correctly, plus the sourceKey manipulation
    // (USE success_2_1 for this)
    errors.setLength(0);
    testName = "inputs_2_3";
    inputName = "success_2_3";
    if (null == (output = LogstashConfigUtils.validateLogstashInput(globals, testName, getTestFile(inputName),
            errors, true))) {
        fail("**** FAIL " + testName + ": errored: " + errors);
    } else {
        String outputToTest = output.replaceAll("[\r\n]", "").replaceAll("\\s+", " ");
        String testAgainst = "input { file { sincedb_path => \"_XXX_DOTSINCEDB_XXX_\" path => \"/root/odin-poc-data/proxy_logs/may_known_cnc.csv\" start_position => beginning type => \"proxy_logs\" codec => multiline { pattern => \"^%{YEAR}-%{MONTHNUM}-%{MONTHDAY}%{DATA:summary}\" negate => true what => \"previous\" } add_field => [ \"[@metadata][sourceKey]\", \"inputs_2_3\"] }}filter { if [@metadata][sourceKey] == \"inputs_2_3\" { if [type] == \"proxy_logs\" { csv { columns => [\"Device_Name\",\"SimpleDate\",\"Event_#Date\",\"Source_IP\",\"Source_Port\",\"Destination_IP\",\"Destination_Port\",\"Protocol\",\"Vendor_Alert\",\"MSS_Action\",\"Logging_Device_IP\",\"Application\",\"Bytes_Received\",\"Bytes_Sent\",\"Dest._Country\",\"Message\",\"Message_Type\",\"MSS_Log_Source_IP\",\"MSS_Log_Source_Type\",\"MSS_Log_Source_UUID\",\"network_protocol_id\",\"OS_Type\",\"PIX_Main-Code\",\"PIX_Sub-Code\",\"Port\",\"Product_ID\",\"Product\",\"Rule\",\"Rule_Identifier\",\"Sensor_Name\",\"Class\",\"Translate_Destination_IP\",\"Translate_Destination_Port\",\"Translate_Source_IP\"] } if [Device_Name] == \"Device Name\" { drop {} } mutate { convert => [ \"Bytes_Received\", \"integer\" ] convert => [ \"Bytes_Sent\", \"integer\" ] } date { timezone => \"Europe/London\" match => [ \"Event_Date\" , \"yyyy-MM-dd'T'HH:mm:ss\" ] } geoip { source => \"Destination_IP\" fields => [\"timezone\",\"location\",\"latitude\",\"longitude\"] } } mutate { update => [ \"[@metadata][sourceKey]\", \"inputs_2_3\"] } }}";
        testAgainst = testAgainst.replaceAll("[\n\r]", "\\\\n").replaceAll("\\s+", " ");
        assertEquals(testAgainst, outputToTest);
    }

    // 3) Valid formatted source, access to restricted types

    // 3.1) input 
    // a) restricted - admin
    // (USE success_2_1 for this)
    errors.setLength(0);
    testName = "inputs_3_1a";
    inputName = "success_2_1";
    if (null != (output = LogstashConfigUtils.validateLogstashInput(globals, testName, getTestFile(inputName),
            errors, false))) {
        fail("**** FAIL " + testName + ": Should have errored: " + output);
    } else if (!errors.toString()
            .startsWith("Security error, non-admin not allowed input type file, allowed options: ")) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }

    // b) restricted - non admin
    // (USE success_2_1 for this)
    errors.setLength(0);
    testName = "inputs_3_1b";
    inputName = "success_2_1";
    if (null == (output = LogstashConfigUtils.validateLogstashInput(globals, testName, getTestFile(inputName),
            errors, true))) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }

    // c) unrestricted - non admin
    errors.setLength(0);
    testName = "inputs_3_1c";
    inputName = "inputs_3_1c";
    if (null == (output = LogstashConfigUtils.validateLogstashInput(globals, testName, getTestFile(inputName),
            errors, true))) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }
    //System.out.println("(val="+output+")");

    // d) no input at all
    errors.setLength(0);
    testName = "inputs_3_1d";
    inputName = "inputs_3_1d";
    if (null != (output = LogstashConfigUtils.validateLogstashInput(globals, testName, getTestFile(inputName),
            errors, false))) {
        fail("**** FAIL " + testName + ": Should have errored: " + output);
    } else if (!errors.toString().startsWith(
            "Invalid input format, should be 'input { INPUT_TYPE { ... } }' (only one INPUT_TYPE) and also contain a filter, no \"s around them.")) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }

    // 3.2) filter
    // a) restricted - admin
    errors.setLength(0);
    testName = "filters_3_2a";
    inputName = "filters_3_2a";
    if (null != (output = LogstashConfigUtils.validateLogstashInput(globals, testName, getTestFile(inputName),
            errors, false))) {
        fail("**** FAIL " + testName + ": Should have errored: " + output);
    } else if (!errors.toString()
            .startsWith("Security error, non-admin not allowed filter type elasticsearch, allowed options: ")) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }
    //System.out.println("(err="+errors.toString()+")");

    // b) restricted - non admin
    // (USE filters_3_2a for this)
    errors.setLength(0);
    testName = "filters_3_2a";
    inputName = "filters_3_2a";
    if (null == (output = LogstashConfigUtils.validateLogstashInput(globals, testName, getTestFile(inputName),
            errors, true))) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }
    //System.out.println("(val="+output+")");

    // c) unrestricted - non admin
    // (implicitly tested via 3.1bc)

    // d) no filter at all
    errors.setLength(0);
    testName = "filters_3_2d";
    inputName = "filters_3_2d";
    if (null != (output = LogstashConfigUtils.validateLogstashInput(globals, testName, getTestFile(inputName),
            errors, false))) {
        fail("**** FAIL " + testName + ": Should have errored: " + output);
    } else if (!errors.toString().startsWith(
            "Invalid input format, should be 'input { INPUT_TYPE { ... } }' (only one INPUT_TYPE) and also contain a filter, no \"s around them.")) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }

    // e) filter w/ regex that has { } in it
    // (USE filters_3_2e for this)
    errors.setLength(0);
    testName = "filters_3_2e";
    inputName = "filters_3_2e";
    if (null == (output = LogstashConfigUtils.validateLogstashInput(globals, testName, getTestFile(inputName),
            errors, true))) {
        fail("**** FAIL " + testName + ": " + errors.toString());
    }

}

From source file:org.dcm4che2.tool.dcmdir.DcmDir.java

private void dump(DicomObject firstRec, DicomObjectToStringParam param, String id, StringBuffer sb)
        throws IOException {
    int i = 1;/* w  w w.  jav  a 2  s.  c  o m*/
    for (DicomObject rec = firstRec; rec != null; rec = dicomdir.findNextSiblingRecord(rec), ++i) {
        sb.setLength(0);
        rec.toStringBuffer(sb, param);
        System.out.println(
                "" + rec.getItemOffset() + ": " + rec.getString(Tag.DirectoryRecordType) + " - " + id + i);
        System.out.println(sb.toString());
        dump(dicomdir.findFirstChildRecord(rec), param, id + i + '.', sb);
    }
}

From source file:org.mindswap.swoop.renderer.ontology.SwoopSpeciesValidationRenderer.java

License:asdf

public void render(OWLOntology ontology, SwoopModel swoopModel, Writer writer) throws RendererException {

    myModel = swoopModel;//from   w  ww.j  av  a2 s.  co  m

    PrintWriter out = new PrintWriter(writer);

    SpeciesValidator sv = null;
    try {
        sv = new SwoopSpeciesValidator(swoopModel);

    } catch (OWLException e1) {
        throw new RendererException(e1.getMessage());
    }
    StringWriter lw = new StringWriter();
    StringWriter dw = new StringWriter();
    StringWriter fw = new StringWriter();
    StringWriter rw = new StringWriter();
    StringWriter mw = new StringWriter();
    final PrintWriter lpw = new PrintWriter(lw);
    final PrintWriter dpw = new PrintWriter(dw);
    final PrintWriter fpw = new PrintWriter(fw);
    final PrintWriter mpw = new PrintWriter(mw);

    final StringBuffer level = new StringBuffer();

    sv.setReporter(new SpeciesValidatorReporter() {
        public void ontology(OWLOntology onto) {
        }

        public void done(String str) {
            level.setLength(0);
            level.append(str);
        }

        public void message(String str) {
            mpw.println("<li>" + str + "</li>");
        }

        public void explain(int l, int code, String str) {
            str = hyperlinkizeCode(str);
            switch (l) {
            case SwoopSpeciesValidator.LITE:
                lpw.println("<li>" + reformatInHTML(str) + "</li>");
                break;
            case SwoopSpeciesValidator.DL:
                dpw.println("<li>" + reformatInHTML(str) + "</li>");
                break;
            case SwoopSpeciesValidator.FULL:
                fpw.println("<li>" + reformatInHTML(str) + "</li>");
                break;
            }
        }
    });

    try {
        //int l = SwoopSpeciesValidator.LITE;
        // check for OWL lite so we get all the messages
        sv.isOWLLite(ontology);
        out.println("<html><body style='background-color: white; color: black'>");
        out.println("<FONT FACE=\"" + swoopModel.getFontFace() + "\" SIZE=" + swoopModel.getFontSize() + ">");
        //         out.println("<b>DL Expressivity:</b> " + swoopModel.getReasoner().getExpressivity()+"<br>");
        out.println("<h1>Level: " + level + "<h1>");

        //out.println( "<li>"+"asdf adfd "+ encodeHLink("http://www.google.com", "http://www.google.com") + "</li>"); 

        if (!lw.toString().equals("")) {
            out.println("<p><strong>OWL-Lite features:</strong></p>");
            out.println("<ul>");
            out.println(lw.toString());
            out.println("</ul>");
        } // end of if ()
        if (!dw.toString().equals("")) {
            out.println("<p><strong>OWL-DL features:</strong></p>");
            out.println("<ul>");
            out.println(dw.toString());
            out.println("</ul>");
        } // end of if ()
        if (!fw.toString().equals("")) {
            out.println("<p><strong>OWL-Full features:</strong></p>");
            out.println("<ul>");
            out.println(fw.toString());
            out.println("</ul>");
        } // end of if ()
        if (!mw.toString().equals("")) {
            out.println("<p><b>Additional Messages</b></p>");
            out.println("<ul>");
            out.println(mw.toString());
            out.println("</ul>");
        } // end of if ()

        out.println("</FONT>");
        out.println("</body></html>");
    } catch (Exception e) {
        out.println("Exception: " + e.getMessage());
        e.printStackTrace();
    } // end of try-catch

}

From source file:com.thoughtworks.cruise.util.command.CommandLine.java

public static String[] translateCommandLine(String toProcess) throws CommandLineException {
    if (toProcess == null || toProcess.length() == 0) {
        return new String[0];
    }/* w  w w .j a  va 2s  .c o  m*/

    // parse with a simple finite state machine

    final int normal = 0;
    final int inQuote = 1;
    final int inDoubleQuote = 2;
    int state = normal;
    StringTokenizer tok = new StringTokenizer(toProcess, "\"\' ", true);
    Vector v = new Vector();
    StringBuffer current = new StringBuffer();

    while (tok.hasMoreTokens()) {
        String nextTok = tok.nextToken();
        switch (state) {
        case inQuote:
            if ("\'".equals(nextTok)) {
                state = normal;
            } else {
                current.append(nextTok);
            }
            break;
        case inDoubleQuote:
            if ("\"".equals(nextTok)) {
                state = normal;
            } else {
                current.append(nextTok);
            }
            break;
        default:
            if ("\'".equals(nextTok)) {
                state = inQuote;
            } else if ("\"".equals(nextTok)) {
                state = inDoubleQuote;
            } else if (" ".equals(nextTok)) {
                if (current.length() != 0) {
                    v.addElement(current.toString());
                    current.setLength(0);
                }
            } else {
                current.append(nextTok);
            }
            break;
        }
    }

    if (current.length() != 0) {
        v.addElement(current.toString());
    }

    if (state == inQuote || state == inDoubleQuote) {
        throw new CommandLineException("unbalanced quotes in " + toProcess);
    }

    String[] args = new String[v.size()];
    v.copyInto(args);
    return args;
}