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.Map; 31 32 import com.sun.star.tooling.converter.ExtMap; 33 34 /** 35 * @author Christian Schmidt 2005 36 * 37 */ 38 public class Tag { 39 private static int indent=0; 40 Map tagNames; 41 private String tagType; 42 private String tagName; 43 private String tagString; 44 public static Tag EMPTYTAG=new Tag("","",""); 45 46 /** 47 * Create a new Instance of Tag 48 * 49 * @param tagType 50 * @param tagName 51 * @param tagString 52 */ Tag(String tagType, String tagName, String tagString)53 public Tag(String tagType, String tagName, String tagString) { 54 55 this.tagType=tagType; 56 this.tagName=tagName; 57 this.tagString=tagString; 58 59 tagNames=new ExtMap(); 60 tagNames.put("link","name"); 61 tagNames.put("caption","xml-lang"); 62 tagNames.put("alt","xml-lang"); 63 } 64 getWrappedTagString()65 public String getWrappedTagString() throws IOException{ 66 if(this.canHaveTranslateableContent()){ 67 return this.wrapTagStringIntern(); 68 }else{ 69 return xmlString(this.tagString); 70 } 71 } 72 xmlString( final String string)73 private final String xmlString( final String string) throws java.io.IOException { 74 if (string == null) 75 return string; // "" 76 String str = string; 77 78 for(int i=0;i<str.length();i++){ 79 if(str.charAt(i)=='&'){ 80 str=str.substring(0, i)+"&"+str.substring(i+1); 81 continue; 82 } 83 84 if(str.charAt(i)=='<'){ 85 str=str.substring(0, i)+"<"+str.substring(i+1); 86 continue; 87 } 88 89 if(str.charAt(i)=='>'){ 90 str=str.substring(0, i)+">"+str.substring(i+1); 91 continue; 92 } 93 94 if(str.charAt(i)=='"'){ 95 str=str.substring(0, i)+"""+str.substring(i+1); 96 continue; 97 } 98 99 if(str.charAt(i)=='\''){ 100 str=str.substring(0, i)+"'"+str.substring(i+1); 101 continue; 102 } 103 } 104 105 return str; 106 } 107 /** 108 * @return 109 */ canHaveTranslateableContent()110 private boolean canHaveTranslateableContent() { 111 return (tagNames.containsKey(this.tagName)); 112 113 } 114 115 /** 116 * @throws IOException 117 * 118 */ wrapTagStringIntern()119 private String wrapTagStringIntern() throws IOException { 120 121 122 String[] split=this.tagString.split("="); 123 int length=split.length; 124 // no attribute found; 125 if (length==0) return xmlString(tagString); 126 else{ 127 int i=0; 128 129 while(i<length-1/*the last part can only contain an attribute value*/){ 130 String attributeName = split[i].trim(); 131 if(split[i]. indexOf("</sub>")<0) split[i]=xmlString(split[i]); 132 i++; 133 String value; 134 attributeName=(attributeName.substring(attributeName.lastIndexOf(" ")).trim()); 135 if((value=translateableAttributeValue(this.tagName)).equals(attributeName)){ 136 int valueStart=0; 137 int valueEnd=0; 138 139 // get the value to the found attribute name 140 // it must either be surrounded by '"'... 141 if((valueStart=split[i].indexOf('"'))>=0){ 142 valueEnd = split[i].lastIndexOf('"'); 143 //...or surrounded by "'" 144 }else if((valueStart=split[i].indexOf("'"))>=0){ 145 valueEnd = split[i].lastIndexOf("'"); 146 }else{ 147 // there seems to be an error, 148 // we found an '=' (we split there) but no '"' or ''' 149 // but although we don't check the syntax 150 // we just continue 151 continue; 152 } 153 //ok we found the border of a value that might be translated 154 //now we wrap it with the tags 155 156 split[i]=xmlString(split[i].substring(0,valueStart+1))+"<sub>"+xmlString(split[i].substring(valueStart+1,valueEnd))+"</sub>"+xmlString(split[i].substring(valueEnd)); 157 158 } 159 } 160 String wrappedString=""; 161 // we have the wrapped parts, now we put them together 162 int j=0; 163 for(j=0;j<split.length-1;j++){ 164 wrappedString+=(split[j]+"="); 165 } 166 wrappedString+=split[j]; 167 // System.out.println(this.tagString); 168 // System.out.println(wrappedString); 169 return wrappedString; 170 } 171 172 } 173 174 175 176 /** 177 * @param tagName the name of the tag to check 178 * @return the name of the attribute that can contain translateable value 179 */ translateableAttributeValue(String tagName)180 private String translateableAttributeValue(String tagName) { 181 182 return (String)this.tagNames.get(tagName); 183 } 184 185 186 /** 187 * Create a new Instance of Tag 188 * 189 * 190 */ Tag(String tagString)191 public Tag(String tagString) { 192 this(extractTagType(extractTagName(tagString)),extractTagName(tagString),tagString); 193 } 194 extractTagName(String tagString)195 private static String extractTagName(String tagString){ 196 197 int start=tagString.indexOf('<')+1; 198 int end=tagString.lastIndexOf('\\'); 199 if(start>=0&&end>0){ 200 tagString=tagString.substring(start,end); 201 202 if(tagString.indexOf(" ")>0){ 203 tagString=tagString.substring(0,tagString.indexOf(" ")); 204 } 205 return tagString; 206 }else{ 207 return ""; 208 } 209 } extractTagType(String tagName)210 private static String extractTagType(String tagName){ 211 if(tagName.equals("")){ 212 return "Text"; 213 }else if(tagName.startsWith("/")){ 214 return "EndTag"; 215 }else if(tagName.endsWith("/")){ 216 return "StartAndEndTag"; 217 }else { 218 return "StartTag"; 219 } 220 } 221 222 /** 223 * @return Returns the tagName. 224 */ getTagName()225 public String getTagName() { 226 return this.tagName; 227 } 228 /** 229 * @return Returns the tagString. 230 */ getTagString()231 public String getTagString() { 232 return this.tagString; 233 } 234 /** 235 * @return Returns the tagType. 236 */ getTagType()237 public String getTagType() { 238 return this.tagType; 239 } 240 241 242 } 243