KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > publisher > serverimpl > docpreparation > PreparationPipe


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.docpreparation;
17
18 import org.xml.sax.ContentHandler JavaDoc;
19 import org.outerj.daisy.repository.*;
20 import org.outerj.daisy.repository.user.UserManager;
21 import org.outerj.daisy.repository.variant.VariantManager;
22 import org.outerj.daisy.repository.variant.BranchNotFoundException;
23 import org.outerj.daisy.repository.schema.*;
24 import org.outerj.daisy.publisher.serverimpl.DummyLexicalHandler;
25 import org.outerj.daisy.publisher.serverimpl.requestmodel.PublisherContext;
26 import org.outerj.daisy.publisher.serverimpl.requestmodel.PublisherVersionMode;
27 import org.apache.avalon.framework.logger.Logger;
28 import org.apache.xmlbeans.XmlObject;
29 import org.apache.xmlbeans.XmlCursor;
30 import org.apache.xmlbeans.XmlLong;
31 import org.outerx.daisy.x10.DocumentDocument;
32 import org.outerx.daisy.x10.FieldDocument;
33
34 import java.util.Locale JavaDoc;
35 import java.util.Set JavaDoc;
36
37 public class PreparationPipe {
38     private final Repository repository;
39     private final Logger logger;
40     private final Locale JavaDoc locale;
41     private final PublisherVersionMode versionMode;
42     private VariantKey navigationDoc;
43
44     public PreparationPipe(Repository repository, Logger logger, Locale JavaDoc locale, VariantKey navigationDoc, PublisherVersionMode versionMode) {
45         this.repository = repository;
46         this.logger = logger;
47         this.locale = locale;
48         this.navigationDoc = navigationDoc;
49         this.versionMode = versionMode;
50     }
51
52     public void prepareDocumentXml(DocumentDocument documentDocument) throws RepositoryException {
53         annotateDocument(documentDocument.getDocument(), repository, locale);
54         annotateFields(documentDocument.getDocument(), repository, locale, versionMode);
55     }
56
57     public Repository getRepository() {
58         return repository;
59     }
60
61     public Logger getLogger() {
62         return logger;
63     }
64
65     public Locale JavaDoc getLocale() {
66         return locale;
67     }
68
69     public VariantKey getNavigationDoc() {
70         return navigationDoc;
71     }
72
73     public static void annotateDocument(DocumentDocument.Document documentXml, Repository repository, Locale JavaDoc locale) throws RepositoryException {
74         VariantManager variantManager = repository.getVariantManager();
75         UserManager userManager = repository.getUserManager();
76
77         // Note: the branch/language from which a document variant has been created may have been deleted
78
// in the meantime, so therefore we handle the branch/languageNotFoundExceptions
79
String JavaDoc createdFromBranch = null;
80         String JavaDoc createdFromLanguage = null;
81         if (documentXml.getCreatedFromBranchId() != -1) {
82             try {
83                 createdFromBranch = repository.getVariantManager().getBranch(documentXml.getCreatedFromBranchId(), false).getName();
84             } catch (BranchNotFoundException e) {
85                 createdFromBranch = String.valueOf(documentXml.getCreatedFromBranchId());
86             }
87             try {
88                 createdFromLanguage = repository.getVariantManager().getLanguage(documentXml.getCreatedFromLanguageId(), false).getName();
89             } catch (BranchNotFoundException e) {
90                 createdFromLanguage = String.valueOf(documentXml.getCreatedFromLanguageId());
91             }
92         }
93         String JavaDoc typeName = null;
94         String JavaDoc typeLabel = null;
95         try {
96             DocumentType documentType = repository.getRepositorySchema().getDocumentTypeById(documentXml.getTypeId(), false);
97             typeName = documentType.getName();
98             typeLabel = documentType.getLabel(locale);
99         } catch (DocumentTypeNotFoundException e) {
100             // ignore
101
}
102
103         XmlCursor cursor = documentXml.newCursor();
104         cursor.toNextToken();
105         cursor.insertAttributeWithValue("ownerDisplayName", userManager.getUserDisplayName(documentXml.getOwner()));
106         cursor.insertAttributeWithValue("branch", variantManager.getBranch(documentXml.getBranchId(), false).getName());
107         cursor.insertAttributeWithValue("language", variantManager.getLanguage(documentXml.getLanguageId(), false).getName());
108         if (createdFromBranch != null) {
109             cursor.insertAttributeWithValue("createdFromBranch", createdFromBranch);
110             cursor.insertAttributeWithValue("createdFromLanguage", createdFromLanguage);
111         }
112         if (typeName != null)
113             cursor.insertAttributeWithValue("typeName", typeName);
114         if (typeLabel != null)
115             cursor.insertAttributeWithValue("typeLabel", typeLabel);
116         cursor.dispose();
117
118         CollectionManager collectionManager = repository.getCollectionManager();
119         XmlLong[] collectionIdsXml = documentXml.getCollectionIds().xgetCollectionIdArray();
120         for (int i = 0; i < collectionIdsXml.length; i++) {
121             long collectionId = collectionIdsXml[i].getLongValue();
122             String JavaDoc collectionName = null;
123             try {
124                 collectionName = collectionManager.getCollection(collectionId, false).getName();
125             } catch (CollectionNotFoundException e) {
126                 // ignore
127
}
128             if (collectionName != null) {
129                 cursor = collectionIdsXml[i].newCursor();
130                 cursor.toNextToken();
131                 cursor.insertAttributeWithValue("name", collectionName);
132                 cursor.dispose();
133             }
134         }
135     }
136
137     public static void annotateFields(DocumentDocument.Document documentXml, Repository repository, Locale JavaDoc locale,
138             PublisherVersionMode versionMode) throws RepositoryException {
139         FieldDocument.Field[] fieldsXml = documentXml.getFields().getFieldArray();
140         RepositorySchema schema = repository.getRepositorySchema();
141         for (int i = 0; i < fieldsXml.length; i++) {
142             FieldDocument.Field fieldXml = fieldsXml[i];
143             FieldType fieldType = schema.getFieldTypeById(fieldXml.getTypeId(), false);
144             String JavaDoc fieldLabel = fieldType.getLabel(locale);
145
146             boolean hasStaticSelectionList = fieldType.getSelectionList() instanceof StaticSelectionList;
147             ValueType fieldValueType = fieldType.getValueType();
148             long documentBranchId = documentXml.getBranchId();
149             long documentLanguageId = documentXml.getLanguageId();
150             String JavaDoc formattedValue = null;
151             XmlObject[] valuesXml = fieldsXml[i].selectPath("*");
152             Object JavaDoc[] values = FieldHelper.getFieldValuesFromXml(fieldType.getValueType(), fieldXml);
153             for (int k = 0; k < values.length; k++) {
154                 formattedValue = null;
155                 if (hasStaticSelectionList) {
156                     formattedValue = fieldType.getSelectionList().getLabel(values[k], locale);
157                 } else if (fieldValueType == ValueType.LINK) {
158                     VariantKey variantKey = (VariantKey)values[k];
159                     long branchId = variantKey.getBranchId() == -1 ? documentBranchId : variantKey.getBranchId();
160                     long languageId = variantKey.getLanguageId() == -1 ? documentLanguageId : variantKey.getLanguageId();
161                     try {
162                         Document document = repository.getDocument(variantKey.getDocumentId(), branchId, languageId, false);
163
164                         Version version = versionMode == PublisherVersionMode.LIVE ? document.getLiveVersion() : document.getLastVersion();
165                         if (version != null)
166                             formattedValue = version.getDocumentName();
167                     } catch (Throwable JavaDoc e) {
168                         // ignore (doc does not exist, no access allowed, ...)
169
}
170                 }
171
172                 String JavaDoc linkTarget = null;
173                 if (fieldValueType == ValueType.LINK) {
174                     linkTarget = FieldHelper.getFormattedValue(values[k], fieldType.getValueType(), locale, repository);
175                     if (formattedValue == null)
176                         formattedValue = linkTarget;
177                 } else if (formattedValue == null) {
178                     formattedValue = FieldHelper.getFormattedValue(values[k], fieldType.getValueType(), locale, repository);
179                 }
180
181                 XmlCursor cursor = valuesXml[k].newCursor();
182                 cursor.toNextToken();
183                 if (linkTarget != null)
184                     cursor.insertAttributeWithValue("target", linkTarget);
185                 cursor.insertAttributeWithValue("valueFormatted", formattedValue);
186                 cursor.dispose();
187             }
188
189             XmlCursor cursor = fieldXml.newCursor();
190             cursor.toNextToken();
191             cursor.insertAttributeWithValue("label", fieldLabel);
192             cursor.insertAttributeWithValue("name", fieldType.getName());
193             cursor.insertAttributeWithValue("valueType", fieldValueType.toString());
194             // adding valueFormatted attribute here too for backwards compatibility
195
if (!fieldType.isMultiValue())
196                 cursor.insertAttributeWithValue("valueFormatted", formattedValue);
197             cursor.dispose();
198         }
199     }
200
201     /**
202      * Streams a document's XML through a chain of SAX processors which will deliver prepared
203      * content for publishing. Note that this will happen recursively through the IncludesProcessor.
204      */

205     public void process(ContentProcessor parentProcessor, Document document, Version version,
206             PublisherContext publisherContext, Set JavaDoc inlineParts, ContentHandler JavaDoc contentHandler) throws Exception JavaDoc {
207
208         DocumentDocument documentDocument = document.getXml(version.getId());
209         prepareDocumentXml(documentDocument);
210
211         DaisyLinkEnhancerHandler daisyLinkEnhancer = new DaisyLinkEnhancerHandler(repository, document.getBranchId(),
212                 document.getLanguageId(), navigationDoc, publisherContext.getVersionMode(), contentHandler, logger);
213         ContentProcessor contentProcessor = new ContentProcessor(document, version, daisyLinkEnhancer, this, publisherContext, parentProcessor);
214         MergePartsHandler mergePartsHandler = new MergePartsHandler(version, inlineParts, contentProcessor, repository, locale, logger);
215
216         documentDocument.save(mergePartsHandler, new DummyLexicalHandler());
217     }
218
219     public class DocumentHandlerInfo {
220         public long documentHolderId;
221         public ContentHandler JavaDoc documentHandler;
222     }
223 }
224
Popular Tags