1 16 package org.outerj.daisy.publisher.serverimpl; 17 18 import org.xml.sax.ContentHandler ; 19 import org.xml.sax.Attributes ; 20 import org.xml.sax.SAXException ; 21 import org.xml.sax.helpers.AttributesImpl ; 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 ; 31 32 public class XmlDocDiffOutput implements DocDiffOutput { 33 private ContentHandler consumer; 34 private DocDiffOutputHelper outputHelper; 35 36 public XmlDocDiffOutput(ContentHandler consumer, DocDiffOutputHelper outputHelper) { 37 this.consumer = consumer; 38 this.outputHelper = outputHelper; 39 } 40 41 public void begin() throws Exception { 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 dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, outputHelper.getLocale()); 53 UserManager userManager = outputHelper.getRepository().getUserManager(); 54 55 AttributesImpl attrs = new AttributesImpl (); 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 attrs = new AttributesImpl (); 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 version1Name = version1.getDocumentName(); 85 String version2Name = version2.getDocumentName(); 86 attrs = new AttributesImpl (); 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 { 95 endElement("diff-report"); 96 } 97 98 private void startElement(String name, Attributes attrs) throws SAXException { 99 consumer.startElement("", name, name, attrs); 100 } 101 102 private void startElement(String name) throws SAXException { 103 consumer.startElement("", name, name, new AttributesImpl ()); 104 } 105 106 private void endElement(String name) throws SAXException { 107 consumer.endElement("", name, name); 108 } 109 110 public void partRemoved(Part removedPart) throws Exception { 111 AttributesImpl attrs = new AttributesImpl (); 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 { 119 AttributesImpl attrs = new AttributesImpl (); 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 { 127 AttributesImpl attrs = new AttributesImpl (); 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 part1Data, String part2Data) throws Exception { 135 boolean mimeTypesEqual = version1Part.getMimeType().equals(version2Part.getMimeType()); 136 boolean sizeEqual = version1Part.getSize() == version2Part.getSize(); 137 String version1FileName = nullToEmpty(version1Part.getFileName()); 138 String version2FileName = nullToEmpty(version2Part.getFileName()); 139 boolean fileNamesEqual = version1FileName.equals(version2FileName); 140 141 AttributesImpl attrs = new AttributesImpl (); 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 nullToEmpty(String value) { 168 if (value == null) 169 return ""; 170 return value; 171 } 172 173 public void partMightBeUpdated(Part version2Part) throws Exception { 174 AttributesImpl attrs = new AttributesImpl (); 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 { 182 consumer.startElement("", "parts", "parts", new AttributesImpl ()); 183 } 184 185 public void endPartChanges() throws Exception { 186 endElement("parts"); 187 } 188 189 public void beginFieldChanges() throws Exception { 190 startElement("fields"); 191 } 192 193 public void endFieldChanges() throws Exception { 194 endElement("fields"); 195 } 196 197 public void fieldRemoved(Field removedField) throws Exception { 198 long typeId = removedField.getTypeId(); 199 AttributesImpl attrs = new AttributesImpl (); 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 { 208 long typeId = addedField.getTypeId(); 209 AttributesImpl attrs = new AttributesImpl (); 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 { 218 long typeId = version1Field.getTypeId(); 219 AttributesImpl attrs = new AttributesImpl (); 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 { 229 startElement("links"); 230 } 231 232 public void linkRemoved(Link link) throws Exception { 233 AttributesImpl attrs = new AttributesImpl (); 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 { 241 AttributesImpl attrs = new AttributesImpl (); 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 { 249 endElement("links"); 250 } 251 } 252 | Popular Tags |