Search in sources :

Example 21 with CharArrayWriter

use of java.io.CharArrayWriter in project sling by apache.

the class JspDocumentParser method processChars.

private void processChars() throws SAXException {
    if (charBuffer == null || directivesOnly) {
        return;
    }
    /*
         * JSP.6.1.1: All textual nodes that have only white space are to be
         * dropped from the document, except for nodes in a jsp:text element,
         * and any leading and trailing white-space-only textual nodes in a
         * jsp:attribute whose 'trim' attribute is set to FALSE, which are to
         * be kept verbatim.
         * JSP.6.2.3 defines white space characters.
         */
    boolean isAllSpace = true;
    if (!(current instanceof Node.JspText) && !(current instanceof Node.NamedAttribute)) {
        for (int i = 0; i < charBuffer.length(); i++) {
            if (!(charBuffer.charAt(i) == ' ' || charBuffer.charAt(i) == '\n' || charBuffer.charAt(i) == '\r' || charBuffer.charAt(i) == '\t')) {
                isAllSpace = false;
                break;
            }
        }
    }
    if (!isAllSpace && tagDependentPending) {
        tagDependentPending = false;
        tagDependentNesting++;
    }
    if (tagDependentNesting > 0) {
        if (charBuffer.length() > 0) {
            new Node.TemplateText(charBuffer.toString(), startMark, current);
        }
        startMark = new Mark(ctxt, path, locator.getLineNumber(), locator.getColumnNumber());
        charBuffer = null;
        return;
    }
    if ((current instanceof Node.JspText) || (current instanceof Node.NamedAttribute) || !isAllSpace) {
        int line = startMark.getLineNumber();
        int column = startMark.getColumnNumber();
        CharArrayWriter ttext = new CharArrayWriter();
        int lastCh = 0, elType = 0;
        for (int i = 0; i < charBuffer.length(); i++) {
            int ch = charBuffer.charAt(i);
            if (ch == '\n') {
                column = 1;
                line++;
            } else {
                column++;
            }
            if ((lastCh == '$' || lastCh == '#') && ch == '{') {
                elType = lastCh;
                if (ttext.size() > 0) {
                    new Node.TemplateText(ttext.toString(), startMark, current);
                    ttext = new CharArrayWriter();
                    //We subtract two from the column number to
                    //account for the '[$,#]{' that we've already parsed
                    startMark = new Mark(ctxt, path, line, column - 2);
                }
                // following "${" || "#{" to first unquoted "}"
                i++;
                boolean singleQ = false;
                boolean doubleQ = false;
                lastCh = 0;
                for (; ; i++) {
                    if (i >= charBuffer.length()) {
                        throw new SAXParseException(Localizer.getMessage("jsp.error.unterminated", (char) elType + "{"), locator);
                    }
                    ch = charBuffer.charAt(i);
                    if (ch == '\n') {
                        column = 1;
                        line++;
                    } else {
                        column++;
                    }
                    if (lastCh == '\\' && (singleQ || doubleQ)) {
                        ttext.write(ch);
                        lastCh = 0;
                        continue;
                    }
                    if (ch == '}') {
                        new Node.ELExpression((char) elType, ttext.toString(), startMark, current);
                        ttext = new CharArrayWriter();
                        startMark = new Mark(ctxt, path, line, column);
                        break;
                    }
                    if (ch == '"')
                        doubleQ = !doubleQ;
                    else if (ch == '\'')
                        singleQ = !singleQ;
                    ttext.write(ch);
                    lastCh = ch;
                }
            } else if (lastCh == '\\' && (ch == '$' || ch == '#')) {
                ttext.write(ch);
                // Not start of EL anymore
                ch = 0;
            } else {
                if (lastCh == '$' || lastCh == '#' || lastCh == '\\') {
                    ttext.write(lastCh);
                }
                if (ch != '$' && ch != '#' && ch != '\\') {
                    ttext.write(ch);
                }
            }
            lastCh = ch;
        }
        if (lastCh == '$' || lastCh == '#' || lastCh == '\\') {
            ttext.write(lastCh);
        }
        if (ttext.size() > 0) {
            new Node.TemplateText(ttext.toString(), startMark, current);
        }
    }
    startMark = new Mark(ctxt, path, locator.getLineNumber(), locator.getColumnNumber());
    charBuffer = null;
}
Also used : SAXParseException(org.xml.sax.SAXParseException) CharArrayWriter(java.io.CharArrayWriter)

Example 22 with CharArrayWriter

use of java.io.CharArrayWriter in project sling by apache.

the class JspUtil method removeQuotes.

public static char[] removeQuotes(char[] chars) {
    CharArrayWriter caw = new CharArrayWriter();
    for (int i = 0; i < chars.length; i++) {
        if (chars[i] == '%' && chars[i + 1] == '\\' && chars[i + 2] == '>') {
            caw.write('%');
            caw.write('>');
            i = i + 2;
        } else {
            caw.write(chars[i]);
        }
    }
    return caw.toCharArray();
}
Also used : CharArrayWriter(java.io.CharArrayWriter)

Example 23 with CharArrayWriter

use of java.io.CharArrayWriter in project sling by apache.

the class Parser method parseScriptText.

private String parseScriptText(String tx) {
    CharArrayWriter cw = new CharArrayWriter();
    int size = tx.length();
    int i = 0;
    while (i < size) {
        char ch = tx.charAt(i);
        if (i + 2 < size && ch == '%' && tx.charAt(i + 1) == '\\' && tx.charAt(i + 2) == '>') {
            cw.write('%');
            cw.write('>');
            i += 3;
        } else {
            cw.write(ch);
            ++i;
        }
    }
    cw.close();
    return cw.toString();
}
Also used : CharArrayWriter(java.io.CharArrayWriter)

Example 24 with CharArrayWriter

use of java.io.CharArrayWriter in project sling by apache.

the class Parser method parseTemplateText.

/*
     * Parse for a template text string until '<' or "${" or "#{" is encountered,
     * recognizing escape sequences "\%", "\$", and "\#".
     */
private void parseTemplateText(Node parent) throws JasperException {
    if (!reader.hasMoreInput())
        return;
    CharArrayWriter ttext = new CharArrayWriter();
    // Output the first character
    int ch = reader.nextChar();
    if (ch == '\\') {
        reader.pushChar();
    } else {
        ttext.write(ch);
    }
    while (reader.hasMoreInput()) {
        ch = reader.nextChar();
        if (ch == '<') {
            reader.pushChar();
            break;
        } else if (ch == '$' || ch == '#') {
            if (!reader.hasMoreInput()) {
                ttext.write(ch);
                break;
            }
            if (reader.nextChar() == '{') {
                reader.pushChar();
                reader.pushChar();
                break;
            }
            ttext.write(ch);
            reader.pushChar();
            continue;
        } else if (ch == '\\') {
            if (!reader.hasMoreInput()) {
                ttext.write('\\');
                break;
            }
            char next = (char) reader.peekChar();
            // convolude multiple steps at once and create confusing parsers...)
            if (next == '%' || next == '$' || next == '#') {
                ch = reader.nextChar();
            }
        }
        ttext.write(ch);
    }
    new Node.TemplateText(ttext.toString(), start, parent);
}
Also used : CharArrayWriter(java.io.CharArrayWriter)

Example 25 with CharArrayWriter

use of java.io.CharArrayWriter in project sling by apache.

the class Parser method parseXMLTemplateText.

/*
     * XMLTemplateText ::= ( S? '/>' ) | ( S? '>' ( ( Char* - ( Char* ( '<' |
     * '${' ) ) ) ( '${' ELExpressionBody )? CDSect? )* ETag ) |
     * <TRANSLATION_ERROR>
     */
private void parseXMLTemplateText(Node parent) throws JasperException {
    reader.skipSpaces();
    if (!reader.matches("/>")) {
        if (!reader.matches(">")) {
            err.jspError(start, "jsp.error.unterminated", "&lt;jsp:text&gt;");
        }
        CharArrayWriter ttext = new CharArrayWriter();
        while (reader.hasMoreInput()) {
            int ch = reader.nextChar();
            if (ch == '<') {
                // Check for <![CDATA[
                if (!reader.matches("![CDATA[")) {
                    break;
                }
                start = reader.mark();
                Mark stop = reader.skipUntil("]]>");
                if (stop == null) {
                    err.jspError(start, "jsp.error.unterminated", "CDATA");
                }
                String text = reader.getText(start, stop);
                ttext.write(text, 0, text.length());
            } else if (ch == '\\') {
                if (!reader.hasMoreInput()) {
                    ttext.write('\\');
                    break;
                }
                ch = reader.nextChar();
                if (ch != '$' && ch != '#') {
                    ttext.write('\\');
                }
                ttext.write(ch);
            } else if (ch == '$' || ch == '#') {
                if (!reader.hasMoreInput()) {
                    ttext.write(ch);
                    break;
                }
                if (reader.nextChar() != '{') {
                    ttext.write(ch);
                    reader.pushChar();
                    continue;
                }
                // Create a template text node
                new Node.TemplateText(ttext.toString(), start, parent);
                // Mark and parse the EL expression and create its node:
                start = reader.mark();
                parseELExpression(parent, (char) ch);
                start = reader.mark();
                ttext = new CharArrayWriter();
            } else {
                ttext.write(ch);
            }
        }
        new Node.TemplateText(ttext.toString(), start, parent);
        if (!reader.hasMoreInput()) {
            err.jspError(start, "jsp.error.unterminated", "&lt;jsp:text&gt;");
        } else if (!reader.matchesETagWithoutLessThan("jsp:text")) {
            err.jspError(start, "jsp.error.jsptext.badcontent");
        }
    }
}
Also used : CharArrayWriter(java.io.CharArrayWriter)

Aggregations

CharArrayWriter (java.io.CharArrayWriter)211 PrintWriter (java.io.PrintWriter)51 IOException (java.io.IOException)39 IndentingPrintWriter (com.android.internal.util.IndentingPrintWriter)21 Test (org.junit.Test)11 InputStreamReader (java.io.InputStreamReader)9 Map (java.util.Map)9 BufferedReader (java.io.BufferedReader)8 File (java.io.File)8 ArrayList (java.util.ArrayList)8 TitledBorder (javax.swing.border.TitledBorder)8 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)7 Graph (edu.cmu.tetrad.graph.Graph)7 Writable (groovy.lang.Writable)7 CharArrayReader (java.io.CharArrayReader)7 Reader (java.io.Reader)7 Date (java.util.Date)7 InputStream (java.io.InputStream)6 StringReader (java.io.StringReader)6 IKnowledge (edu.cmu.tetrad.data.IKnowledge)5