summaryrefslogtreecommitdiff
path: root/l10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags
diff options
context:
space:
mode:
Diffstat (limited to 'l10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags')
-rwxr-xr-xl10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/DirtyTagWrapCheck.java85
-rwxr-xr-xl10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/DirtyTagWrapper.java252
-rwxr-xr-xl10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/Tag.java249
-rwxr-xr-xl10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/TagPair.java310
4 files changed, 896 insertions, 0 deletions
diff --git a/l10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/DirtyTagWrapCheck.java b/l10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/DirtyTagWrapCheck.java
new file mode 100755
index 000000000000..d304c2544bff
--- /dev/null
+++ b/l10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/DirtyTagWrapCheck.java
@@ -0,0 +1,85 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2008 by Sun Microsystems, Inc.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * $RCSfile: DirtyTagWrapCheck.java,v $
+ * $Revision: 1.3 $
+ *
+ * This file is part of OpenOffice.org.
+ *
+ * OpenOffice.org is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 3
+ * only, as published by the Free Software Foundation.
+ *
+ * OpenOffice.org is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License version 3 for more details
+ * (a copy is included in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * version 3 along with OpenOffice.org. If not, see
+ * <http://www.openoffice.org/license.html>
+ * for a copy of the LGPLv3 License.
+ *
+ ************************************************************************/
+/*
+ * Created on 2005
+ * by Christian Schmidt
+ */
+package com.sun.star.tooling.DirtyTags;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+
+
+
+public class DirtyTagWrapCheck {
+ static String line="";
+ public static void main(String[] args) {
+ try {
+ File fi = new File("D:\\Testfiles\\KID_helpcontent.sdf");//Copy of
+ FileReader fr = new FileReader(fi);
+ BufferedReader br = new BufferedReader(fr);
+
+ int readCounter=0;
+ int missCounter=0;
+ int lineErrorCounter=0;
+ while((line=br.readLine())!=null){
+ readCounter++;
+ String [] split = line.split("\t");
+ if(split.length<15){
+
+ lineErrorCounter++;
+ continue;
+ }
+ String string = split[10];
+ String wrapped = DirtyTagWrapper.wrapString(string);
+ String unwrapped=DirtyTagWrapper.unwrapString(wrapped);
+ if(!string.equals(unwrapped)){
+
+ missCounter++;
+ System.out.println(""+readCounter+"\n"+string+"\n"+unwrapped+"\n"+wrapped+"\n");
+ }
+ }
+ System.out.println("Fertig "+readCounter+" "+missCounter+" "+lineErrorCounter);
+ } catch (FileNotFoundException e) {
+ //
+ e.printStackTrace();
+ } catch (IOException e) {
+ //
+ e.printStackTrace();
+ } catch (DirtyTagWrapper.TagWrapperException e) {
+ System.out.println(e.getMessage()+"\n"+line+"\n");
+
+ }
+
+ }
+}
diff --git a/l10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/DirtyTagWrapper.java b/l10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/DirtyTagWrapper.java
new file mode 100755
index 000000000000..49fc08b69d32
--- /dev/null
+++ b/l10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/DirtyTagWrapper.java
@@ -0,0 +1,252 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2008 by Sun Microsystems, Inc.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * $RCSfile: DirtyTagWrapper.java,v $
+ * $Revision: 1.3 $
+ *
+ * This file is part of OpenOffice.org.
+ *
+ * OpenOffice.org is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 3
+ * only, as published by the Free Software Foundation.
+ *
+ * OpenOffice.org is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License version 3 for more details
+ * (a copy is included in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * version 3 along with OpenOffice.org. If not, see
+ * <http://www.openoffice.org/license.html>
+ * for a copy of the LGPLv3 License.
+ *
+ ************************************************************************/
+/*
+ * Created on 2005
+ * by Christian Schmidt
+ */
+package com.sun.star.tooling.DirtyTags;
+
+import java.io.IOException;
+import java.util.ArrayList;
+
+/**
+ * Solves the problem with translating strings from the
+ * Star-Office Help. This Strings contain XML snippets
+ * (that means parts of an xml dokument). I call them 'dirty'
+ * because the start of a tag (<) and the and of an tag (>)
+ * are quoted by a single backslash(\<.....\>). This is done
+ * because the text out of th tags should not contain '<' and '>'
+ * as Entity references (&lt; or &gt;) but as readable signs.
+ * This is for translation purposes.
+ * Because translators get mad while find out the really translatable
+ * parts between all the markup information, the XLIFF Specification
+ * allows to wrap parts of a String that should not be translated by
+ * special tags (<ept>, <bpt>).
+ * This Class has two static methods that do the wrapping and unwrapping
+ * NOTE: this won't work with not 'dirty' Strings.
+ *
+ * @author Christian Schmidt 2005
+ *
+ */
+public class DirtyTagWrapper {
+
+ private static boolean doWrap=true;
+ public static void setWrapping(boolean doWrap){
+ DirtyTagWrapper.doWrap=doWrap;
+ }
+
+ /**
+ * Unwraps the 'dirty' parts of a String from ept and bpt tags
+ *
+ * @param checkString The String to unwrap
+ * @return the unwrapped String
+ */
+ public static String unwrapString(String checkString){
+ //remove the ept and bpt tags
+ String[] splitted =checkString.split("(<ept ([^<>])*>)|(</ept>)|(<bpt ([^<>])*>)|(</bpt>)|(<sub([^<>])*>)|(</sub>)|(<ex ([^<>])*/>)");
+ StringBuffer workBuffer= new StringBuffer();
+ for(int i=0;i<splitted.length;i++){
+ workBuffer.append(splitted[i]);
+ }
+ String string = new String(workBuffer);
+ //replace Entity references
+ string=string.replaceAll( "&amp;","&").replaceAll( "&lt;","<").replaceAll( "&gt;",">").replaceAll( "&quot;","\"").replaceAll( "&apos;","'");
+
+ //remove the nsub tags
+ splitted =string.split("(<sub([^<>])*>)|(</sub>)");
+ StringBuffer returnBuffer= new StringBuffer();
+ for(int i=0;i<splitted.length;i++){
+ returnBuffer.append(splitted[i]);
+ }
+ String returnString = new String(returnBuffer);
+ return returnString;
+ }
+
+
+ /**
+ * Wrap the dirty parts of a string
+ *
+ * @param checkString The String to check if there are dirty Parts to wrap
+ * @return A String with wrapped dirty parts
+ * @throws TagWrapperException
+ * @throws IOException
+ */
+ public static String wrapString(String checkString) throws TagWrapperException, IOException {
+ // if no wrapping should be done return the given string
+ if(!doWrap) return checkString;
+ // let's wrap
+ String[] parts=null;
+ int idx=0;
+ //split the string at tag ends
+ String[] parts2 = checkString.split("\\\\>");
+
+ ArrayList tagString =new ArrayList();
+ // put the while splitting lost parts to the end of the single strings
+ for(int j=0;j<parts2.length-1;j++){
+ parts2[j]+="\\>";
+ }
+ // same for the last string
+ if (checkString.endsWith("\\>")){
+ parts2[parts2.length-1]+="\\>";
+ }
+ // split the leading text from the real tag string (<...>)
+ for(int j=0;j<parts2.length;j++){
+
+ //is it just a tag
+ if(parts2[j].startsWith("\\<")){
+ tagString.add(parts2[j]);
+ // or is it a tag with leading text?
+ }else if((idx=parts2[j].indexOf("\\<"))>0&&parts2[j].indexOf("\\>")>0){
+ //...then split it in two parts
+ // the leading text
+ tagString.add(parts2[j].substring(0,(parts2[j].indexOf("\\<"))));
+ // ...and the tag
+ tagString.add(parts2[j].substring(parts2[j].indexOf("\\<")));
+
+ }else{
+ //no tag...must be text only
+ tagString.add(parts2[j]);
+ }
+
+ }
+ ArrayList tagNames=new ArrayList();
+ String item="";
+ for(int i=0;i<tagString.size();i++){
+ item=((String)tagString.get(i));
+ int start=item.indexOf("\\<")+2;
+ // check if we have an index that is ok
+ if(start==1) start=-1;
+ int end=item.lastIndexOf("\\>");
+ if(start>=0&&end>0){
+ boolean isStandalone=false;
+ if(item.endsWith("/\\>")){
+ // this is a standalone tag
+ isStandalone=true;
+ }
+ item=item.substring(start,end);
+
+ if(item.indexOf(" ")>0){
+ item=item.substring(0,item.indexOf(" "));
+ }
+ if(isStandalone){
+ item=item+"/";
+ }
+ tagNames.add(item);
+ }else{
+ tagNames.add("");
+ }
+ }
+ ArrayList tagType=new ArrayList();
+ for(int i=0;i<tagNames.size();i++){
+ if(((String)tagNames.get(i)).equals("")){
+ tagType.add("Text");
+ }else if(((String)tagNames.get(i)).startsWith("/")){
+ tagType.add("EndTag");
+ }else if(((String)tagNames.get(i)).endsWith("/")){
+ tagType.add("StartAndEndTag");
+ }else {
+ tagType.add("StartTag");
+ }
+
+ }
+
+ ArrayList tagList=new ArrayList();
+ for(int i=0;i<tagNames.size();i++){
+ tagList.add(new Tag(
+ (String)tagType.get(i),
+ (String)tagNames.get(i),
+ (String)tagString.get(i)));
+ }
+ tagType=null;
+ tagNames=null;
+ tagString=null;
+
+ TagPair start;
+ StringBuffer returnBuffer=new StringBuffer();
+ while(tagList.size()>0){
+ try{
+ start=new TagPair(tagList);
+ returnBuffer.append(start.getWrapped());
+ }catch(TagPair.TagPairConstructionException e){
+ throw (new DirtyTagWrapper()).new TagWrapperException(e);
+ }
+ }
+ TagPair.resetCounter();
+ return new String(returnBuffer);
+ }
+ /**
+ * @author Christian Schmidt 2005
+ *
+ */
+ public class TagWrapperException extends Exception {
+
+ /**
+ * Create a new Instance of TagWrapperException
+ *
+ *
+ */
+ public TagWrapperException() {
+ super();
+ //
+ }
+
+ /**
+ * Create a new Instance of TagWrapperException
+ *
+ * @param arg0
+ */
+ public TagWrapperException(String arg0) {
+ super(arg0);
+ //
+ }
+
+ /**
+ * Create a new Instance of TagWrapperException
+ *
+ * @param arg0
+ * @param arg1
+ */
+ public TagWrapperException(String arg0, Throwable arg1) {
+ super(arg0, arg1);
+ //
+ }
+
+ /**
+ * Create a new Instance of TagWrapperException
+ *
+ * @param arg0
+ */
+ public TagWrapperException(Throwable arg0) {
+ super(arg0);
+ //
+ }
+
+ }
+}
diff --git a/l10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/Tag.java b/l10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/Tag.java
new file mode 100755
index 000000000000..7495736ab68c
--- /dev/null
+++ b/l10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/Tag.java
@@ -0,0 +1,249 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2008 by Sun Microsystems, Inc.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * $RCSfile: Tag.java,v $
+ * $Revision: 1.3 $
+ *
+ * This file is part of OpenOffice.org.
+ *
+ * OpenOffice.org is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 3
+ * only, as published by the Free Software Foundation.
+ *
+ * OpenOffice.org is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License version 3 for more details
+ * (a copy is included in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * version 3 along with OpenOffice.org. If not, see
+ * <http://www.openoffice.org/license.html>
+ * for a copy of the LGPLv3 License.
+ *
+ ************************************************************************/
+/*
+ * Created on 2005
+ * by Christian Schmidt
+ */
+package com.sun.star.tooling.DirtyTags;
+
+import java.io.IOException;
+import java.util.Map;
+
+import com.sun.star.tooling.converter.ExtMap;
+
+/**
+ * @author Christian Schmidt 2005
+ *
+ */
+public class Tag {
+ private static int indent=0;
+ Map tagNames;
+ private String tagType;
+ private String tagName;
+ private String tagString;
+ public static Tag EMPTYTAG=new Tag("","","");
+
+ /**
+ * Create a new Instance of Tag
+ *
+ * @param tagType
+ * @param tagName
+ * @param tagString
+ */
+ public Tag(String tagType, String tagName, String tagString) {
+
+ this.tagType=tagType;
+ this.tagName=tagName;
+ this.tagString=tagString;
+
+ tagNames=new ExtMap();
+ tagNames.put("link","name");
+ tagNames.put("caption","xml-lang");
+ tagNames.put("alt","xml-lang");
+ }
+
+ public String getWrappedTagString() throws IOException{
+ if(this.canHaveTranslateableContent()){
+ return this.wrapTagStringIntern();
+ }else{
+ return xmlString(this.tagString);
+ }
+ }
+
+ private final String xmlString( final String string) throws java.io.IOException {
+ if (string == null)
+ return string; // ""
+ String str = string;
+
+ for(int i=0;i<str.length();i++){
+ if(str.charAt(i)=='&'){
+ str=str.substring(0, i)+"&amp;"+str.substring(i+1);
+ continue;
+ }
+
+ if(str.charAt(i)=='<'){
+ str=str.substring(0, i)+"&lt;"+str.substring(i+1);
+ continue;
+ }
+
+ if(str.charAt(i)=='>'){
+ str=str.substring(0, i)+"&gt;"+str.substring(i+1);
+ continue;
+ }
+
+ if(str.charAt(i)=='"'){
+ str=str.substring(0, i)+"&quot;"+str.substring(i+1);
+ continue;
+ }
+
+ if(str.charAt(i)=='\''){
+ str=str.substring(0, i)+"&apos;"+str.substring(i+1);
+ continue;
+ }
+ }
+
+ return str;
+ }
+ /**
+ * @return
+ */
+ private boolean canHaveTranslateableContent() {
+ return (tagNames.containsKey(this.tagName));
+
+ }
+
+ /**
+ * @throws IOException
+ *
+ */
+ private String wrapTagStringIntern() throws IOException {
+
+
+ String[] split=this.tagString.split("=");
+ int length=split.length;
+ // no attribute found;
+ if (length==0) return xmlString(tagString);
+ else{
+ int i=0;
+
+ while(i<length-1/*the last part can only contain an attribute value*/){
+ String attributeName = split[i].trim();
+ if(split[i]. indexOf("</sub>")<0) split[i]=xmlString(split[i]);
+ i++;
+ String value;
+ attributeName=(attributeName.substring(attributeName.lastIndexOf(" ")).trim());
+ if((value=translateableAttributeValue(this.tagName)).equals(attributeName)){
+ int valueStart=0;
+ int valueEnd=0;
+
+ // get the value to the found attribute name
+ // it must either be surrounded by '"'...
+ if((valueStart=split[i].indexOf('"'))>=0){
+ valueEnd = split[i].lastIndexOf('"');
+ //...or surrounded by "'"
+ }else if((valueStart=split[i].indexOf("'"))>=0){
+ valueEnd = split[i].lastIndexOf("'");
+ }else{
+ // there seems to be an error,
+ // we found an '=' (we split there) but no '"' or '''
+ // but although we don't check the syntax
+ // we just continue
+ continue;
+ }
+ //ok we found the border of a value that might be translated
+ //now we wrap it with the tags
+
+ split[i]=xmlString(split[i].substring(0,valueStart+1))+"<sub>"+xmlString(split[i].substring(valueStart+1,valueEnd))+"</sub>"+xmlString(split[i].substring(valueEnd));
+
+ }
+ }
+ String wrappedString="";
+ // we have the wrapped parts, now we put them together
+ int j=0;
+ for(j=0;j<split.length-1;j++){
+ wrappedString+=(split[j]+"=");
+ }
+ wrappedString+=split[j];
+// System.out.println(this.tagString);
+// System.out.println(wrappedString);
+ return wrappedString;
+ }
+
+ }
+
+
+
+ /**
+ * @param tagName the name of the tag to check
+ * @return the name of the attribute that can contain translateable value
+ */
+ private String translateableAttributeValue(String tagName) {
+
+ return (String)this.tagNames.get(tagName);
+ }
+
+
+ /**
+ * Create a new Instance of Tag
+ *
+ *
+ */
+ public Tag(String tagString) {
+ this(extractTagType(extractTagName(tagString)),extractTagName(tagString),tagString);
+ }
+
+ private static String extractTagName(String tagString){
+
+ int start=tagString.indexOf('<')+1;
+ int end=tagString.lastIndexOf('\\');
+ if(start>=0&&end>0){
+ tagString=tagString.substring(start,end);
+
+ if(tagString.indexOf(" ")>0){
+ tagString=tagString.substring(0,tagString.indexOf(" "));
+ }
+ return tagString;
+ }else{
+ return "";
+ }
+ }
+ private static String extractTagType(String tagName){
+ if(tagName.equals("")){
+ return "Text";
+ }else if(tagName.startsWith("/")){
+ return "EndTag";
+ }else if(tagName.endsWith("/")){
+ return "StartAndEndTag";
+ }else {
+ return "StartTag";
+ }
+ }
+
+ /**
+ * @return Returns the tagName.
+ */
+ public String getTagName() {
+ return this.tagName;
+ }
+ /**
+ * @return Returns the tagString.
+ */
+ public String getTagString() {
+ return this.tagString;
+ }
+ /**
+ * @return Returns the tagType.
+ */
+ public String getTagType() {
+ return this.tagType;
+ }
+
+
+}
diff --git a/l10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/TagPair.java b/l10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/TagPair.java
new file mode 100755
index 000000000000..7993fc9cf0a4
--- /dev/null
+++ b/l10ntools/java/l10nconv/java/com/sun/star/tooling/DirtyTags/TagPair.java
@@ -0,0 +1,310 @@
+/*************************************************************************
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * Copyright 2008 by Sun Microsystems, Inc.
+ *
+ * OpenOffice.org - a multi-platform office productivity suite
+ *
+ * $RCSfile: TagPair.java,v $
+ * $Revision: 1.3 $
+ *
+ * This file is part of OpenOffice.org.
+ *
+ * OpenOffice.org is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 3
+ * only, as published by the Free Software Foundation.
+ *
+ * OpenOffice.org is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License version 3 for more details
+ * (a copy is included in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * version 3 along with OpenOffice.org. If not, see
+ * <http://www.openoffice.org/license.html>
+ * for a copy of the LGPLv3 License.
+ *
+ ************************************************************************/
+/*
+ * Created on 2005
+ * by Christian Schmidt
+ */
+package com.sun.star.tooling.DirtyTags;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+
+
+/**
+ * @author Christian Schmidt 2005
+ *
+ */
+public class TagPair {
+
+
+ private Tag startTag=Tag.EMPTYTAG;
+ private Tag endTag=Tag.EMPTYTAG;
+ private String startingText="";
+ private ArrayList enclosedTags=new ArrayList();
+ private long id;
+ private static int ElementCounter=1;
+ private String endingText="";
+
+ /**
+ * @author Christian Schmidt 2005
+ *
+ */
+ public class TagPairConstructionException extends Exception {
+
+ /**
+ * Create a new Instance of TagPairConstructionException
+ *
+ * @param string
+ */
+ public TagPairConstructionException(String string) {
+
+ }
+
+ }
+
+ /**
+ * Create a new Instance of TagPair
+ *
+ *
+ */
+ public TagPair() {
+
+ }
+
+
+ /**
+ * Create a new Instance of TagPair
+ *
+ * Find matching tags in tagList, create a TagPair of it, create
+ * tagPairs from the content in the tagPair and remove all used
+ * tags from tagList. The rest of the tagList starts after the
+ * endTag of this TagPair.
+ *
+ * @param tagList a List of the tags to check
+ *
+ * @throws TagPairConstructionException
+ */
+ public TagPair(ArrayList tagList) throws TagPairConstructionException {
+
+ if(tagList.size()==0){
+ return;
+ }
+ ArrayList contentList=new ArrayList();;
+ Tag tag=(Tag)tagList.get(0);
+ tagList.remove(0);
+
+
+ if("Text".equals(tag.getTagType())){
+ // is this Text the only content
+ // of this Tag ?
+ if(tagList.size()==0){
+ //yes...then it is the starting Text of this TagPair
+ this.startingText=tag.getTagString();
+ return;
+ }else{
+ //no...the tag is normal content
+ contentList.add(tag);
+ }
+ this.startingText=tag.getTagString();
+
+ }else if("EndTag".equals(tag.getTagType())){
+ //ERRor throw EXception
+ }else if("StartTag".equals(tag.getTagType())){
+ // find the matching end tag
+ this.startTag=tag;
+ Iterator iter=tagList.iterator();
+
+ int equivalentTagCounter=0;
+ while(iter.hasNext()){
+ //is this the end tag?
+ if((tag=(Tag)iter.next()).getTagName().equals('/'+this.startTag.getTagName())&&equivalentTagCounter==0){
+ //found the corresponding end tag
+
+ //this TagPair is complete
+ //so it needs an id
+ this.id=TagPair.ElementCounter++;
+ this.endTag=tag;
+ //...remove it from list
+ tagList.removeAll(contentList);
+ tagList.remove(tag);
+ break;
+ }else{
+ // tag is not the end tag
+ // so it is between the start and the end tag
+ // and belongs to the content
+ // but first check if it has the same name as the current tag
+ if(tag.getTagName().equals(this.startTag.getTagName())){
+ // if this is a start tag like the current start tag
+ // we count it to find out the matching end tag in nested tags
+ if(tag.getTagType().equals("StartTag")){
+ equivalentTagCounter++;
+ }
+ }
+ if(tag.getTagName().equals("/"+this.startTag.getTagName())){
+ if(tag.getTagType().equals("EndTag")){
+ equivalentTagCounter--;
+ }
+ }
+
+ contentList.add(tag);
+ }
+ }
+ //found the end tag ?
+ //no...
+ if (this.endTag.getTagType()==""){
+
+ throw new TagPairConstructionException("ERROR: Missing end tag ("+
+ this.startTag.getTagString()+").");
+ //...yes
+ }else{
+ //We need to check whether the content is starting or ending with text
+ //...check starting with text
+ if(contentList.size()>=1&&((String)((Tag)contentList.get(0)).getTagType()).equals("Text")){
+ //yes...store it as startingText
+ this.startingText=(String)((Tag)contentList.get(0)).getTagString();
+ //remove it from list
+ contentList.remove(0);
+ }
+ // ...check ending with text
+ if(contentList.size()>=1&&((String)((Tag)contentList.get(contentList.size()-1)).getTagType()).equals("Text")){
+ //yes...store it as endingText
+ this.endingText=(String)((Tag)contentList.get(contentList.size()-1)).getTagString();
+ //remove it from list
+ contentList.remove(contentList.size()-1);
+ }
+ //create the list of tags enclosed by this tagPair
+ createEnclosedTags(contentList);
+ }
+ //if stand AloneTag create own TagObject...give ID...add to List
+ }else if("StartAndEndTag".equals(tag.getTagType())){
+ this.startTag=tag;
+ this.endTag=new Tag("EndOfStandAlone","","");
+ createEnclosedTags(contentList);
+ }
+
+ }
+
+ /**
+ * @param contentList
+ * @throws TagPairConstructionException
+ */
+ private void createEnclosedTags(ArrayList contentList) throws TagPairConstructionException {
+ while(contentList.size()>0){
+ //create the inner TagPairs
+ this.enclosedTags.add(new TagPair(contentList));
+ }
+
+ }
+
+ public String toString(){
+ StringBuffer outString= new StringBuffer(this.startTag.toString());
+ TagPair help=new TagPair();
+ Iterator iter=enclosedTags.iterator();
+ outString.append(this.startingText);
+ while(iter.hasNext()){
+ if((help=(TagPair)iter.next())==null){
+ continue;
+ }else{
+ outString.append(help.toString());
+ }
+ }
+ outString.append(this.endingText);
+ outString.append(this.endTag.toString());
+ return new String(outString);
+ }
+
+ public String getWrapped() throws IOException{
+ Iterator iter=enclosedTags.iterator();
+ StringBuffer returnBuffer=new StringBuffer();
+
+ returnBuffer.append(wrap(this.startTag)+xmlString(this.startingText));
+ while(iter.hasNext()){
+ returnBuffer.append(((TagPair)iter.next()).getWrapped());
+ }
+ returnBuffer.append(xmlString(this.endingText)+wrap(this.endTag));
+
+
+
+ return new String(returnBuffer);
+ }
+
+ private String wrap(Tag tag) throws IOException{
+ String string="";
+ //can be a start tag
+ if(tag.getTagType().startsWith("Start")){
+ return new String("<bpt id='"+this.id+"'>"+tag.getWrappedTagString()+"</bpt>");
+ //...or a end tag
+ }else if (tag.getTagType().startsWith("End")){
+ //maybe the end tag of a Start and end tag
+// if("EndOfStandAlone".equals(tag.getTagType())){
+// return new String("<ex id='"+this.id+"'/>");
+// }else{
+ string=tag.getWrappedTagString();
+ return new String("<ept id='"+this.id+"'>"+string+"</ept>");
+// }
+
+ //...or text
+ }else{
+ return xmlString(tag.getTagString());
+ }
+ }
+ /**
+ * Replaces all characters that mustn't be in XLIFF PCdata
+ *
+ * @param string the string to check
+ * @return the checked string with all characters replaced
+ * @throws java.io.IOException
+ */
+ private final String xmlString( final String string) throws java.io.IOException {
+ if (string == null)
+ return string; // ""
+ String str = string;
+
+ for(int i=0;i<str.length();i++){
+ if(str.charAt(i)=='&'){
+ str=str.substring(0, i)+"&amp;"+str.substring(i+1);
+ continue;
+ }
+
+ if(str.charAt(i)=='<'){
+ str=str.substring(0, i)+"&lt;"+str.substring(i+1);
+ continue;
+ }
+
+ if(str.charAt(i)=='>'){
+ str=str.substring(0, i)+"&gt;"+str.substring(i+1);
+ continue;
+ }
+
+ if(str.charAt(i)=='"'){
+ str=str.substring(0, i)+"&quot;"+str.substring(i+1);
+ continue;
+ }
+
+ if(str.charAt(i)=='\''){
+ str=str.substring(0, i)+"&apos;"+str.substring(i+1);
+ continue;
+ }
+ }
+
+ return str;
+ }
+
+ /**
+ *
+ */
+ public static void resetCounter() {
+ TagPair.ElementCounter=1;
+
+ }
+
+
+}