KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > editor > SelectionListAdapter


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.frontend.editor;
17
18 import org.apache.cocoon.forms.datatype.Datatype;
19 import org.apache.cocoon.forms.datatype.convertor.Convertor;
20 import org.apache.cocoon.forms.datatype.convertor.DefaultFormatCache;
21 import org.apache.cocoon.forms.FormsConstants;
22 import org.apache.cocoon.xml.XMLUtils;
23 import org.apache.cocoon.xml.AttributesImpl;
24 import org.xml.sax.ContentHandler JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.outerj.daisy.repository.schema.SelectionList;
27 import org.outerj.daisy.repository.schema.ListItem;
28 import org.outerj.daisy.repository.ValueType;
29 import org.outerj.daisy.repository.VariantKey;
30 import org.outerj.daisy.repository.RepositoryException;
31 import org.outerj.daisy.repository.variant.VariantManager;
32
33 import java.util.Locale JavaDoc;
34
35 /**
36  * Adapts a Daisy selection list to a CForms selection list.
37  */

38 public class SelectionListAdapter implements org.apache.cocoon.forms.datatype.SelectionList {
39     private Datatype datatype;
40     private SelectionList selectionList;
41     private boolean includeEmpty;
42     private ValueType fieldValueType;
43     private VariantManager variantManager;
44     private long documentBranchId;
45     private long documentLanguageId;
46
47     public SelectionListAdapter(Datatype datatype, SelectionList selectionList, boolean includeEmpty,
48             ValueType fieldValueType, VariantManager variantManager, long documentBranchId, long documentLanguageId) {
49         this.datatype = datatype;
50         this.selectionList = selectionList;
51         this.includeEmpty = includeEmpty;
52         this.fieldValueType = fieldValueType;
53         this.variantManager = variantManager;
54         this.documentBranchId = documentBranchId;
55         this.documentLanguageId = documentLanguageId;
56     }
57
58     public Datatype getDatatype() {
59         return datatype;
60     }
61
62     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
63         ListItem[] items = selectionList.getItems(documentBranchId, documentLanguageId, locale);
64         Convertor.FormatCache formatCache = new DefaultFormatCache();
65         contentHandler.startElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL, XMLUtils.EMPTY_ATTRIBUTES);
66
67         if (includeEmpty) {
68             AttributesImpl itemAttrs = new AttributesImpl();
69             itemAttrs.addCDATAAttribute("value", "");
70             contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, itemAttrs);
71             contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES);
72             contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL);
73             contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
74         }
75
76         for (int i = 0; i < items.length; i++) {
77             String JavaDoc stringValue;
78             if (fieldValueType == ValueType.LINK)
79                 stringValue = variantKeyToString((VariantKey)items[i].getValue());
80             else
81                 stringValue = datatype.getConvertor().convertToString(items[i].getValue(), locale, formatCache);
82             AttributesImpl itemAttrs = new AttributesImpl();
83             itemAttrs.addCDATAAttribute("value", stringValue);
84             contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, itemAttrs);
85             contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES);
86
87             String JavaDoc label = items[i].getLabel(locale);
88             if (label == null)
89                 label = stringValue;
90
91             if (label != null) {
92                 contentHandler.characters(label.toCharArray(), 0, label.length());
93             }
94
95             contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL);
96             contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
97
98         }
99         contentHandler.endElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL);
100     }
101
102     /**
103      * Converts a variant key to string representation, but does not add the branch and language if they
104      * are the same as the document that is being edited.
105      */

106     private String JavaDoc variantKeyToString(VariantKey variantKey) {
107         StringBuffer JavaDoc text = new StringBuffer JavaDoc(20);
108         text.append("daisy:");
109         text.append(variantKey.getDocumentId());
110         long branchId = variantKey.getBranchId() == -1 ? documentBranchId : variantKey.getBranchId();
111         long languageId = variantKey.getLanguageId() == -1 ? documentLanguageId : variantKey.getLanguageId();
112         if (branchId != documentBranchId || languageId != documentLanguageId) {
113             text.append("@");
114             if (branchId != documentBranchId) {
115                 String JavaDoc branchName;
116                 try {
117                     branchName = variantManager.getBranch(variantKey.getBranchId(), false).getName();
118                 } catch (RepositoryException e) {
119                     branchName = String.valueOf(variantKey.getBranchId());
120                 }
121                 text.append(branchName);
122             }
123             if (languageId != documentLanguageId) {
124                 text.append(":");
125                 String JavaDoc languageName;
126                 try {
127                     languageName = variantManager.getLanguage(variantKey.getLanguageId(), false).getName();
128                 } catch (RepositoryException e) {
129                     languageName = String.valueOf(variantKey.getLanguageId());
130                 }
131                 text.append(languageName);
132             }
133         }
134         return text.toString();
135     }
136 }
137
Popular Tags