summaryrefslogtreecommitdiff
path: root/transex3/java/l10nconv/java/com/sun/star/tooling/DirtyTags/TagPair.java
blob: 7993fc9cf0a4ae60a1bd0dcca449395d37aa0ec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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;

    }


}