KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > publisher > serverimpl > XmlDocDiffOutput


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.publisher.serverimpl;
17
18 import org.xml.sax.ContentHandler JavaDoc;
19 import org.xml.sax.Attributes JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
21 import org.xml.sax.helpers.AttributesImpl JavaDoc;
22 import org.outerj.daisy.repository.*;
23 import org.outerj.daisy.repository.variant.VariantManager;
24 import org.outerj.daisy.repository.user.UserManager;
25 import org.outerj.daisy.diff.Diff;
26 import org.outerj.daisy.diff.HtmlSaxDiffOutput;
27 import org.outerj.daisy.docdiff.DocDiffOutput;
28 import org.outerj.daisy.docdiff.DocDiffOutputHelper;
29
30 import java.text.DateFormat JavaDoc;
31
32 public class XmlDocDiffOutput implements DocDiffOutput {
33     private ContentHandler JavaDoc consumer;
34     private DocDiffOutputHelper outputHelper;
35
36     public XmlDocDiffOutput(ContentHandler JavaDoc consumer, DocDiffOutputHelper outputHelper) {
37         this.consumer = consumer;
38         this.outputHelper = outputHelper;
39     }
40
41     public void begin() throws Exception JavaDoc {
42         startElement("diff-report");
43         startElement("info");
44
45         Version version1 = outputHelper.getVersion1();
46         Version version2 = outputHelper.getVersion2();
47
48         Document document1 = outputHelper.getDocument1();
49         Document document2 = outputHelper.getDocument2();
50         VariantManager variantManager = outputHelper.getRepository().getVariantManager();
51
52         DateFormat JavaDoc dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, outputHelper.getLocale());
53         UserManager userManager = outputHelper.getRepository().getUserManager();
54
55         // Version 1
56
AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
57         attrs.addAttribute("", "branch", "branch", "CDATA", variantManager.getBranch(document1.getBranchId(), false).getName());
58         attrs.addAttribute("", "language", "language", "CDATA", variantManager.getLanguage(document1.getLanguageId(), false).getName());
59         attrs.addAttribute("", "id", "id", "CDATA", String.valueOf(version1.getId()));
60         attrs.addAttribute("", "created", "created", "CDATA", dateFormat.format(version1.getCreated()));
61         attrs.addAttribute("", "creatorId", "creatorId", "CDATA", String.valueOf(version1.getCreator()));
62         attrs.addAttribute("", "creatorName", "creatorName", "CDATA", userManager.getUserDisplayName(version1.getCreator()));
63         attrs.addAttribute("", "state", "state", "CDATA", version1.getState().toString());
64
65         startElement("version1", attrs);
66         endElement("version1");
67
68         // Version 2
69
attrs = new AttributesImpl JavaDoc();
70         attrs.addAttribute("", "branch", "branch", "CDATA", variantManager.getBranch(document2.getBranchId(), false).getName());
71         attrs.addAttribute("", "language", "language", "CDATA", variantManager.getLanguage(document2.getLanguageId(), false).getName());
72         attrs.addAttribute("", "id", "id", "CDATA", String.valueOf(version2.getId()));
73         attrs.addAttribute("", "created", "created", "CDATA", dateFormat.format(version2.getCreated()));
74         attrs.addAttribute("", "creatorId", "creatorId", "CDATA", String.valueOf(version2.getCreator()));
75         attrs.addAttribute("", "creatorName", "creatorName", "CDATA", userManager.getUserDisplayName(version2.getCreator()));
76         attrs.addAttribute("", "state", "state", "CDATA", version2.getState().toString());
77
78         startElement("version2", attrs);
79         endElement("version2");
80
81         endElement("info");
82
83
84         String JavaDoc version1Name = version1.getDocumentName();
85         String JavaDoc version2Name = version2.getDocumentName();
86         attrs = new AttributesImpl JavaDoc();
87         attrs.addAttribute("", "version1", "version1", "CDATA", version1Name);
88         if (!version1Name.equals(version2Name))
89             attrs.addAttribute("", "version2", "version2", "CDATA", version2Name);
90         startElement("documentName", attrs);
91         endElement("documentName");
92     }
93
94     public void end() throws Exception JavaDoc {
95         endElement("diff-report");
96     }
97
98     private void startElement(String JavaDoc name, Attributes JavaDoc attrs) throws SAXException JavaDoc {
99         consumer.startElement("", name, name, attrs);
100     }
101
102     private void startElement(String JavaDoc name) throws SAXException JavaDoc {
103         consumer.startElement("", name, name, new AttributesImpl JavaDoc());
104     }
105
106     private void endElement(String JavaDoc name) throws SAXException JavaDoc {
107         consumer.endElement("", name, name);
108     }
109
110     public void partRemoved(Part removedPart) throws Exception JavaDoc {
111         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
112         attrs.addAttribute("", "typeId", "typeId", "CDATA", String.valueOf(removedPart.getTypeId()));
113         attrs.addAttribute("", "typeLabel", "typeLabel", "CDATA", outputHelper.getPartLabel(removedPart.getTypeId()));
114         startElement("partRemoved", attrs);
115         endElement("partRemoved");
116     }
117
118     public void partAdded(Part addedPart) throws Exception JavaDoc {
119         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
120         attrs.addAttribute("", "typeId", "typeId", "CDATA", String.valueOf(addedPart.getTypeId()));
121         attrs.addAttribute("", "typeLabel", "typeLabel", "CDATA", outputHelper.getPartLabel(addedPart.getTypeId()));
122         startElement("partAdded", attrs);
123         endElement("partAdded");
124     }
125
126     public void partUnchanged(Part unchangedPart) throws Exception JavaDoc {
127         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
128         attrs.addAttribute("", "typeId", "typeId", "CDATA", String.valueOf(unchangedPart.getTypeId()));
129         attrs.addAttribute("", "typeLabel", "typeLabel", "CDATA", outputHelper.getPartLabel(unchangedPart.getTypeId()));
130         startElement("partUnchanged", attrs);
131         endElement("partUnchanged");
132     }
133
134     public void partUpdated(Part version1Part, Part version2Part, String JavaDoc part1Data, String JavaDoc part2Data) throws Exception JavaDoc {
135         boolean mimeTypesEqual = version1Part.getMimeType().equals(version2Part.getMimeType());
136         boolean sizeEqual = version1Part.getSize() == version2Part.getSize();
137         String JavaDoc version1FileName = nullToEmpty(version1Part.getFileName());
138         String JavaDoc version2FileName = nullToEmpty(version2Part.getFileName());
139         boolean fileNamesEqual = version1FileName.equals(version2FileName);
140
141         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
142         attrs.addAttribute("", "typeId", "typeId", "CDATA", String.valueOf(version2Part.getTypeId()));
143         attrs.addAttribute("", "typeLabel", "typeLabel", "CDATA", outputHelper.getPartLabel(version2Part.getTypeId()));
144         attrs.addAttribute("", "version1MimeType", "version1MimeType", "CDATA", version1Part.getMimeType());
145         if (!mimeTypesEqual) {
146             attrs.addAttribute("", "version2MimeType", "version2MimeType", "CDATA", version2Part.getMimeType());
147         }
148         attrs.addAttribute("", "version1FileName", "version1FileName", "CDATA", version1FileName);
149         if (!fileNamesEqual) {
150             attrs.addAttribute("", "version2FileName", "version2FileName", "CDATA", version2FileName);
151         }
152         attrs.addAttribute("", "version1Size", "version1Size", "CDATA", String.valueOf(version1Part.getSize()));
153         if (!sizeEqual) {
154             attrs.addAttribute("", "version2Size", "version2Size", "CDATA", String.valueOf(version2Part.getSize()));
155         }
156         startElement("partUpdated", attrs);
157
158         if (part1Data != null) {
159             startElement("diff");
160             Diff.diff(part1Data, part2Data, new HtmlSaxDiffOutput(consumer), -1);
161             endElement("diff");
162         }
163
164         endElement("partUpdated");
165     }
166
167     private String JavaDoc nullToEmpty(String JavaDoc value) {
168         if (value == null)
169             return "";
170         return value;
171     }
172
173     public void partMightBeUpdated(Part version2Part) throws Exception JavaDoc {
174         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
175         attrs.addAttribute("", "typeId", "typeId", "CDATA", String.valueOf(version2Part.getTypeId()));
176         attrs.addAttribute("", "typeLabel", "typeLabel", "CDATA", outputHelper.getPartLabel(version2Part.getTypeId()));
177         startElement("partMightBeUpdated", attrs);
178         endElement("partMightBeUpdated");
179     }
180
181     public void beginPartChanges() throws Exception JavaDoc {
182         consumer.startElement("", "parts", "parts", new AttributesImpl JavaDoc());
183     }
184
185     public void endPartChanges() throws Exception JavaDoc {
186         endElement("parts");
187     }
188
189     public void beginFieldChanges() throws Exception JavaDoc {
190         startElement("fields");
191     }
192
193     public void endFieldChanges() throws Exception JavaDoc {
194         endElement("fields");
195     }
196
197     public void fieldRemoved(Field removedField) throws Exception JavaDoc {
198         long typeId = removedField.getTypeId();
199         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
200         attrs.addAttribute("", "typeId", "typeId", "CDATA", String.valueOf(typeId));
201         attrs.addAttribute("", "typeLabel", "typeLabel", "CDATA", outputHelper.getFieldLabel(typeId));
202         attrs.addAttribute("", "version1", "version1", "CDATA", FieldHelper.getFormattedValue(removedField.getValue(), removedField.getValueType(), outputHelper.getLocale(), outputHelper.getRepository()));
203         startElement("fieldRemoved", attrs);
204         endElement("fieldRemoved");
205     }
206
207     public void fieldAdded(Field addedField) throws Exception JavaDoc {
208         long typeId = addedField.getTypeId();
209         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
210         attrs.addAttribute("", "typeId", "typeId", "CDATA", String.valueOf(typeId));
211         attrs.addAttribute("", "typeLabel", "typeLabel", "CDATA", outputHelper.getFieldLabel(typeId));
212         attrs.addAttribute("", "version2", "version2", "CDATA", FieldHelper.getFormattedValue(addedField.getValue(), addedField.getValueType(), outputHelper.getLocale(), outputHelper.getRepository()));
213         startElement("fieldAdded", attrs);
214         endElement("fieldAdded");
215     }
216
217     public void fieldUpdated(Field version1Field, Field version2Field) throws Exception JavaDoc {
218         long typeId = version1Field.getTypeId();
219         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
220         attrs.addAttribute("", "typeId", "typeId", "CDATA", String.valueOf(typeId));
221         attrs.addAttribute("", "typeLabel", "typeLabel", "CDATA", outputHelper.getFieldLabel(typeId));
222         attrs.addAttribute("", "version1", "version1", "CDATA", FieldHelper.getFormattedValue(version1Field.getValue(), version1Field.getValueType(), outputHelper.getLocale(), outputHelper.getRepository()));
223         attrs.addAttribute("", "version2", "version2", "CDATA", FieldHelper.getFormattedValue(version2Field.getValue(), version2Field.getValueType(), outputHelper.getLocale(), outputHelper.getRepository()));
224         startElement("fieldUpdated", attrs);
225         endElement("fieldUpdated");
226     }
227
228     public void beginLinkChanges() throws Exception JavaDoc {
229         startElement("links");
230     }
231
232     public void linkRemoved(Link link) throws Exception JavaDoc {
233         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
234         attrs.addAttribute("", "title", "title", "CDATA", link.getTitle());
235         attrs.addAttribute("", "target", "target", "CDATA", link.getTarget());
236         startElement("linkRemoved", attrs);
237         endElement("linkRemoved");
238     }
239
240     public void linkAdded(Link link) throws Exception JavaDoc {
241         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
242         attrs.addAttribute("", "title", "title", "CDATA", link.getTitle());
243         attrs.addAttribute("", "target", "target", "CDATA", link.getTarget());
244         startElement("linkAdded", attrs);
245         endElement("linkAdded");
246     }
247
248     public void endLinkChanges() throws Exception JavaDoc {
249         endElement("links");
250     }
251 }
252
Popular Tags