net.sf.okapi.lib.xliff2.renderer.XLIFFFragmentObject Maven / Gradle / Ivy
/*===========================================================================
Copyright (C) 2014 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.lib.xliff2.renderer;
import java.util.Map;
import java.util.Stack;
import net.sf.okapi.lib.xliff2.Const;
import net.sf.okapi.lib.xliff2.NSContext;
import net.sf.okapi.lib.xliff2.Util;
import net.sf.okapi.lib.xliff2.core.CTag;
import net.sf.okapi.lib.xliff2.core.Fragment;
import net.sf.okapi.lib.xliff2.core.MTag;
import net.sf.okapi.lib.xliff2.core.Tag;
import net.sf.okapi.lib.xliff2.core.TagType;
/**
* Implements {@link IFragmentObject} for XLIFF 2.
*/
public class XLIFFFragmentObject implements IFragmentObject {
private final Object obj;
private final Integer status;
private final Stack nsStack;
/**
* Creates a new fragment object for XLIFF.
* @param object the object to render.
* @param tagsStatus the map of the tags status.
* @param nsStack the namespace context.
*/
XLIFFFragmentObject (Object object,
Map tagsStatus,
Stack nsStack)
{
this.obj = object;
if ( obj instanceof String ) status = null;
else status = tagsStatus.get(obj);
this.nsStack = nsStack;
}
@Override
public String render () {
if ( obj instanceof String ) {
return render((String)obj);
}
else if ( obj instanceof CTag ) {
return render((CTag)obj);
}
else { // It's an MTag
return render((MTag)obj);
}
}
@Override
public String getText () {
return (String)obj;
}
@Override
public CTag getCTag () {
return (CTag)obj;
}
@Override
public MTag getMTag () {
return (MTag)obj;
}
@Override
public Object getObject () {
return obj;
}
private String render (String ctext) {
StringBuilder tmp = new StringBuilder();
for ( int i=0; i ");
i++;
}
else {
// In XML 1.0 the valid characters are:
// #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
switch ( ch ) {
case '\r':
tmp.append("
"); // Literal
break;
case '<':
tmp.append("<");
break;
case '&':
tmp.append("&");
break;
case '\n':
case '\t':
tmp.append(ch);
break;
default:
if (( ch > 0x001F ) && ( ch < 0xD800 )) {
// Valid char (most frequent)
tmp.append(ch);
}
else if ( Character.isHighSurrogate(ch) ) {
tmp.append(Character.toChars(ctext.codePointAt(i)));
i++;
}
else if (( ch < 0x0020 )
|| (( ch > 0xD7FF ) && ( ch < 0xE000 ))
|| ( ch == 0xFFFE )
|| ( ch == 0xFFFF )) {
// Invalid characters
tmp.append(String.format("", (int)ch));
}
break;
}
}
}
return tmp.toString();
}
private String render (CTag ctag) {
StringBuilder tmp = new StringBuilder();
switch ( ctag.getTagType() ) {
case OPENING:
if ( status == 2 ) {
tmp.append("");
else tmp.append("/>");
break;
case CLOSING:
if ( status == 2 ) {
tmp.append(" ");
}
else {
tmp.append(" ");
}
break;
case STANDALONE:
tmp.append(String.format(" ");
break;
}
return tmp.toString();
}
private String render (MTag mtag) {
StringBuilder tmp = new StringBuilder();
if ( mtag.getTagType() == TagType.OPENING ) {
if ( status == 2 ) {
tmp.append("(mtag.getId(), amAR));
// tmp.append(itsWriter.outputAttributes(mtag, amAR, annotRefs.get(annotRefs.size()-2).getValue()));
// Extension attributes
Fragment.printExtAttributes(mtag, tmp, nsStack);
// Closing
if ( status == 2 ) tmp.append(">");
else tmp.append("/>");
}
else { // Can only be CLOSING for MTag
if ( status == 2 ) {
tmp.append(" ");
}
else {
tmp.append("");
}
//TODO
// But we need to remove the annotator-references item (if one exists)
// if ( annotRefs != null ) {
// String id = mtag.getId();
// for ( SimpleEntry entry : annotRefs ) {
// if ( id.equals(entry.getKey()) ) {
// annotRefs.remove(entry);
// break;
// }
// }
// }
}
return tmp.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy