/**
 * The {@link EncodingAware} interface describes an {@link Object} aware of multiple encodings
 * existing withing the platform.
 *
 * @author <a href="http://could.it/">Pier Fumagalli</a>
 */
public interface EncodingAware {

  /** The default encoding is specified as being <code>UTF-8</code>. */
  public static final String DEFAULT_ENCODING = Encodings.get("UTF-8");

  /** The platform encoding is evaluated at runtime from the JVM. */
  public static final String PLATFORM_ENCODING = Encodings.get(null);
}
  /**
   * Creates an empty OutputProperties with the property key/value defaults specified by a property
   * file. The method argument is used to construct a string of the form output_[method].properties
   * (for instance, output_html.properties). The output_xml.properties file is always used as the
   * base.
   *
   * <p>Anything other than 'text', 'xml', and 'html', will use the output_xml.properties file.
   *
   * @param method non-null reference to method name.
   * @return Properties object that holds the defaults for the given method.
   */
  public static final Properties getDefaultMethodProperties(String method) {
    String fileName = null;
    Properties defaultProperties = null;
    // According to this article : Double-check locking does not work
    // http://www.javaworld.com/javaworld/jw-02-2001/jw-0209-toolbox.html
    try {
      synchronized (m_synch_object) {
        if (null == m_xml_properties) // double check
        {
          fileName = PROP_FILE_XML;
          m_xml_properties = loadPropertiesFile(fileName, null);
        }
      }

      if (method.equals(Method.XML)) {
        defaultProperties = m_xml_properties;
      } else if (method.equals(Method.HTML)) {
        if (null == m_html_properties) // double check
        {
          fileName = PROP_FILE_HTML;
          m_html_properties = loadPropertiesFile(fileName, m_xml_properties);
        }

        defaultProperties = m_html_properties;
      } else if (method.equals(Method.TEXT)) {
        if (null == m_text_properties) // double check
        {
          fileName = PROP_FILE_TEXT;
          m_text_properties = loadPropertiesFile(fileName, m_xml_properties);
          if (null == m_text_properties.getProperty(OutputKeys.ENCODING)) {
            String mimeEncoding = Encodings.getMimeEncoding(null);
            m_text_properties.put(OutputKeys.ENCODING, mimeEncoding);
          }
        }

        defaultProperties = m_text_properties;
      } else if (method.equals(com.sun.org.apache.xml.internal.serializer.Method.UNKNOWN)) {
        if (null == m_unknown_properties) // double check
        {
          fileName = PROP_FILE_UNKNOWN;
          m_unknown_properties = loadPropertiesFile(fileName, m_xml_properties);
        }

        defaultProperties = m_unknown_properties;
      } else {
        // TODO: Calculate res file from name.
        defaultProperties = m_xml_properties;
      }
    } catch (IOException ioe) {
      throw new WrappedRuntimeException(
          Utils.messages.createMessage(
              MsgKey.ER_COULD_NOT_LOAD_METHOD_PROPERTY, new Object[] {fileName, method}),
          ioe);
    }
    // wrap these cached defaultProperties in a new Property object just so
    // that the caller of this method can't modify the default values
    return new Properties(defaultProperties);
  }
Example #3
0
 public void updateTitle() {
   int enc = cc.lastServerEncoding;
   if (enc < 0) enc = cc.currentEncoding;
   if (enc == Encodings.encodingTight) {
     if (cc.opts.allowJpeg) {
       String[] subsampStr = {"1X", "4X", "2X", "Gray"};
       setTitle(
           cc.cp.name()
               + " [Tight + JPEG "
               + subsampStr[cc.opts.subsampling]
               + " Q"
               + cc.opts.quality
               + " + CL "
               + cc.opts.compressLevel
               + "]");
     } else {
       setTitle(cc.cp.name() + " [Lossless Tight" + " + CL " + cc.opts.compressLevel + "]");
     }
   } else {
     setTitle(cc.cp.name() + " [" + Encodings.encodingName(enc) + "]");
   }
 }
  /**
   * Normalize the characters, but don't escape. Different from SerializerToXML#writeNormalizedChars
   * because it does not attempt to do XML escaping at all.
   *
   * @param ch The characters from the XML document.
   * @param start The start position in the array.
   * @param length The number of characters to read from the array.
   * @param useLineSep true if the operating systems end-of-line separator should be output rather
   *     than a new-line character.
   * @throws IOException
   * @throws org.xml.sax.SAXException
   */
  void writeNormalizedChars(
      final char ch[], final int start, final int length, final boolean useLineSep)
      throws IOException, org.xml.sax.SAXException {
    final String encoding = getEncoding();
    final java.io.Writer writer = m_writer;
    final int end = start + length;

    /* copy a few "constants" before the loop for performance */
    final char S_LINEFEED = CharInfo.S_LINEFEED;

    // This for() loop always increments i by one at the end
    // of the loop.  Additional increments of i adjust for when
    // two input characters (a high/low UTF16 surrogate pair)
    // are processed.
    for (int i = start; i < end; i++) {
      final char c = ch[i];

      if (S_LINEFEED == c && useLineSep) {
        writer.write(m_lineSep, 0, m_lineSepLen);
        // one input char processed
      } else if (m_encodingInfo.isInEncoding(c)) {
        writer.write(c);
        // one input char processed
      } else if (Encodings.isHighUTF16Surrogate(c)) {
        final int codePoint = writeUTF16Surrogate(c, ch, i, end);
        if (codePoint != 0) {
          // I think we can just emit the message,
          // not crash and burn.
          final String integralValue = Integer.toString(codePoint);
          final String msg =
              Utils.messages.createMessage(
                  MsgKey.ER_ILLEGAL_CHARACTER, new Object[] {integralValue, encoding});

          // Older behavior was to throw the message,
          // but newer gentler behavior is to write a message to System.err
          // throw new SAXException(msg);
          System.err.println(msg);
        }
        i++; // two input chars processed
      } else {
        // Don't know what to do with this char, it is
        // not in the encoding and not a high char in
        // a surrogate pair, so write out as an entity ref
        if (encoding != null) {
          /* The output encoding is known,
           * so somthing is wrong.
           */

          // not in the encoding, so write out a character reference
          writer.write('&');
          writer.write('#');
          writer.write(Integer.toString(c));
          writer.write(';');

          // I think we can just emit the message,
          // not crash and burn.
          final String integralValue = Integer.toString(c);
          final String msg =
              Utils.messages.createMessage(
                  MsgKey.ER_ILLEGAL_CHARACTER, new Object[] {integralValue, encoding});

          // Older behavior was to throw the message,
          // but newer gentler behavior is to write a message to System.err
          // throw new SAXException(msg);
          System.err.println(msg);
        } else {
          /* The output encoding is not known,
           * so just write it out as-is.
           */
          writer.write(c);
        }

        // one input char was processed
      }
    }
  }