KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > publisher > serverimpl > requestmodel > DiffRequest


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.requestmodel;
17
18 import org.xml.sax.ContentHandler JavaDoc;
19 import org.outerj.daisy.docdiff.DocDiffOutputHelper;
20 import org.outerj.daisy.docdiff.DiffGenerator;
21 import org.outerj.daisy.publisher.serverimpl.XmlDocDiffOutput;
22 import org.outerj.daisy.repository.Document;
23 import org.outerj.daisy.repository.Version;
24 import org.outerj.daisy.repository.Repository;
25
26 public class DiffRequest implements Request {
27     private final long otherDocumentId;
28     private final long otherBranchId;
29     private final long otherLanguageId;
30     private final long otherVersionId;
31
32     public DiffRequest(long otherDocumentId, long otherBranchId, long otherLanguageId, long otherVersionId) {
33         this.otherDocumentId = otherDocumentId;
34         this.otherBranchId = otherBranchId;
35         this.otherLanguageId = otherLanguageId;
36         this.otherVersionId = otherVersionId;
37     }
38
39     public void process(ContentHandler JavaDoc contentHandler, PublisherContext publisherContext) throws Exception JavaDoc {
40         Repository repository = publisherContext.getRepository();
41
42         Document document1 = publisherContext.getDocument();
43         Version version1 = publisherContext.getVersion();
44
45         if (version1 == null) // diff with live version but live version doesn't exist
46
return;
47
48         long otherDocumentId = this.otherDocumentId == -1 ? publisherContext.getDocumentId() : this.otherDocumentId;
49         long otherBranchId = this.otherBranchId == -1 ? publisherContext.getBranchId() : this.otherBranchId;
50         long otherLanguageId = this.otherLanguageId == -1 ? publisherContext.getLanguageId() : this.otherLanguageId;
51         long otherVersionId = this.otherVersionId == -1 ? version1.getId() - 1 : this.otherVersionId;
52         if (otherVersionId < 1)
53             return;
54
55         Document document2 = repository.getDocument(otherDocumentId, otherBranchId, otherLanguageId, false);
56         Version version2 = document2.getVersion(otherVersionId);
57
58         // in case the second version was automatically determined, force a forward diff
59
if (this.otherVersionId == -1 && version1.getId() > version2.getId()) {
60             Document tmpDoc = document1;
61             Version tmpVersion = version1;
62
63             document1 = document2;
64             version1 = version2;
65
66             document2 = tmpDoc;
67             version2 = tmpVersion;
68         }
69
70         DocDiffOutputHelper diffOutputHelper = new DocDiffOutputHelper(document1, document2, version1, version2, repository, publisherContext.getLocale());
71         DiffGenerator differ = new DiffGenerator(version1, version2, new XmlDocDiffOutput(contentHandler, diffOutputHelper));
72         differ.generateDiff();
73     }
74 }
75
Popular Tags