summaryrefslogtreecommitdiff
path: root/core/source/org/openoffice/ide/eclipse/core/model/description/DescriptionModel.java
blob: e4bb50b80791cc19874f077c87bad5e503a1b43f (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
/*************************************************************************
 *
 * The Contents of this file are made available subject to the terms of
 * the GNU Lesser General Public License Version 2.1
 *
 * GNU Lesser General Public License Version 2.1
 * =============================================
 * Copyright 2009 by Novell, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software Foundation.
 *
 * This library 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 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 * 
 * The Initial Developer of the Original Code is: Cédric Bosdonnat.
 *
 * Copyright: 2009 by Novell, Inc.
 *
 * All Rights Reserved.
 * 
 ************************************************************************/
package org.openoffice.ide.eclipse.core.model.description;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Set;
import java.util.Map.Entry;

import org.openoffice.ide.eclipse.core.model.utils.IModel;
import org.openoffice.ide.eclipse.core.model.utils.IModelChangedListener;
import org.openoffice.plugin.core.utils.XMLWriter;

/**
 * Class representing the description.xml file.
 * 
 * @author Cédric Bosdonnat
 *
 */
public class DescriptionModel implements IModel {

    private ArrayList< IModelChangedListener > mListeners;
    private boolean mDirty;
    private boolean mSuspendEvents;
    
    private String mId = new String( );
    private String mVersion = new String( );
    private HashMap<Locale, String> mDisplayNames;
    
    private String mMinOOo = new String( );
    private String mMaxOOo = new String( );
    private String mPlatforms = new String( );
    
    private String mDefaultIcon = new String( );
    private String mHCIcon = new String( );

    private HashMap<Locale, String> mDescriptions;
    
    private HashMap<Locale, String> mReleaseNotes;
    private ArrayList<String> mUpdateInfos;
    
    private boolean mAcceptByUser = false;
    private boolean mSuppressOnUpdate = false;
    private HashMap<Locale, String> mLicenses;
    
    private HashMap<Locale, PublisherInfos> mPublisherInfos;
    
    /**
     * Default constructor.
     */
    public DescriptionModel( ) {
        mListeners = new ArrayList< IModelChangedListener >();
        
        mDisplayNames = new HashMap<Locale, String>( );
        mDescriptions = new HashMap<Locale, String>( );
        mReleaseNotes = new HashMap<Locale, String>( );
        mUpdateInfos = new ArrayList<String>( );
        mLicenses = new HashMap<Locale, String>( );
        mPublisherInfos = new HashMap<Locale, PublisherInfos>( );
    }
    
    /**
     * {@inheritDoc}
     */
    public void addListener( IModelChangedListener pListener ) {
        mListeners.add( pListener );
    }
    
    /**
     * {@inheritDoc}
     */
    public void removeListener( IModelChangedListener pListener ) {
        mListeners.remove( pListener );
    }
    
    /**
     * {@inheritDoc}
     */
    public boolean isDirty() {
        return mDirty;
    }
    
    /**
     * {@inheritDoc}
     */
    public void setSuspendEvent(boolean pSuspend) {
        mSuspendEvents = pSuspend;
    }
    
    /**
     * @return all the locales defined in the different parts of the model.
     */
    public ArrayList<Locale> getAllLocales( ) {        
        ArrayList<Locale> locales = new ArrayList<Locale>( );
        
        appendNew( locales, mDisplayNames.keySet() );
        appendNew( locales, mDescriptions.keySet() );
        appendNew( locales, mReleaseNotes.keySet() );
        appendNew( locales, mLicenses.keySet() );
        appendNew( locales, mPublisherInfos.keySet() );
        
        return locales;
    }
    
    /**
     * @return the id
     */
    public String getId() {
        return mId;
    }

    /**
     * @param pId the id to set
     */
    public void setId(String pId) {
        if ( mId != pId ) {
            mId = pId;
            fireModelChanged();
        }
    }

    /**
     * @return the version
     */
    public String getVersion() {
        return mVersion;
    }

    /**
     * @param pVersion the version to set
     */
    public void setVersion(String pVersion) {
        if ( mVersion != pVersion ) {
            mVersion = pVersion;
            fireModelChanged();
        }
    }

    /**
     * @return the displayNames
     */
    public HashMap<Locale, String> getDisplayNames() {
        if ( mDisplayNames == null ) {
            mDisplayNames = new HashMap<Locale, String>();
        }
        return mDisplayNames;
    }
    
    /**
     * Add or update a display name.
     * 
     * @param pLocale the locale for the display name to add / update
     * @param pValue the value to add / update.
     */
    public void addDisplayName( Locale pLocale, String pValue ) {
        if ( getDisplayNames().containsKey( pLocale ) && 
                !getDisplayNames().get( pLocale ).equals( pValue ) ||
                !getDisplayNames().containsKey( pLocale ) ) {
            getDisplayNames().put( pLocale, pValue);
            fireModelChanged();
        }
    }
    
    /**
     * Remove a display name.
     * 
     * @param pLocale the locale of the display name to remove
     */
    public void removeDisplayName( Locale pLocale ) {
        if ( getDisplayNames().containsKey( pLocale ) ) {
            getDisplayNames().remove( pLocale );
            fireModelChanged();
        }
    }

    /**
     * @return the minOOo
     */
    public String getMinOOo() {
        return mMinOOo;
    }

    /**
     * @param pMinOOo the minOOo to set
     */
    public void setMinOOo(String pMinOOo) {
        if ( mMinOOo != pMinOOo ) {
            mMinOOo = pMinOOo;
            fireModelChanged();
        }
    }

    /**
     * @return the maxOOo
     */
    public String getMaxOOo() {
        return mMaxOOo;
    }

    /**
     * @param pMaxOOo the maxOOo to set
     */
    public void setMaxOOo(String pMaxOOo) {
        if ( mMaxOOo != pMaxOOo ) {
            mMaxOOo = pMaxOOo;
            fireModelChanged();
        }
    }

    /**
     * @return the platforms
     */
    public String getPlatforms() {
        return mPlatforms;
    }

    /**
     * @param pPlatforms the platforms to set
     */
    public void setPlatforms(String pPlatforms) {
        if ( mPlatforms != pPlatforms ) {
            mPlatforms = pPlatforms;
            fireModelChanged();
        }
    }

    /**
     * @return the defaultIcon
     */
    public String getDefaultIcon() {
        return mDefaultIcon;
    }

    /**
     * @param pDefaultIcon the defaultIcon to set
     */
    public void setDefaultIcon(String pDefaultIcon) {
        if ( mDefaultIcon != pDefaultIcon ) {
            mDefaultIcon = pDefaultIcon;
            fireModelChanged();
        }
    }

    /**
     * @return the hCIcon
     */
    public String getHCIcon() {
        return mHCIcon;
    }

    /**
     * @param pHCIcon the hCIcon to set
     */
    public void setHCIcon(String pHCIcon) {
        if ( mHCIcon != pHCIcon ) {
            mHCIcon = pHCIcon;
            fireModelChanged();
        }
    }

    /**
     * @return the descriptions
     */
    public HashMap<Locale, String> getDescriptions() {
        if ( mDescriptions == null ) {
            mDescriptions = new HashMap<Locale, String>();
        }
        return mDescriptions;
    }
    
    /**
     * Add or update a description.
     * 
     * @param pLocale the locale for the description to add / update
     * @param pValue the value to add / update.
     */
    public void addDescription( Locale pLocale, String pValue ) {
        if ( getDescriptions().containsKey( pLocale ) && 
                !getDescriptions().get( pLocale ).equals( pValue ) ||
                !getDescriptions().containsKey( pLocale ) ) {
            getDescriptions().put( pLocale, pValue);
            fireModelChanged();
        }
    }
    
    /**
     * Remove a description.
     * 
     * @param pLocale the locale of the description to remove
     */
    public void removeDescription( Locale pLocale ) {
        if ( getDescriptions().containsKey( pLocale ) ) {
            getDescriptions().remove( pLocale );
            fireModelChanged();
        }
    }

    /**
     * @return the releaseNotes
     */
    public HashMap<Locale, String> getReleaseNotes() {
        if ( mReleaseNotes == null ) {
            mReleaseNotes = new HashMap<Locale, String>();
        }
        return mReleaseNotes;
    }
    
    /**
     * Add or update a release note.
     * 
     * @param pLocale the locale for the release note to add / update
     * @param pValue the value to add / update.
     */
    public void addReleaseNote( Locale pLocale, String pValue ) {
        if ( getReleaseNotes().containsKey( pLocale ) && 
                !getReleaseNotes().get( pLocale ).equals( pValue ) ||
                !getReleaseNotes().containsKey( pLocale ) ) {
            getReleaseNotes().put( pLocale, pValue);
            fireModelChanged();
        }
    }
    
    /**
     * Remove a release note.
     * 
     * @param pLocale the locale of the release note to remove
     */
    public void removeReleaseNote( Locale pLocale ) {
        if ( getReleaseNotes().containsKey( pLocale ) ) {
            getReleaseNotes().remove( pLocale );
            fireModelChanged();
        }
    }

    /**
     * @return the updateInfos
     */
    public ArrayList<String> getUpdateInfos() {
        if ( mUpdateInfos == null ) {
            mUpdateInfos = new ArrayList<String>();
        }
        return mUpdateInfos;
    }
    
    /**
     * Add or update a updateInfos.
     * 
     * @param pValue the value to add / update.
     */
    public void addUpdateInfo( String pValue ) {
        if ( !getUpdateInfos().contains( pValue ) ) {
            getUpdateInfos().add( pValue );
            fireModelChanged();
        }
    }
    
    /**
     * Replace an existing update info.
     * 
     * @param pIndex the index to change
     * @param pValue the new value
     */
    public void replaceUpdateInfo( int pIndex, String pValue ) {
        String o = getUpdateInfos().get( pIndex );
        if ( o != null && !o.equals( pValue ) ) {
            getUpdateInfos().set( pIndex, pValue );
            fireModelChanged();
        }
    }
    
    /**
     * Remove a updateInfos.
     * 
     * @param pValue the value to remove
     */
    public void removeUpdateInfo( String pValue ) {
        if ( getUpdateInfos().contains( pValue ) ) {
            getUpdateInfos().remove( pValue );
            fireModelChanged();
        }
    }

    /**
     * @return the acceptByUser
     */
    public boolean isAcceptByUser() {
        return mAcceptByUser;
    }

    /**
     * @param pAcceptByUser the acceptByUser to set
     */
    public void setAcceptByUser(boolean pAcceptByUser) {
        if ( mAcceptByUser != pAcceptByUser ) {
            mAcceptByUser = pAcceptByUser;
            fireModelChanged();
        }
    }

    /**
     * @return the suppressOnUpdate
     */
    public boolean isSuppressOnUpdate() {
        return mSuppressOnUpdate;
    }

    /**
     * @param pSuppressOnUpdate the suppressOnUpdate to set
     */
    public void setSuppressOnUpdate(boolean pSuppressOnUpdate) {
        if ( mSuppressOnUpdate != pSuppressOnUpdate ) {
            mSuppressOnUpdate = pSuppressOnUpdate;
            fireModelChanged();
        }
    }

    /**
     * @return the licenses
     */
    public HashMap<Locale, String> getLicenses() {
        if ( mLicenses == null ) {
            mLicenses = new HashMap<Locale, String>();
        }
        return mLicenses;
    }
    
    /**
     * Add or update a license.
     * 
     * @param pLocale the locale for the license to add / update
     * @param pValue the value to add / update.
     */
    public void addLicense( Locale pLocale, String pValue ) {
        if ( getLicenses().containsKey( pLocale ) && 
                !getLicenses().get( pLocale ).equals( pValue ) ||
                !getLicenses().containsKey( pLocale ) ) {
            getLicenses().put( pLocale, pValue);
            fireModelChanged();
        }
    }
    
    /**
     * Remove a license.
     * 
     * @param pLocale the locale of the license to remove
     */
    public void removeLicense( Locale pLocale ) {
        if ( getLicenses().containsKey( pLocale ) ) {
            getLicenses().remove( pLocale );
            fireModelChanged();
        }
    }

    /**
     * @return the publisherInfos
     */
    public HashMap<Locale, PublisherInfos> getPublisherInfos() {
        if ( mPublisherInfos == null ) {
            mPublisherInfos = new HashMap<Locale, PublisherInfos>();
        }
        return mPublisherInfos;
    }

    /**
     * Add or update a publisherInfo.
     * 
     * @param pLocale the locale for the publisherInfo to add / update
     * @param pValue the value to add / update.
     */
    public void addPublisherInfo( Locale pLocale, PublisherInfos pValue ) {
        if ( getPublisherInfos().containsKey( pLocale ) && 
                !getPublisherInfos().get( pLocale ).equals( pValue ) ||
                !getPublisherInfos().containsKey( pLocale ) ) {
            pValue.setModel( this );
            getPublisherInfos().put( pLocale, pValue);
            fireModelChanged();
        }
    }
    
    /**
     * Remove a publisherInfo.
     * 
     * @param pLocale the locale of the publisherInfo to remove
     */
    public void removePublisherInfo( Locale pLocale ) {
        if ( getLicenses().containsKey( pLocale ) ) {
            getPublisherInfos().remove( pLocale );
            fireModelChanged();
        }
    }

    /**
     * Serializes the data in XML to an output stream.
     * 
     * @param pOut the output stream where to write the data
     */
    public void serialize( OutputStream pOut ) {
        XMLWriter writer = null;
        try {
            writer = new XMLWriter( pOut );
            
            HashMap<String, String> mapping = new HashMap<String, String>( );
            mapping.put( XMLTokens.ATTR_XMLNS, XMLTokens.URI_DESCRIPTION );
            mapping.put( XMLTokens.createQName( XMLTokens.ATTR_XMLNS, XMLTokens.PREFIX_DESCRIPTION ), 
                    XMLTokens.URI_DESCRIPTION );
            mapping.put( XMLTokens.createQName( XMLTokens.ATTR_XMLNS, XMLTokens.PREFIX_XLINK), 
                    XMLTokens.URI_XLINK );
            writer.startTag( XMLTokens.ELEMENT_DESCRIPTION, mapping );
            
            // Write the version element
            printValueElement( writer, XMLTokens.ELEMENT_VERSION, mVersion );
            printValueElement( writer, XMLTokens.ELEMENT_IDENTIFIER, mId );
            printValueElement( writer, XMLTokens.ELEMENT_PLATFORM, mPlatforms );
            
            writeDependencies( writer );
            writeUpdateInfos( writer );
            writeLicenses( writer );
            writePublisherInfos( writer );
            writeReleaseNotes( writer );
            writeDisplayNames( writer );
            writeIcons( writer );
            writeDescriptions( writer );
            
            writer.endTag( XMLTokens.ELEMENT_DESCRIPTION );
            
        } catch (UnsupportedEncodingException e) {
            // Should never happen
        } finally {
            writer.close();
        }
        
        fireModelSaved();
    }

    /**
     * Write the dependencies element and its children.
     * 
     * @param pWriter the XML writer
     */
    private void writeDependencies(XMLWriter pWriter) {
        
        boolean hasMin = !(0 == mMinOOo.trim().length());
        boolean hasMax = !(0 == mMaxOOo.trim().length());
        if ( hasMin || hasMax ) {
            pWriter.startTag( XMLTokens.ELEMENT_DEPENDENCIES, null );

            if ( hasMin ) {
                HashMap<String, String> attrs = new HashMap<String, String>( );
                attrs.put( XMLTokens.ATTR_VALUE, mMinOOo.trim() );
                attrs.put( XMLTokens.createQName(XMLTokens.PREFIX_DESCRIPTION, XMLTokens.ELEMENT_NAME ),
                        "OpenOffice.org " + mMinOOo.trim() ); //$NON-NLS-1$
                pWriter.printSingleTag( XMLTokens.ELEMENT_OOO_MIN, attrs );
            }

            if ( hasMax ) {
                HashMap<String, String> attrs = new HashMap<String, String>( );
                attrs.put( XMLTokens.ATTR_VALUE, mMaxOOo.trim() );
                attrs.put( XMLTokens.createQName(XMLTokens.PREFIX_DESCRIPTION, XMLTokens.ELEMENT_NAME ),
                        "OpenOffice.org " + mMaxOOo.trim( ) ); //$NON-NLS-1$
                pWriter.printSingleTag( XMLTokens.ELEMENT_OOO_MAX, attrs );
            }
            
            pWriter.endTag( XMLTokens.ELEMENT_DEPENDENCIES );
        }
    }
    
    /**
     * Write the update-information element and its children.
     * 
     * @param pWriter the XML writer
     */
    private void writeUpdateInfos(XMLWriter pWriter) {
        if ( mUpdateInfos.size() > 0 ) {
            pWriter.startTag( XMLTokens.ELEMENT_UPDATE_INFORMATION, null );
            
            HashMap<String, String> attrs = new HashMap<String, String>( );
            for (String mirror : mUpdateInfos) {
                attrs.clear();
                attrs.put( XMLTokens.createQName( XMLTokens.PREFIX_XLINK, XMLTokens.ATTR_HREF ),
                        mirror.trim( ) );
                pWriter.printSingleTag( XMLTokens.ELEMENT_SRC, attrs );
            }
            
            pWriter.endTag( XMLTokens.ELEMENT_UPDATE_INFORMATION );
        }
    }
    
    /**
     * Write the registration element and its children.
     * 
     * @param pWriter the XML writer
     */
    private void writeLicenses(XMLWriter pWriter) {
        
        // Check the presence of a license
        boolean hasLicenses = false;
        Iterator<String> i = mLicenses.values().iterator();
        while ( !hasLicenses && i.hasNext( ) ) {
            String value = i.next();
            hasLicenses |= !(0 == value.trim().length());
        }
        
        //Write the block
        if ( hasLicenses ) {
            pWriter.startTag( XMLTokens.ELEMENT_REGISTRATION, null );
            
            HashMap<String, String> attrs = new HashMap<String, String>( );
            String acceptLevel = "admin"; //$NON-NLS-1$
            if ( mAcceptByUser ) {
                acceptLevel = "user"; //$NON-NLS-1$
            }
            attrs.put( XMLTokens.ATTR_ACCEPT_BY, acceptLevel );
            if ( mSuppressOnUpdate ) {
                attrs.put( XMLTokens.ATTR_SUPPRESS_ON_UPDATE, Boolean.toString( mSuppressOnUpdate ) );
            }
            pWriter.startTag( XMLTokens.ELEMENT_SIMPLE_LICENSE, attrs );
            
            Iterator<Entry<Locale, String>> iter = mLicenses.entrySet().iterator();
            while ( iter.hasNext() ) {
                Entry<Locale, String> entry = iter.next();
                String locale = writeLocale( entry.getKey() );
                attrs.clear();
                attrs.put( XMLTokens.createQName( XMLTokens.PREFIX_XLINK, XMLTokens.ATTR_HREF ),
                        entry.getValue().trim( ) );
                attrs.put( XMLTokens.ATTR_LANG, locale );
                pWriter.printSingleTag( XMLTokens.ELEMENT_LICENSE_TEXT, attrs );
            }
            
            pWriter.endTag( XMLTokens.ELEMENT_SIMPLE_LICENSE );
            pWriter.endTag( XMLTokens.ELEMENT_REGISTRATION );
        }
    }
    
    /**
     * Write the publisher element and its children.
     * 
     * @param pWriter the XML writer
     */
    private void writePublisherInfos(XMLWriter pWriter) {
        boolean hasInfos = false;
        // Check the presence of an information
        Iterator<PublisherInfos> i = mPublisherInfos.values().iterator();
        while ( !hasInfos && i.hasNext() ) {
            PublisherInfos info = i.next();
            boolean hasName = !(0 == info.getName().length());
            boolean hasUrl = !(0 == info.getUrl().length());
            
            hasInfos |= hasName && hasUrl;
        }
        
        // Write the infos
        if ( hasInfos ) {
            pWriter.startTag( XMLTokens.ELEMENT_PUBLISHER, null );
            
            HashMap<String, String> attrs = new HashMap<String, String>( );
            Iterator<Entry<Locale, PublisherInfos>> iter = mPublisherInfos.entrySet().iterator();
            while ( iter.hasNext() ) {
                Entry<Locale, PublisherInfos> entry = iter.next();
                String locale = writeLocale( entry.getKey() );
                attrs.clear();
                
                PublisherInfos info = entry.getValue();
                boolean hasName = !(0 == info.getName().length());
                boolean hasUrl = !(0 == info.getUrl().length());
                
                attrs.put( XMLTokens.createQName( XMLTokens.PREFIX_XLINK, XMLTokens.ATTR_HREF), 
                        info.getUrl() );
                attrs.put( XMLTokens.ATTR_LANG, locale );
                
                if ( hasName && hasUrl ) {
                    pWriter.startTag( XMLTokens.ELEMENT_NAME, attrs, false );
                    pWriter.print( XMLWriter.getEscaped( info.getName().trim( ) ) );
                    pWriter.endTag( XMLTokens.ELEMENT_NAME, false );
                }
            }
            
            pWriter.endTag( XMLTokens.ELEMENT_PUBLISHER );
        }
    }
    
    /**
     * Write the release-notes element and its children.
     * 
     * @param pWriter the XML writer
     */
    private void writeReleaseNotes(XMLWriter pWriter) {
        
        // Check the presence of a release note
        boolean hasReleaseNote = false;
        Iterator<String> i = mReleaseNotes.values().iterator();
        while ( !hasReleaseNote && i.hasNext( ) ) {
            String value = i.next();
            hasReleaseNote |= !(0 == value.trim().length());
        }
        
        //Write the block
        if ( hasReleaseNote ) {
            pWriter.startTag( XMLTokens.ELEMENT_RELEASE_NOTES, null );
            
            HashMap<String, String> attrs = new HashMap<String, String>( );
            Iterator<Entry<Locale, String>> iter = mReleaseNotes.entrySet().iterator();
            while ( iter.hasNext() ) {
                Entry<Locale, String> entry = iter.next();
                String locale = writeLocale( entry.getKey() );
                attrs.clear();
                attrs.put( XMLTokens.createQName( XMLTokens.PREFIX_XLINK, XMLTokens.ATTR_HREF ),
                        entry.getValue().trim( ) );
                attrs.put( XMLTokens.ATTR_LANG, locale );
                pWriter.printSingleTag( XMLTokens.ELEMENT_SRC, attrs );
            }
            
            pWriter.endTag( XMLTokens.ELEMENT_RELEASE_NOTES );
        }
    }

    /**
     * Write the display-name element and its children.
     * 
     * @param pWriter the XML writer
     */
    private void writeDisplayNames(XMLWriter pWriter) {
        
        // Check the presence of a release note
        boolean hasReleaseNote = false;
        Iterator<String> i = mDisplayNames.values().iterator();
        while ( !hasReleaseNote && i.hasNext( ) ) {
            String value = i.next();
            hasReleaseNote |= !(0 == value.trim().length());
        }
        
        //Write the block
        if ( hasReleaseNote ) {
            pWriter.startTag( XMLTokens.ELEMENT_DISPLAY_NAME, null );
            
            HashMap<String, String> attrs = new HashMap<String, String>( );
            Iterator<Entry<Locale, String>> iter = mDisplayNames.entrySet().iterator();
            while ( iter.hasNext() ) {
                Entry<Locale, String> entry = iter.next();
                String locale = writeLocale( entry.getKey() );
                attrs.clear();
                attrs.put( XMLTokens.ATTR_LANG, locale );
                pWriter.startTag( XMLTokens.ELEMENT_NAME, attrs, false );
                pWriter.print( XMLWriter.getEscaped( entry.getValue().trim() ) );
                pWriter.endTag( XMLTokens.ELEMENT_NAME, false );
            }
            
            pWriter.endTag( XMLTokens.ELEMENT_DISPLAY_NAME );
        }
    }
    
    /**
     * Write the icon element and its children.
     * 
     * @param pWriter the XML writer
     */
    private void writeIcons(XMLWriter pWriter) {
        boolean hasDefault = !(0 == mDefaultIcon.trim().length());
        boolean hasHC = !(0 == mHCIcon.trim().length());
        
        if ( hasDefault || hasHC ) {
            pWriter.startTag( XMLTokens.ELEMENT_ICON, null );
            
            HashMap<String, String> attrs = new HashMap<String, String>( );
            if ( hasDefault ) {
                attrs.put( XMLTokens.createQName( XMLTokens.PREFIX_XLINK, XMLTokens.ATTR_HREF), 
                        mDefaultIcon.trim() );
                pWriter.printSingleTag( XMLTokens.ELEMENT_DEFAULT, attrs);
            }
            
            if ( hasHC ) {
                attrs.clear();
                attrs.put( XMLTokens.createQName( XMLTokens.PREFIX_XLINK, XMLTokens.ATTR_HREF), 
                        mHCIcon.trim() );
                pWriter.printSingleTag( XMLTokens.ELEMENT_HIGH_CONTRAST, attrs);
            }
            
            pWriter.endTag( XMLTokens.ELEMENT_ICON );
        }
    }
    
    /**
     * Write the extension-descriptions element and its children.
     * 
     * @param pWriter the XML writer
     */
    private void writeDescriptions(XMLWriter pWriter) {
        
        // Check the presence of a description
        boolean hasDescription = false;
        Iterator<String> i = mDescriptions.values().iterator();
        while ( !hasDescription && i.hasNext( ) ) {
            String value = i.next();
            hasDescription |= !(0 == value.trim().length());
        }
        
        //Write the block
        if ( hasDescription ) {
            pWriter.startTag( XMLTokens.ELEMENT_EXTENSION_DESCRIPTION, null );
            
            HashMap<String, String> attrs = new HashMap<String, String>( );
            Iterator<Entry<Locale, String>> iter = mDescriptions.entrySet().iterator();
            while ( iter.hasNext() ) {
                Entry<Locale, String> entry = iter.next();
                String locale = writeLocale( entry.getKey() );
                attrs.clear();
                attrs.put( XMLTokens.createQName( XMLTokens.PREFIX_XLINK, XMLTokens.ATTR_HREF ),
                        entry.getValue().trim( ) );
                attrs.put( XMLTokens.ATTR_LANG, locale );
                pWriter.printSingleTag( XMLTokens.ELEMENT_SRC, attrs );
            }
            
            pWriter.endTag( XMLTokens.ELEMENT_EXTENSION_DESCRIPTION );
        }
    }
    
    /**
     * Outputs the locale in a form ready to output to description.xml file.
     * 
     * @param pLocale the locale to write.
     * 
     * @return the string form of the locale
     */
    private String writeLocale( Locale pLocale ) {
        char sep = '-';
        String result = new String( );
        
        result = pLocale.toString().replace( '_', sep );
        
        return result;
    }
    
    /**
     * Writes an XML element of the following form:
     * &lt;pElementName value="pValue"/&gt;.
     * 
     * @param pWriter the XML writer
     * @param pElementName the element name
     * @param pValue the element value
     */
    private void printValueElement(XMLWriter pWriter, String pElementName,
            String pValue) {
        HashMap<String, String> pAttributes = new HashMap<String, String>( );
        pAttributes.put( XMLTokens.ATTR_VALUE , pValue );
        pWriter.printSingleTag( pElementName, pAttributes );
    }

    /**
     * Merges the pNewLocales into the pLocales, but avoids duplicates elements.
     * 
     * @param pLocales the target list
     * @param pNewLocales the set of locales to add
     */
    private void appendNew( ArrayList<Locale> pLocales, Set<Locale> pNewLocales ) {
        Iterator<Locale> iter = pNewLocales.iterator();
        
        while ( iter.hasNext() ) {
            Locale locale = iter.next();
            if ( !pLocales.contains( locale ) ) {
                pLocales.add( locale );
            }
        }
    }
    
    /**
     * Tells everyone that the model has changed.
     */
    protected void fireModelChanged( ) {
        if ( !mSuspendEvents ) {
            mDirty = true;
            for ( IModelChangedListener listener : mListeners ) {
                listener.modelChanged();
            }
        }
    }
    
    /**
     * Tells everyone that the model has been saved.
     */
    protected void fireModelSaved( ) {
        if ( !mSuspendEvents ) {
            mDirty = false;
            for ( IModelChangedListener listener : mListeners ) {
                listener.modelSaved();
            }
        }
    }
}