All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.phloc.commons.mime.EMimeQuoting Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
/**
 * Copyright (C) 2006-2015 phloc systems
 * http://www.phloc.com
 * office[at]phloc[dot]com
 *
 * Licensed 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 com.phloc.commons.mime;

import java.util.BitSet;

import javax.annotation.Nonnull;

import com.phloc.commons.annotations.Nonempty;
import com.phloc.commons.charset.CCharset;
import com.phloc.commons.codec.DecoderException;
import com.phloc.commons.codec.EncoderException;
import com.phloc.commons.codec.QuotedPrintableCodec;
import com.phloc.commons.codec.URLCodec;

/**
 * Defines the possible MIME type parameter value quotings
 * 
 * @author Philip Helger
 */
public enum EMimeQuoting
{
  /**
   * Create a quoted string according to RFC 822 (surrounding everything in
   * double-quotes and masking using backslash).
* Example: foo bar results in "foo bar" */ QUOTED_STRING { @Override @Nonnull @Nonempty public String getQuotedString (@Nonnull @Nonempty final String sUnquotedString) { final StringBuilder aSB = new StringBuilder (sUnquotedString.length () * 2); aSB.append (QUOTED_STRING_SEPARATOR_CHAR); final char [] aChars = sUnquotedString.toCharArray (); for (final char c : aChars) if (c == QUOTED_STRING_SEPARATOR_CHAR || c == QUOTED_STRING_MASK_CHAR) { // Mask char aSB.append (QUOTED_STRING_MASK_CHAR).append (c); } else aSB.append (c); return aSB.append (QUOTED_STRING_SEPARATOR_CHAR).toString (); } @Override @Nonnull @Nonempty public String getUnquotedString (@Nonnull @Nonempty final String sQuotedString) { throw new UnsupportedOperationException ("This is handled directly inside the MimeTypeParser!"); } }, /** * Create a quoted printable String. Replace all non-printable characters with * =XY where XY is the hex encoding of the char.
* Example: foo bar results in foo=20bar */ QUOTED_PRINTABLE { @Override @Nonnull @Nonempty public String getQuotedString (@Nonnull @Nonempty final String sUnquotedString) { // Use a special BitSet return QuotedPrintableCodec.encodeText (PRINTABLE_QUOTED_PRINTABLE, sUnquotedString, CCharset.CHARSET_UTF_8_OBJ); } @Override @Nonnull @Nonempty public String getUnquotedString (@Nonnull @Nonempty final String sQuotedString) { return QuotedPrintableCodec.decodeText (sQuotedString, CCharset.CHARSET_UTF_8_OBJ); } }, /** * Create a URL escaped String. Replace all non-printable characters with %XY * where XY is the hex encoding of the char. Special note: space (ASCII 20) * should be escaped as "%20" and not as "+".
* Example: foo bar results in foo%20bar */ URL_ESCAPE { @Override @Nonnull @Nonempty public String getQuotedString (@Nonnull @Nonempty final String sUnquotedString) { // Use a special BitSet return URLCodec.encodeText (PRINTABLE_URL, sUnquotedString, CCharset.CHARSET_UTF_8_OBJ); } @Override @Nonnull @Nonempty public String getUnquotedString (@Nonnull @Nonempty final String sQuotedString) { return URLCodec.decodeText (sQuotedString, CCharset.CHARSET_UTF_8_OBJ); } }; public static final char QUOTED_STRING_SEPARATOR_CHAR = '"'; public static final char QUOTED_STRING_MASK_CHAR = '\\'; private static final BitSet PRINTABLE_QUOTED_PRINTABLE = QuotedPrintableCodec.getDefaultBitSet (); private static final BitSet PRINTABLE_URL = URLCodec.getDefaultBitSet (); static { // Modify BitSets PRINTABLE_QUOTED_PRINTABLE.set ('\t', false); PRINTABLE_QUOTED_PRINTABLE.set (' ', false); PRINTABLE_URL.set (' ', false); for (final char c : MimeTypeParser.getAllTSpecialChars ()) { PRINTABLE_QUOTED_PRINTABLE.set (c, false); PRINTABLE_URL.set (c, false); } } @Nonnull @Nonempty public abstract String getQuotedString (@Nonnull @Nonempty String sUnquotedString) throws EncoderException; @Nonnull @Nonempty public abstract String getUnquotedString (@Nonnull @Nonempty String sQuotedString) throws DecoderException; }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy