net.sf.okapi.common.encoder.OpenXMLEncoder Maven / Gradle / Ivy
/*===========================================================================
Copyright (C) 2008-2010 by the Okapi Framework contributors
-----------------------------------------------------------------------------
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 net.sf.okapi.common.encoder;
import java.nio.charset.CharsetEncoder;
import net.sf.okapi.common.IParameters;
/**
* Implements {@link IEncoder} for the Open XML format.
* This encoder encodes <, >, and &, but not " or '.
*/
public class OpenXMLEncoder implements IEncoder {
private String lineBreak;
/**
* Sets the options for this encoder. This encoder supports the following
* parameters:
*
- escapeGt=true to converts '>' characters to to
>
.
* - escapeNbsp=true to converts non-breaking space to
.
*
* @param params the parameters object with all the configuration information
* specific to this encoder.
* @param encoding the name of the charset encoding to use.
* @param lineBreak the type of line break to use.
*/
public void setOptions (IParameters params,
String encoding,
String lineBreak)
{
this.lineBreak = lineBreak;
// Use an encoder only if the output is not UTF-8/16
// since those support all characters
}
@Override
public void reset() { }
@Override
public String encode (String text,
EncoderContext context)
{
if ( text == null ) return "";
StringBuilder sbTmp = new StringBuilder(text.length());
char ch;
for ( int i=0; i':
sbTmp.append(">");
continue;
case '&':
sbTmp.append("&");
continue;
case '\n':
sbTmp.append(lineBreak);
break;
default:
if ( ch > 127 ) // Extended chars
{
if ( Character.isHighSurrogate(ch) )
{
int cp = text.codePointAt(i++);
String tmp = new String(Character.toChars(cp));
sbTmp.append(tmp);
}
else
sbTmp.append(ch);
}
else // ASCII chars
sbTmp.append(ch);
break;
}
}
return sbTmp.toString();
}
@Override
public String encode (char value,
EncoderContext context)
{
switch ( value ) {
case '<':
return "<";
case '&':
return "&";
case '>':
return ">";
case '\n':
return lineBreak;
default:
return String.valueOf(value);
}
}
@Override
public String encode (int value,
EncoderContext context)
{
switch ( value ) {
case '<':
return "<";
case '&':
return "&";
case '>':
return "<";
case '\n':
return lineBreak;
default:
if ( value > 127 ) { // Extended chars
if ( Character.isSupplementaryCodePoint(value) ) {
return new String(Character.toChars(value));
}
return String.valueOf((char)value);
}
else { // ASCII chars
return String.valueOf((char)value);
}
}
}
@Override
public String toNative (String propertyName,
String value)
{
// PROP_ENCODING: Same value in native
// PROP_LANGUGE: Same value in native
return value;
}
@Override
public String getLineBreak () {
return this.lineBreak;
}
@Override
public CharsetEncoder getCharsetEncoder () {
return null;
}
@Override
public IParameters getParameters() {
return null;
}
@Override
public String getEncoding() {
return "";
}
}