Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 0A64B200C2C for ; Fri, 17 Feb 2017 00:08:32 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 08F04160B6F; Thu, 16 Feb 2017 23:08:32 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 29841160B61 for ; Fri, 17 Feb 2017 00:08:31 +0100 (CET) Received: (qmail 55672 invoked by uid 500); 16 Feb 2017 23:08:30 -0000 Mailing-List: contact notifications-help@freemarker.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@freemarker.incubator.apache.org Delivered-To: mailing list notifications@freemarker.incubator.apache.org Received: (qmail 55663 invoked by uid 99); 16 Feb 2017 23:08:30 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 Feb 2017 23:08:30 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id E8385C18DA for ; Thu, 16 Feb 2017 23:08:29 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -7.019 X-Spam-Level: X-Spam-Status: No, score=-7.019 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-2.999] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id m0hFonp3CC81 for ; Thu, 16 Feb 2017 23:08:28 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id 22FFF5FBAE for ; Thu, 16 Feb 2017 23:08:26 +0000 (UTC) Received: (qmail 55472 invoked by uid 99); 16 Feb 2017 23:08:26 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 Feb 2017 23:08:26 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 27065E0518; Thu, 16 Feb 2017 23:08:26 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: ddekany@apache.org To: notifications@freemarker.incubator.apache.org Date: Thu, 16 Feb 2017 23:08:33 -0000 Message-Id: <004a835b64834c36a570bfb1325c9c33@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [08/54] [partial] incubator-freemarker git commit: Top level package name change to org.apache.freemarker, and some of of the internal package structure changes. Other smaller cleanup. To be continued... archived-at: Thu, 16 Feb 2017 23:08:32 -0000 http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/ecb4e230/src/main/java/freemarker/template/utility/StandardCompress.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/template/utility/StandardCompress.java b/src/main/java/freemarker/template/utility/StandardCompress.java deleted file mode 100644 index 4a40acf..0000000 --- a/src/main/java/freemarker/template/utility/StandardCompress.java +++ /dev/null @@ -1,237 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package freemarker.template.utility; - -import java.io.IOException; -import java.io.Writer; -import java.util.Map; - -import freemarker.template.TemplateBooleanModel; -import freemarker.template.TemplateModelException; -import freemarker.template.TemplateNumberModel; -import freemarker.template.TemplateTransformModel; - -/** - *

A filter that compresses each sequence of consecutive whitespace - * to a single line break (if the sequence contains a line break) or a - * single space. In addition, leading and trailing whitespace is - * completely removed.

- * - *

Specify the transform parameter single_line = true - * to always compress to a single space instead of a line break.

- * - *

The default buffer size can be overridden by specifying a - * buffer_size transform parameter (in bytes).

- * - *

Note: The compress tag is implemented using this filter

- * - *

Usage:
- * From java:

- *
- * SimpleHash root = new SimpleHash();
- *
- * root.put( "standardCompress", new freemarker.template.utility.StandardCompress() );
- *
- * ...
- * 
- * - *

From your FreeMarker template:

- *
- * <transform standardCompress>
- *   <p>This    paragraph will have
- *       extraneous
- *
- * whitespace removed.</p>
- * </transform>
- * 
- * - *

Output:

- *
- * <p>This paragraph will have
- * extraneous
- * whitespace removed.</p>
- * 
- */ -public class StandardCompress implements TemplateTransformModel { - private static final String BUFFER_SIZE_KEY = "buffer_size"; - private static final String SINGLE_LINE_KEY = "single_line"; - private int defaultBufferSize; - - public static final StandardCompress INSTANCE = new StandardCompress(); - - public StandardCompress() { - this(2048); - } - - /** - * @param defaultBufferSize the default amount of characters to buffer - */ - public StandardCompress(int defaultBufferSize) { - this.defaultBufferSize = defaultBufferSize; - } - - public Writer getWriter(final Writer out, Map args) - throws TemplateModelException { - int bufferSize = defaultBufferSize; - boolean singleLine = false; - if (args != null) { - try { - TemplateNumberModel num = (TemplateNumberModel) args.get(BUFFER_SIZE_KEY); - if (num != null) - bufferSize = num.getAsNumber().intValue(); - } catch (ClassCastException e) { - throw new TemplateModelException("Expecting numerical argument to " + BUFFER_SIZE_KEY); - } - try { - TemplateBooleanModel flag = (TemplateBooleanModel) args.get(SINGLE_LINE_KEY); - if (flag != null) - singleLine = flag.getAsBoolean(); - } catch (ClassCastException e) { - throw new TemplateModelException("Expecting boolean argument to " + SINGLE_LINE_KEY); - } - } - return new StandardCompressWriter(out, bufferSize, singleLine); - } - - private static class StandardCompressWriter extends Writer { - private static final int MAX_EOL_LENGTH = 2; // CRLF is two bytes - - private static final int AT_BEGINNING = 0; - private static final int SINGLE_LINE = 1; - private static final int INIT = 2; - private static final int SAW_CR = 3; - private static final int LINEBREAK_CR = 4; - private static final int LINEBREAK_CRLF = 5; - private static final int LINEBREAK_LF = 6; - - private final Writer out; - private final char[] buf; - private final boolean singleLine; - - private int pos = 0; - private boolean inWhitespace = true; - private int lineBreakState = AT_BEGINNING; - - public StandardCompressWriter(Writer out, int bufSize, boolean singleLine) { - this.out = out; - this.singleLine = singleLine; - buf = new char[bufSize]; - } - - @Override - public void write(char[] cbuf, int off, int len) throws IOException { - for (; ; ) { - // Need to reserve space for the EOL potentially left in the state machine - int room = buf.length - pos - MAX_EOL_LENGTH; - if (room >= len) { - writeHelper(cbuf, off, len); - break; - } else if (room <= 0) { - flushInternal(); - } else { - writeHelper(cbuf, off, room); - flushInternal(); - off += room; - len -= room; - } - } - } - - private void writeHelper(char[] cbuf, int off, int len) { - for (int i = off, end = off + len; i < end; i++) { - char c = cbuf[i]; - if (Character.isWhitespace(c)) { - inWhitespace = true; - updateLineBreakState(c); - } else if (inWhitespace) { - inWhitespace = false; - writeLineBreakOrSpace(); - buf[pos++] = c; - } else { - buf[pos++] = c; - } - } - } - - /* - \r\n => CRLF - \r[^\n] => CR - \r$ => CR - [^\r]\n => LF - ^\n => LF - */ - private void updateLineBreakState(char c) { - switch (lineBreakState) { - case INIT: - if (c == '\r') { - lineBreakState = SAW_CR; - } else if (c == '\n') { - lineBreakState = LINEBREAK_LF; - } - break; - case SAW_CR: - if (c == '\n') { - lineBreakState = LINEBREAK_CRLF; - } else { - lineBreakState = LINEBREAK_CR; - } - } - } - - private void writeLineBreakOrSpace() { - switch (lineBreakState) { - case SAW_CR: - // whitespace ended with CR, fall through - case LINEBREAK_CR: - buf[pos++] = '\r'; - break; - case LINEBREAK_CRLF: - buf[pos++] = '\r'; - // fall through - case LINEBREAK_LF: - buf[pos++] = '\n'; - break; - case AT_BEGINNING: - // ignore leading whitespace - break; - case INIT: - case SINGLE_LINE: - buf[pos++] = ' '; - } - lineBreakState = (singleLine) ? SINGLE_LINE : INIT; - } - - private void flushInternal() throws IOException { - out.write(buf, 0, pos); - pos = 0; - } - - @Override - public void flush() throws IOException { - flushInternal(); - out.flush(); - } - - @Override - public void close() throws IOException { - flushInternal(); - } - } -}