This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
ResourceUtils.java
1193 lines (1080 loc) · 36.2 KB
/
ResourceUtils.java
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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.zanata.rest.service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import javax.annotation.PostConstruct;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.commons.lang.StringUtils;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;
import org.zanata.ApplicationConfiguration;
import org.zanata.common.ContentState;
import org.zanata.common.LocaleId;
import org.zanata.common.ResourceType;
import org.zanata.model.HDocument;
import org.zanata.model.HLocale;
import org.zanata.model.HPerson;
import org.zanata.model.HSimpleComment;
import org.zanata.model.HTextFlow;
import org.zanata.model.HTextFlowTarget;
import org.zanata.model.HasSimpleComment;
import org.zanata.model.po.HPoHeader;
import org.zanata.model.po.HPoTargetHeader;
import org.zanata.model.po.HPotEntryData;
import org.zanata.rest.dto.Person;
import org.zanata.rest.dto.extensions.comment.SimpleComment;
import org.zanata.rest.dto.extensions.gettext.AbstractResourceMetaExtension;
import org.zanata.rest.dto.extensions.gettext.HeaderEntry;
import org.zanata.rest.dto.extensions.gettext.PoHeader;
import org.zanata.rest.dto.extensions.gettext.PoTargetHeader;
import org.zanata.rest.dto.extensions.gettext.PotEntryHeader;
import org.zanata.rest.dto.extensions.gettext.TextFlowExtension;
import org.zanata.rest.dto.extensions.gettext.TextFlowTargetExtension;
import org.zanata.rest.dto.extensions.gettext.TranslationsResourceExtension;
import org.zanata.rest.dto.resource.AbstractResourceMeta;
import org.zanata.rest.dto.resource.ExtensionSet;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.TextFlow;
import org.zanata.rest.dto.resource.TextFlowTarget;
import org.zanata.rest.dto.resource.TranslationsResource;
import org.zanata.util.StringUtil;
@Name("resourceUtils")
@Scope(ScopeType.STATELESS)
@AutoCreate
@BypassInterceptors
public class ResourceUtils
{
/**
* Newline character used for multi-line comments
*/
private static final char NEWLINE = '\n';
private static final String ZANATA_GENERATOR_PREFIX = "Zanata";
private static final String ZANATA_TAG = "#zanata";
private static final String PO_DATE_FORMAT = "yyyy-MM-dd hh:mmZ";
private static final String[] PO_VALID_CONTENT_TYPES = {"charset=UTF-8", "charset=UTF8", "charset=ASCII", "charset=CHARSET"};
/**
* PO Header entries
*/
private static final String LAST_TRANSLATOR_HDR = "Last-Translator";
private static final String PO_REVISION_DATE_HDR = "PO-Revision-Date";
private static final String LANGUAGE_TEAM_HDR = "Language-Team";
private static final String X_GENERATOR_HDR = "X-Generator";
private static final String LANGUAGE_HDR = "Language";
private static final String CONTENT_TYPE_HDR = "Content-Type";
private static final String PLURAL_FORMS_HDR = "Plural-Forms";
private static final Log log = Logging.getLog(ResourceUtils.class);
private Properties pluralForms = new Properties();
@PostConstruct
public void create()
{
try
{
pluralForms.load( this.getClass().getClassLoader().getResourceAsStream( "pluralforms.properties" ) );
}
catch (IOException e)
{
log.error("There was an error loading plural forms.", e);
}
}
/**
* Merges the list of TextFlows into the target HDocument, adding and obsoleting TextFlows as necessary.
* @param from
* @param to
* @return
*/
boolean transferFromTextFlows(List<TextFlow> from, HDocument to, Set<String> enabledExtensions, int nextDocRev)
{
boolean changed = false;
to.getTextFlows().clear();
Set<String> incomingIds = new HashSet<String>();
Set<String> previousIds = new HashSet<String>(to.getAllTextFlows().keySet());
for (TextFlow tf : from)
{
if (!incomingIds.add(tf.getId()))
{
Response response = Response.status(Status.BAD_REQUEST).entity("encountered TextFlow with duplicate ID " + tf.getId()).build();
throw new WebApplicationException(response);
}
HTextFlow textFlow;
if (previousIds.contains(tf.getId()))
{
previousIds.remove(tf.getId());
textFlow = to.getAllTextFlows().get(tf.getId());
textFlow.setObsolete(false);
// avoid changing revision when resurrecting an unchanged TF
if (transferFromTextFlow(tf, textFlow, enabledExtensions))
{// content has changed
textFlow.setRevision(nextDocRev);
changed = true;
for (HTextFlowTarget targ : textFlow.getTargets().values())
{
// if (targ.getState() != ContentState.New)
if (targ.getState() == ContentState.Approved)
{
targ.setState(ContentState.NeedReview);
targ.setVersionNum(targ.getVersionNum() + 1);
}
}
log.debug("TextFlow with id {0} has changed", tf.getId());
}
}
else
{
textFlow = new HTextFlow();
textFlow.setDocument(to);
textFlow.setResId(tf.getId());
textFlow.setRevision(nextDocRev);
transferFromTextFlow(tf, textFlow, enabledExtensions);
changed = true;
log.debug("TextFlow with id {0} is new", tf.getId());
}
to.getTextFlows().add(textFlow);
to.getAllTextFlows().put(textFlow.getResId(), textFlow);
}
// set remaining textflows to obsolete.
for (String id : previousIds)
{
HTextFlow textFlow = to.getAllTextFlows().get(id);
if (!textFlow.isObsolete())
{
changed = true;
log.debug("TextFlow with id {0} is now obsolete", id);
textFlow.setRevision(to.getRevision());
textFlow.setObsolete(true);
}
}
if (changed)
to.setRevision(nextDocRev);
return changed;
}
/**
* Merges from the DTO Resource into HDocument, adding and obsoleting textflows, including metadata and the specified extensions
* @param from
* @param to
* @param enabledExtensions
* @return
*/
public boolean transferFromResource(Resource from, HDocument to, Set<String> enabledExtensions, HLocale locale, int nextDocRev)
{
boolean changed = false;
changed |= transferFromResourceMetadata(from, to, enabledExtensions, locale, nextDocRev);
changed |= transferFromTextFlows(from.getTextFlows(), to, enabledExtensions, nextDocRev);
return changed;
}
/**
* Transfers metadata and the specified extensions from DTO AbstractResourceMeta into HDocument
* @param from
* @param to
* @param enabledExtensions
* @return
*/
public boolean transferFromResourceMetadata(AbstractResourceMeta from, HDocument to, Set<String> enabledExtensions, HLocale locale, int nextDocRev)
{
boolean changed = false;
// name
if (!equals(from.getName(), to.getDocId()))
{
to.setFullPath(from.getName());
changed = true;
}
// locale
if (!equals(from.getLang(), to.getLocale().getLocaleId()))
{
log.debug("locale:" + from.getLang());
to.setLocale(locale);
changed = true;
}
// contentType
if (!equals(from.getContentType(), to.getContentType()))
{
to.setContentType(from.getContentType());
changed = true;
}
// handle extensions
changed |= transferFromResourceExtensions(from.getExtensions(true), to, enabledExtensions);
if (changed)
to.setRevision(nextDocRev);
return changed;
}
/**
* Transfers from DTO TextFlowTarget into HTextFlowTarget
* @param from
* @param to
* @return
* @todo merge with {@link #transferFromTextFlowTargetExtensions}
*/
public boolean transferFromTextFlowTarget(TextFlowTarget from, HTextFlowTarget to)
{
boolean changed = false;
if (!equals(from.getContent(), to.getContent()))
{
to.setContent(from.getContent());
changed = true;
}
if (!equals(from.getState(), to.getState()))
{
to.setState(from.getState());
changed = true;
}
if (changed)
{
to.setVersionNum(to.getVersionNum() + 1);
}
return changed;
}
/**
* Transfers the specified extensions from DTO AbstractResourceMeta into HDocument
* @param from
* @param to
* @param enabledExtensions
* @return
* @see #transferFromResource
*/
private boolean transferFromResourceExtensions(ExtensionSet<AbstractResourceMetaExtension> from, HDocument to, Set<String> enabledExtensions)
{
boolean changed = false;
if (enabledExtensions.contains(PoHeader.ID))
{
PoHeader poHeaderExt = from.findByType(PoHeader.class);
if (poHeaderExt != null)
{
HPoHeader poHeader = to.getPoHeader();
if (poHeader == null)
{
log.debug("create a new HPoHeader");
poHeader = new HPoHeader();
}
changed |= transferFromPoHeader(poHeaderExt, poHeader);
if (to.getPoHeader() == null && changed)
{
to.setPoHeader(poHeader);
}
}
}
return changed;
}
/**
* Transfers enabled extensions from TranslationsResource into HDocument for a single locale
* @param from
* @param to
* @param enabledExtensions
* @param locale
* @param mergeType
* @return
* @see #transferToTranslationsResourceExtensions
*/
public boolean transferFromTranslationsResourceExtensions(ExtensionSet<TranslationsResourceExtension> from, HDocument to, Set<String> enabledExtensions, HLocale locale, MergeType mergeType)
{
boolean changed = false;
if (enabledExtensions.contains(PoTargetHeader.ID))
{
PoTargetHeader fromTargetHeader = from.findByType(PoTargetHeader.class);
if (fromTargetHeader != null)
{
log.debug("found PO header for locale: {0}", locale);
HPoTargetHeader toTargetHeader = to.getPoTargetHeaders().get(locale);
if (toTargetHeader == null)
{
changed = true;
toTargetHeader = new HPoTargetHeader();
toTargetHeader.setTargetLanguage(locale);
toTargetHeader.setDocument(to);
transferFromPoTargetHeader(fromTargetHeader, toTargetHeader, MergeType.IMPORT); // return value not needed
to.getPoTargetHeaders().put(locale, toTargetHeader);
}
else
{
changed |= transferFromPoTargetHeader(fromTargetHeader, toTargetHeader, mergeType);
}
}
else
{
changed |= to.getPoTargetHeaders().remove(locale) != null;
}
}
return changed;
}
/**
* Transfers enabled extensions from DTO TextFlowTarget to HTextFlowTarget
* @param extensions
* @param hTarget
* @param enabledExtensions
* @return
* @todo merge with {@link #transferFromTextFlowTarget}
*/
public boolean transferFromTextFlowTargetExtensions(ExtensionSet<TextFlowTargetExtension> extensions, HTextFlowTarget hTarget, Set<String> enabledExtensions)
{
boolean changed = false;
if (enabledExtensions.contains(SimpleComment.ID))
{
SimpleComment comment = extensions.findByType(SimpleComment.class);
if (comment != null)
{
changed |= transferFromComment(comment, hTarget);
}
}
return changed;
}
/**
* Transfers from DTO SimpleComment to a Hibernate object's "comment" property
* @param from
* @param to
* @return
*/
private boolean transferFromComment(SimpleComment from, HasSimpleComment to)
{
HSimpleComment hComment = to.getComment();
if (hComment == null)
{
hComment = new HSimpleComment();
}
if (!equals(from.getValue(), hComment.getComment()))
{
hComment.setComment(from.getValue());
to.setComment(hComment);
return true;
}
return false;
}
private boolean transferFromTextFlowExtensions(ExtensionSet<TextFlowExtension> from, HTextFlow to, Set<String> enabledExtensions)
{
boolean changed = false;
if (enabledExtensions.contains(PotEntryHeader.ID))
{
PotEntryHeader entryHeader = from.findByType(PotEntryHeader.class);
if (entryHeader != null)
{
HPotEntryData hEntryHeader = to.getPotEntryData();
if (hEntryHeader == null)
{
changed = true;
hEntryHeader = new HPotEntryData();
to.setPotEntryData(hEntryHeader);
log.debug("set potentryheader");
}
changed |= transferFromPotEntryHeader(entryHeader, hEntryHeader);
}
}
if (enabledExtensions.contains(SimpleComment.ID))
{
SimpleComment comment = from.findByType(SimpleComment.class);
if (comment != null)
{
HSimpleComment hComment = to.getComment();
if (hComment == null)
{
hComment = new HSimpleComment();
}
if (!equals(comment.getValue(), hComment.getComment()))
{
changed = true;
hComment.setComment(comment.getValue());
to.setComment(hComment);
log.debug("set comment:{0}", comment.getValue());
}
}
}
return changed;
}
/**
* @see #transferToPotEntryHeader(HPotEntryData, PotEntryHeader)
* @param from
* @param to
* @return
*/
private boolean transferFromPotEntryHeader(PotEntryHeader from, HPotEntryData to)
{
boolean changed = false;
if (!equals(from.getContext(), to.getContext()))
{
changed = true;
to.setContext(from.getContext());
}
List<String> flagList = from.getFlags();
String flags = StringUtil.concat(from.getFlags(), ',');
if( flagList.isEmpty() )
{
flags = null;
}
if (!equals(flags, to.getFlags()))
{
changed = true;
to.setFlags(flags);
}
List<String> refList = from.getReferences();
String refs = StringUtil.concat(from.getReferences(), ',');
if( refList.isEmpty() )
{
refs = null;
}
if (!equals(refs, to.getReferences()))
{
changed = true;
to.setReferences(refs);
}
return changed;
}
/**
*
* @param from
* @param to
* @param mergeType
* @return
* @see #transferFromTranslationsResourceExtensions
* @see #transferToPoTargetHeader
*/
private boolean transferFromPoTargetHeader(PoTargetHeader from, HPoTargetHeader to, MergeType mergeType)
{
boolean changed = pushPoTargetComment(from, to, mergeType);
// TODO we should probably block PoHeader/POT-specific entries
// ie POT-Creation-Date, Project-Id-Version, Report-Msgid-Bugs-To
String entries = PoUtility.listToHeader(from.getEntries());
if (!equals(entries, to.getEntries()))
{
to.setEntries(entries);
changed = true;
}
return changed;
}
/**
*
* @param fromHeader
* @param toHeader
* @param mergeType
* @return
* @see #pullPoTargetComment
*/
protected boolean pushPoTargetComment(PoTargetHeader fromHeader, HPoTargetHeader toHeader, MergeType mergeType)
{
boolean changed = false;
HSimpleComment hComment = toHeader.getComment();
if (hComment == null)
{
hComment = new HSimpleComment();
}
String fromComment = fromHeader.getComment();
String toComment = hComment.getComment();
if (!equals(fromComment, toComment))
{
// skip #zanata lines
List<String> fromLines = splitLines(fromComment, ZANATA_TAG);
StringBuilder sb = new StringBuilder(fromComment.length());
switch (mergeType)
{
case IMPORT:
for (String line : fromLines)
{
if (sb.length() != 0)
sb.append(NEWLINE);
sb.append(line);
changed = true;
}
break;
default: // AUTO or anything else will merge comments
// to merge, we just append new lines, skip old lines
List<String> toLines = Collections.emptyList();
if (toComment != null)
{
sb.append(toComment);
toLines = splitLines(toComment, null);
}
for (String line : fromLines)
{
if (!toLines.contains(line))
{
if (sb.length() != 0)
sb.append(NEWLINE);
sb.append(line);
changed = true;
}
}
break;
}
if (changed)
{
hComment.setComment(sb.toString());
toHeader.setComment(hComment);
}
}
return changed;
}
/**
* splits s into lines, skipping any which contain tagToSkip
* @param s
* @param tagToSkip
* @return
*/
static List<String> splitLines(String s, String tagToSkip)
{
if (s.isEmpty())
return Collections.emptyList();
try
{
List<String> lineList = new ArrayList<String>(s.length() / 40);
BufferedReader reader = new BufferedReader(new StringReader(s));
String line;
while ((line = reader.readLine()) != null)
{
if (tagToSkip == null || !line.contains(tagToSkip))
{
lineList.add(line);
}
}
return lineList;
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
private boolean transferFromPoHeader(PoHeader from, HPoHeader to)
{
boolean changed = false;
HSimpleComment comment = to.getComment();
if (comment == null)
{
comment = new HSimpleComment();
}
if (!equals(from.getComment(), comment.getComment()))
{
changed = true;
comment.setComment(from.getComment());
to.setComment(comment);
}
String entries = PoUtility.listToHeader(from.getEntries());
if (!equals(entries, to.getEntries()))
{
to.setEntries(entries);
changed = true;
}
return changed;
}
public static <T> boolean equals(T a, T b)
{
if (a == null && b == null)
{
return true;
}
if (a == null || b == null)
{
return false;
}
return a.equals(b);
}
private boolean transferFromTextFlow(TextFlow from, HTextFlow to, Set<String> enabledExtensions)
{
boolean changed = false;
if (!equals(from.getContent(), to.getContent()))
{
to.setContent(from.getContent());
changed = true;
}
// TODO from.getLang()
transferFromTextFlowExtensions(from.getExtensions(true), to, enabledExtensions);
return changed;
}
public void transferToResource(HDocument from, Resource to)
{
transferToAbstractResourceMeta(from, to);
}
private void transferToPoHeader(HPoHeader from, PoHeader to)
{
if (from.getComment() != null)
{
to.setComment(from.getComment().getComment());
}
to.getEntries().addAll(PoUtility.headerToList(from.getEntries()));
}
/**
*
* @param from
* @param to
* @param hTargets
* @param locale
* @see #transferToTranslationsResourceExtensions
* @see #transferFromPoTargetHeader
*/
private void transferToPoTargetHeader(HPoTargetHeader from, PoTargetHeader to, List<HTextFlowTarget> hTargets, HLocale locale)
{
pullPoTargetComment(from, to, hTargets);
to.getEntries().addAll(this.headerToList(from.getEntries()));
populateHeaderEntries(to.getEntries(), hTargets, locale);
}
/**
* Transforms a set of header entries from a String to a list of POJOs.
*
* @param entries The header entries' string.
*/
private List<HeaderEntry> headerToList( final String entries )
{
return PoUtility.headerToList( entries );
}
/**
* Populates a list of header entries with values stored in the system.
* For certain headers, the original value will remain if present.
*
* @param headerEntries The header entries to be populated.
* @param hTargets The Text Flow Targets that the header applies to.
* @param locale The locale that is bein
*/
private void populateHeaderEntries( final List<HeaderEntry> headerEntries, final List<HTextFlowTarget> hTargets,
final HLocale locale)
{
final Map<String, HeaderEntry> containedHeaders = new HashMap<String, HeaderEntry>( headerEntries.size() );
// Collect the existing header entries
for( HeaderEntry entry : headerEntries )
{
containedHeaders.put(entry.getKey(), entry);
}
// Add / Replace headers
HeaderEntry headerEntry = containedHeaders.get( LAST_TRANSLATOR_HDR );
if( headerEntry == null )
{
headerEntry = new HeaderEntry(LAST_TRANSLATOR_HDR, this.getLastTranslator(hTargets));
headerEntries.add( headerEntry );
}
else
{
headerEntry.setValue(this.getLastTranslator(hTargets));
}
headerEntry = containedHeaders.get( PO_REVISION_DATE_HDR );
if( headerEntry == null )
{
headerEntry = new HeaderEntry(PO_REVISION_DATE_HDR, this.getLastModifiedDate(hTargets));
headerEntries.add( headerEntry );
}
else
{
headerEntry.setValue(this.getLastModifiedDate(hTargets));
}
headerEntry = containedHeaders.get( LANGUAGE_TEAM_HDR );
if( headerEntry == null )
{
headerEntry = new HeaderEntry(LANGUAGE_TEAM_HDR, this.getLanguageTeam(hTargets));
headerEntries.add( headerEntry );
}
// Keep the original value if provided
headerEntry = containedHeaders.get( X_GENERATOR_HDR );
if( headerEntry == null )
{
headerEntry = new HeaderEntry(X_GENERATOR_HDR, this.getSystemVersion());
headerEntries.add( headerEntry );
}
else
{
headerEntry.setValue( this.getSystemVersion() );
}
headerEntry = containedHeaders.get( LANGUAGE_HDR );
if( headerEntry == null )
{
headerEntry = new HeaderEntry(LANGUAGE_HDR, this.getLanguage(hTargets));
headerEntries.add( headerEntry );
}
else
{
headerEntry.setValue( this.getLanguage(hTargets) );
}
headerEntry = containedHeaders.get( PLURAL_FORMS_HDR );
if( headerEntry == null )
{
headerEntry = new HeaderEntry(PLURAL_FORMS_HDR, this.getPluralForms(locale));
headerEntries.add(headerEntry);
}
// Keep the original if provided
}
/**
* Gets the last translator for a set of Text Flow targets.
*
* @param translations The text flow targets.
* @return A string with the value of the last translator for the given set of
* translations.
*/
private String getLastTranslator( final List<HTextFlowTarget> translations )
{
Date lastUpdate = new Date(Long.MIN_VALUE);
String lastTranslator = "";
for( HTextFlowTarget trans : translations )
{
if( trans.getLastChanged().after( lastUpdate ) )
{
HPerson lastModifiedBy = trans.getLastModifiedBy();
if( lastModifiedBy != null )
{
lastTranslator = lastModifiedBy.getName() + " <" + lastModifiedBy.getEmail() + ">";
}
}
}
return lastTranslator;
}
/**
* Gets the last date of modification for a set of Text Flow targets.
*
* @param translations The text flow targets.
* @return A string with the value of the Last modified date for the given set of
* translations.
*/
private String getLastModifiedDate( final List<HTextFlowTarget> translations )
{
Date lastUpdate = null;
for( HTextFlowTarget trans : translations )
{
if( lastUpdate == null || trans.getLastChanged().after( lastUpdate ) )
{
lastUpdate = trans.getLastChanged();
}
}
if( lastUpdate == null )
{
return null;
}
else
{
SimpleDateFormat dateFormat = new SimpleDateFormat( PO_DATE_FORMAT );
return dateFormat.format( lastUpdate );
}
}
/**
* Retrieves the language team for a set of translations. Will assume all translations
* have the same locale.
*
* @param translations
*/
private String getLanguageTeam( final List<HTextFlowTarget> translations )
{
for( HTextFlowTarget trans : translations )
{
if( trans.getLocale() != null )
{
return trans.getLocale().retrieveDisplayName();
}
}
return "";
}
/**
* Retrieves the language for a set of translations. Will assume all translations
* have the same locale.
*
* @param translations
*/
private String getLanguage( final List<HTextFlowTarget> translations )
{
for( HTextFlowTarget trans : translations )
{
if( trans.getLocale() != null )
{
return trans.getLocale().getLocaleId().getId();
}
}
return "";
}
/**
* Returns the application version.
*/
private String getSystemVersion()
{
try
{
return ZANATA_GENERATOR_PREFIX + " " +
((ApplicationConfiguration)Component.getInstance(ApplicationConfiguration.class, ScopeType.APPLICATION))
.getVersion();
}
catch (Exception e)
{
return ZANATA_GENERATOR_PREFIX + " UNKNOWN";
}
}
/**
* Returns the appropriate plural form for a given Locale.
*/
private String getPluralForms(HLocale locale)
{
LocaleId localeId = locale.getLocaleId();
String javaLocale = localeId.toJavaName().toLowerCase();
if( pluralForms.containsKey( javaLocale ) )
{
return pluralForms.getProperty( javaLocale );
}
// Try out every combination. e.g: for xxx_yyy_zzz, try xxx_yyyy_zzz, then xxx_yyy, then xxx
while( javaLocale.indexOf('_') > 0 )
{
javaLocale = javaLocale.substring(0, javaLocale.indexOf('_'));
if( pluralForms.containsKey( javaLocale ) )
{
return pluralForms.getProperty( javaLocale );
}
}
// Not found, return null
return null;
}
/**
*
* @param fromHeader
* @param toHeader
* @see #pushPoTargetComment
*/
protected void pullPoTargetComment(HPoTargetHeader fromHeader, PoTargetHeader toHeader, List<HTextFlowTarget> hTargets)
{
StringBuilder sb = new StringBuilder();
HSimpleComment comment = fromHeader.getComment();
if (comment != null)
{
sb.append(comment.getComment());
}
// generate #zanata credit comments
// order by year, then alphabetically
Set<TranslatorCredit> zanataCredits = new TreeSet<TranslatorCredit>();
for(HTextFlowTarget tft : hTargets)
{
HPerson person = tft.getLastModifiedBy();
if (person != null)
{
Calendar lastChanged = Calendar.getInstance();
lastChanged.setTime(tft.getLastChanged());
int year = lastChanged.get(Calendar.YEAR);
TranslatorCredit credit = new TranslatorCredit();
credit.setEmail(person.getEmail());
credit.setName(person.getName());
credit.setYear(year);
zanataCredits.add(credit);
}
}
for(TranslatorCredit credit : zanataCredits)
{
if (sb.length() != 0)
sb.append(NEWLINE);
sb.append(credit);
sb.append(' ');
sb.append(ZANATA_TAG);
}
toHeader.setComment(sb.toString());
}
public void transferToTextFlow(HTextFlow from, TextFlow to)
{
to.setContent(from.getContent());
to.setRevision(from.getRevision());
// TODO HTextFlow should have a lang
// to.setLang(from.get)
}
public void transferToAbstractResourceMeta(HDocument from, AbstractResourceMeta to)
{
to.setContentType(from.getContentType());
to.setLang(from.getLocale().getLocaleId());
to.setName(from.getDocId());
// TODO ADD support within the hibernate model for multiple resource types
to.setType(ResourceType.FILE);
to.setRevision(from.getRevision());
}
public void transferToResourceExtensions(HDocument from, ExtensionSet<AbstractResourceMetaExtension> to, Set<String> enabledExtensions)
{
if (enabledExtensions.contains(PoHeader.ID))
{
PoHeader poHeaderExt = new PoHeader();
if (from.getPoHeader() != null)
{
transferToPoHeader(from.getPoHeader(), poHeaderExt);
to.add(poHeaderExt);
}
}
}
/**
*
* @param from
* @param to
* @param enabledExtensions
* @param locale