/* Generated By:JavaCC: Do not edit this line. StructuredFieldParser.java */ /**************************************************************** * 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 org.apache.james.mime4j.field.structured.parser; /** * Parses generic structure fields. * Unfolds and removes comments. */ public class StructuredFieldParser implements StructuredFieldParserConstants { private boolean preserveFolding = false; /** * Should the \r\n folding sequence be preserved? */ public boolean isFoldingPreserved() { return preserveFolding; } /** * Sets whether the \r\n folding sequence should be preserved. */ public void setFoldingPreserved(boolean preserveFolding) { this.preserveFolding = preserveFolding; } /** * Unfolds the input and removes comments. * @return unfolded header value with comments removed */ public String parse() throws ParseException { try { return doParse(); } catch (TokenMgrError e) { // An issue with the TOKENiser // but it's not polite to throw an Error // when executing on a server throw new ParseException(e); } } final private String doParse() throws ParseException { Token t; StringBuffer buffer = new StringBuffer(50); boolean whitespace = false; boolean first = true; label_1: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case STRING_CONTENT: case FOLD: case QUOTEDSTRING: case WS: case CONTENT: ; break; default: jj_la1[0] = jj_gen; break label_1; } switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case CONTENT: t = jj_consume_token(CONTENT); if (first) { first = false; } else if (whitespace) { buffer.append(" "); whitespace = false; } buffer.append(t.image); break; case STRING_CONTENT: t = jj_consume_token(STRING_CONTENT); buffer.append(t.image); break; case QUOTEDSTRING: t = jj_consume_token(QUOTEDSTRING); if (first) { first = false; } else if (whitespace) { buffer.append(" "); whitespace = false; } buffer.append(t.image); break; case FOLD: t = jj_consume_token(FOLD); if (preserveFolding) buffer.append("\u005cr\u005cn"); break; case WS: t = jj_consume_token(WS); whitespace = true; break; default: jj_la1[1] = jj_gen; jj_consume_token(-1); throw new ParseException(); } } {if (true) return buffer.toString();} throw new Error("Missing return statement in function"); } /** Generated Token Manager. */ public StructuredFieldParserTokenManager token_source; SimpleCharStream jj_input_stream; /** Current token. */ public Token token; /** Next token. */ public Token jj_nt; private int jj_ntk; private int jj_gen; final private int[] jj_la1 = new int[2]; static private int[] jj_la1_0; static { jj_la1_init_0(); } private static void jj_la1_init_0() { jj_la1_0 = new int[] {0xf800,0xf800,}; } /** Constructor with InputStream. */ public StructuredFieldParser(java.io.InputStream stream) { this(stream, null); } /** Constructor with InputStream and supplied encoding */ public StructuredFieldParser(java.io.InputStream stream, String encoding) { try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } token_source = new StructuredFieldParserTokenManager(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 2; i++) jj_la1[i] = -1; } /** Reinitialise. */ public void ReInit(java.io.InputStream stream) { ReInit(stream, null); } /** Reinitialise. */ public void ReInit(java.io.InputStream stream, String encoding) { try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } token_source.ReInit(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 2; i++) jj_la1[i] = -1; } /** Constructor. */ public StructuredFieldParser(java.io.Reader stream) { jj_input_stream = new SimpleCharStream(stream, 1, 1); token_source = new StructuredFieldParserTokenManager(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 2; i++) jj_la1[i] = -1; } /** Reinitialise. */ public void ReInit(java.io.Reader stream) { jj_input_stream.ReInit(stream, 1, 1); token_source.ReInit(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 2; i++) jj_la1[i] = -1; } /** Constructor with generated Token Manager. */ public StructuredFieldParser(StructuredFieldParserTokenManager tm) { token_source = tm; token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 2; i++) jj_la1[i] = -1; } /** Reinitialise. */ public void ReInit(StructuredFieldParserTokenManager tm) { token_source = tm; token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 2; i++) jj_la1[i] = -1; } private Token jj_consume_token(int kind) throws ParseException { Token oldToken; if ((oldToken = token).next != null) token = token.next; else token = token.next = token_source.getNextToken(); jj_ntk = -1; if (token.kind == kind) { jj_gen++; return token; } token = oldToken; jj_kind = kind; throw generateParseException(); } /** Get the next Token. */ final public Token getNextToken() { if (token.next != null) token = token.next; else token = token.next = token_source.getNextToken(); jj_ntk = -1; jj_gen++; return token; } /** Get the specific Token. */ final public Token getToken(int index) { Token t = token; for (int i = 0; i < index; i++) { if (t.next != null) t = t.next; else t = t.next = token_source.getNextToken(); } return t; } private int jj_ntk() { if ((jj_nt=token.next) == null) return (jj_ntk = (token.next=token_source.getNextToken()).kind); else return (jj_ntk = jj_nt.kind); } private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>(); private int[] jj_expentry; private int jj_kind = -1; /** Generate ParseException. */ public ParseException generateParseException() { jj_expentries.clear(); boolean[] la1tokens = new boolean[18]; if (jj_kind >= 0) { la1tokens[jj_kind] = true; jj_kind = -1; } for (int i = 0; i < 2; i++) { if (jj_la1[i] == jj_gen) { for (int j = 0; j < 32; j++) { if ((jj_la1_0[i] & (1<<j)) != 0) { la1tokens[j] = true; } } } } for (int i = 0; i < 18; i++) { if (la1tokens[i]) { jj_expentry = new int[1]; jj_expentry[0] = i; jj_expentries.add(jj_expentry); } } int[][] exptokseq = new int[jj_expentries.size()][]; for (int i = 0; i < jj_expentries.size(); i++) { exptokseq[i] = jj_expentries.get(i); } return new ParseException(token, exptokseq, tokenImage); } /** Enable tracing. */ final public void enable_tracing() { } /** Disable tracing. */ final public void disable_tracing() { } }