1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 /* 24 * Created on 2005 25 * by Christian Schmidt 26 */ 27 package com.sun.star.tooling.DirtyTags; 28 29 import java.io.IOException; 30 import java.util.ArrayList; 31 32 /** 33 * Solves the problem with translating strings from the 34 * Star-Office Help. This Strings contain XML snippets 35 * (that means parts of an xml dokument). I call them 'dirty' 36 * because the start of a tag (<) and the and of an tag (>) 37 * are quoted by a single backslash(\<.....\>). This is done 38 * because the text out of th tags should not contain '<' and '>' 39 * as Entity references (< or >) but as readable signs. 40 * This is for translation purposes. 41 * Because translators get mad while find out the really translatable 42 * parts between all the markup information, the XLIFF Specification 43 * allows to wrap parts of a String that should not be translated by 44 * special tags (<ept>, <bpt>). 45 * This Class has two static methods that do the wrapping and unwrapping 46 * NOTE: this won't work with not 'dirty' Strings. 47 * 48 * @author Christian Schmidt 2005 49 * 50 */ 51 public class DirtyTagWrapper { 52 53 private static boolean doWrap=true; setWrapping(boolean doWrap)54 public static void setWrapping(boolean doWrap){ 55 DirtyTagWrapper.doWrap=doWrap; 56 } 57 58 /** 59 * Unwraps the 'dirty' parts of a String from ept and bpt tags 60 * 61 * @param checkString The String to unwrap 62 * @return the unwrapped String 63 */ unwrapString(String checkString)64 public static String unwrapString(String checkString){ 65 //remove the ept and bpt tags 66 String[] splitted =checkString.split("(<ept ([^<>])*>)|(</ept>)|(<bpt ([^<>])*>)|(</bpt>)|(<sub([^<>])*>)|(</sub>)|(<ex ([^<>])*/>)"); 67 StringBuffer workBuffer= new StringBuffer(); 68 for(int i=0;i<splitted.length;i++){ 69 workBuffer.append(splitted[i]); 70 } 71 String string = new String(workBuffer); 72 //replace Entity references 73 string=string.replaceAll( "&","&").replaceAll( "<","<").replaceAll( ">",">").replaceAll( ""","\"").replaceAll( "'","'"); 74 75 //remove the nsub tags 76 splitted =string.split("(<sub([^<>])*>)|(</sub>)"); 77 StringBuffer returnBuffer= new StringBuffer(); 78 for(int i=0;i<splitted.length;i++){ 79 returnBuffer.append(splitted[i]); 80 } 81 String returnString = new String(returnBuffer); 82 return returnString; 83 } 84 85 86 /** 87 * Wrap the dirty parts of a string 88 * 89 * @param checkString The String to check if there are dirty Parts to wrap 90 * @return A String with wrapped dirty parts 91 * @throws TagWrapperException 92 * @throws IOException 93 */ wrapString(String checkString)94 public static String wrapString(String checkString) throws TagWrapperException, IOException { 95 // if no wrapping should be done return the given string 96 if(!doWrap) return checkString; 97 // let's wrap 98 String[] parts=null; 99 int idx=0; 100 //split the string at tag ends 101 String[] parts2 = checkString.split("\\\\>"); 102 103 ArrayList tagString =new ArrayList(); 104 // put the while splitting lost parts to the end of the single strings 105 for(int j=0;j<parts2.length-1;j++){ 106 parts2[j]+="\\>"; 107 } 108 // same for the last string 109 if (checkString.endsWith("\\>")){ 110 parts2[parts2.length-1]+="\\>"; 111 } 112 // split the leading text from the real tag string (<...>) 113 for(int j=0;j<parts2.length;j++){ 114 115 //is it just a tag 116 if(parts2[j].startsWith("\\<")){ 117 tagString.add(parts2[j]); 118 // or is it a tag with leading text? 119 }else if((idx=parts2[j].indexOf("\\<"))>0&&parts2[j].indexOf("\\>")>0){ 120 //...then split it in two parts 121 // the leading text 122 tagString.add(parts2[j].substring(0,(parts2[j].indexOf("\\<")))); 123 // ...and the tag 124 tagString.add(parts2[j].substring(parts2[j].indexOf("\\<"))); 125 126 }else{ 127 //no tag...must be text only 128 tagString.add(parts2[j]); 129 } 130 131 } 132 ArrayList tagNames=new ArrayList(); 133 String item=""; 134 for(int i=0;i<tagString.size();i++){ 135 item=((String)tagString.get(i)); 136 int start=item.indexOf("\\<")+2; 137 // check if we have an index that is ok 138 if(start==1) start=-1; 139 int end=item.lastIndexOf("\\>"); 140 if(start>=0&&end>0){ 141 boolean isStandalone=false; 142 if(item.endsWith("/\\>")){ 143 // this is a standalone tag 144 isStandalone=true; 145 } 146 item=item.substring(start,end); 147 148 if(item.indexOf(" ")>0){ 149 item=item.substring(0,item.indexOf(" ")); 150 } 151 if(isStandalone){ 152 item=item+"/"; 153 } 154 tagNames.add(item); 155 }else{ 156 tagNames.add(""); 157 } 158 } 159 ArrayList tagType=new ArrayList(); 160 for(int i=0;i<tagNames.size();i++){ 161 if(((String)tagNames.get(i)).equals("")){ 162 tagType.add("Text"); 163 }else if(((String)tagNames.get(i)).startsWith("/")){ 164 tagType.add("EndTag"); 165 }else if(((String)tagNames.get(i)).endsWith("/")){ 166 tagType.add("StartAndEndTag"); 167 }else { 168 tagType.add("StartTag"); 169 } 170 171 } 172 173 ArrayList tagList=new ArrayList(); 174 for(int i=0;i<tagNames.size();i++){ 175 tagList.add(new Tag( 176 (String)tagType.get(i), 177 (String)tagNames.get(i), 178 (String)tagString.get(i))); 179 } 180 tagType=null; 181 tagNames=null; 182 tagString=null; 183 184 TagPair start; 185 StringBuffer returnBuffer=new StringBuffer(); 186 while(tagList.size()>0){ 187 try{ 188 start=new TagPair(tagList); 189 returnBuffer.append(start.getWrapped()); 190 }catch(TagPair.TagPairConstructionException e){ 191 throw (new DirtyTagWrapper()).new TagWrapperException(e); 192 } 193 } 194 TagPair.resetCounter(); 195 return new String(returnBuffer); 196 } 197 /** 198 * @author Christian Schmidt 2005 199 * 200 */ 201 public class TagWrapperException extends Exception { 202 203 /** 204 * Create a new Instance of TagWrapperException 205 * 206 * 207 */ TagWrapperException()208 public TagWrapperException() { 209 super(); 210 // 211 } 212 213 /** 214 * Create a new Instance of TagWrapperException 215 * 216 * @param arg0 217 */ TagWrapperException(String arg0)218 public TagWrapperException(String arg0) { 219 super(arg0); 220 // 221 } 222 223 /** 224 * Create a new Instance of TagWrapperException 225 * 226 * @param arg0 227 * @param arg1 228 */ TagWrapperException(String arg0, Throwable arg1)229 public TagWrapperException(String arg0, Throwable arg1) { 230 super(arg0, arg1); 231 // 232 } 233 234 /** 235 * Create a new Instance of TagWrapperException 236 * 237 * @param arg0 238 */ TagWrapperException(Throwable arg0)239 public TagWrapperException(Throwable arg0) { 240 super(arg0); 241 // 242 } 243 244 } 245 } 246