KickJava   Java API By Example, From Geeks To Geeks.

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


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.outerj.daisy.repository.VariantKey;
19 import org.outerj.daisy.repository.RepositoryException;
20 import org.outerj.daisy.repository.variant.VariantManager;
21 import org.outerj.daisy.repository.variant.BranchNotFoundException;
22 import org.outerj.daisy.repository.variant.LanguageNotFoundException;
23 import org.outerj.daisy.util.Constants;
24 import org.apache.cocoon.forms.formmodel.Widget;
25 import org.apache.cocoon.forms.formmodel.MultiValueField;
26 import org.apache.cocoon.forms.formmodel.Field;
27 import org.apache.cocoon.forms.validation.ValidationError;
28 import org.apache.cocoon.forms.validation.ValidationErrorAware;
29
30 import java.util.regex.Matcher JavaDoc;
31
32 /**
33  * Utility methods for textual editing of link-type fields.
34  */

35 public class LinkFieldHelper {
36     public static VariantKey parseVariantKey(String JavaDoc link, VariantManager variantManager) {
37         Matcher JavaDoc matcher = Constants.DAISY_LINK_PATTERN.matcher(link);
38         if (!matcher.matches())
39             throw new IllegalArgumentException JavaDoc("Invalid link: " + link);
40
41         long documentId = Long.parseLong(matcher.group(1));
42         String JavaDoc branchInput = matcher.group(3);
43         String JavaDoc languageInput = matcher.group(5);
44         long branchId, languageId;
45
46         if (branchInput != null && branchInput.length() > 0) {
47             try {
48                 branchId = variantManager.getBranch(branchInput, false).getId();
49             } catch (RepositoryException e) {
50                 throw new RuntimeException JavaDoc(e);
51             }
52         } else {
53             branchId = -1;
54         }
55
56         if (languageInput != null && languageInput.length() > 0) {
57             try {
58                 languageId = variantManager.getLanguage(languageInput, false).getId();
59             } catch (RepositoryException e) {
60                 throw new RuntimeException JavaDoc(e);
61             }
62         } else {
63             languageId = -1;
64         }
65
66         return new VariantKey(documentId, branchId, languageId);
67     }
68
69     public static String JavaDoc variantKeyToString(VariantKey variantKey, VariantManager variantManager) {
70         StringBuffer JavaDoc text = new StringBuffer JavaDoc(20);
71         text.append("daisy:");
72         text.append(variantKey.getDocumentId());
73         if (variantKey.getBranchId() != -1 || variantKey.getLanguageId() != -1) {
74             text.append("@");
75             if (variantKey.getBranchId() != -1) {
76                 String JavaDoc branchName;
77                 try {
78                     branchName = variantManager.getBranch(variantKey.getBranchId(), false).getName();
79                 } catch (RepositoryException e) {
80                     branchName = String.valueOf(variantKey.getBranchId());
81                 }
82                 text.append(branchName);
83             }
84             if (variantKey.getLanguageId() != -1) {
85                 text.append(":");
86                 String JavaDoc languageName;
87                 try {
88                     languageName = variantManager.getLanguage(variantKey.getLanguageId(), false).getName();
89                 } catch (RepositoryException e) {
90                     languageName = String.valueOf(variantKey.getLanguageId());
91                 }
92                 text.append(languageName);
93             }
94         }
95         return text.toString();
96     }
97
98     public static boolean validate(Widget widget, VariantManager variantManager) {
99         if (widget.getValue() == null)
100             return true;
101         if (widget instanceof MultiValueField) {
102             Object JavaDoc[] values = (Object JavaDoc[])widget.getValue();
103             boolean result = true;
104             for (int i = 0; i < values.length; i++) {
105                 result = validateLink((String JavaDoc)values[i], (ValidationErrorAware)widget, variantManager);
106                 if (!result)
107                     return result;
108             }
109             return result;
110         } else if (widget instanceof Field) {
111             return validateLink((String JavaDoc)widget.getValue(), (ValidationErrorAware)widget, variantManager);
112         } else {
113             throw new RuntimeException JavaDoc("Unexpected type of widget: " + widget.getClass().getName());
114         }
115     }
116
117     private static boolean validateLink(String JavaDoc link, ValidationErrorAware widget, VariantManager variantManager) {
118         link = link.trim();
119         Matcher JavaDoc matcher = Constants.DAISY_LINK_PATTERN.matcher(link);
120         if (matcher.matches()) {
121             String JavaDoc branchInput = matcher.group(3);
122             String JavaDoc languageInput = matcher.group(5);
123
124             // test the specified branch and language (if any) exist
125

126             if (branchInput != null && branchInput.length() > 0) {
127                 try {
128                     variantManager.getBranch(branchInput, false);
129                 } catch (BranchNotFoundException e) {
130                     widget.setValidationError(new ValidationError("editdoc.link-no-valid-branch", new String JavaDoc[] {branchInput}));
131                     return false;
132                 } catch (RepositoryException e) {
133                     widget.setValidationError(new ValidationError("Error testing branch existence: " + e.toString(), false));
134                     return false;
135                 }
136             }
137
138             if (languageInput != null && languageInput.length() > 0) {
139                 try {
140                     variantManager.getLanguage(languageInput, false);
141                 } catch (LanguageNotFoundException e) {
142                     widget.setValidationError(new ValidationError("editdoc.link-no-valid-language", new String JavaDoc[] {languageInput}));
143                     return false;
144                 } catch (RepositoryException e) {
145                     widget.setValidationError(new ValidationError("Error testing language existence: " + e.toString()));
146                     return false;
147                 }
148             }
149         } else {
150             widget.setValidationError(new ValidationError("editdoc.link-not-valid", new String JavaDoc[] {link}));
151             return false;
152         }
153         return true;
154     }
155
156 }
157
Popular Tags