1 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 ; 31 32 35 public class LinkFieldHelper { 36 public static VariantKey parseVariantKey(String link, VariantManager variantManager) { 37 Matcher matcher = Constants.DAISY_LINK_PATTERN.matcher(link); 38 if (!matcher.matches()) 39 throw new IllegalArgumentException ("Invalid link: " + link); 40 41 long documentId = Long.parseLong(matcher.group(1)); 42 String branchInput = matcher.group(3); 43 String 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 (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 (e); 61 } 62 } else { 63 languageId = -1; 64 } 65 66 return new VariantKey(documentId, branchId, languageId); 67 } 68 69 public static String variantKeyToString(VariantKey variantKey, VariantManager variantManager) { 70 StringBuffer text = new StringBuffer (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 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 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 [] values = (Object [])widget.getValue(); 103 boolean result = true; 104 for (int i = 0; i < values.length; i++) { 105 result = validateLink((String )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 )widget.getValue(), (ValidationErrorAware)widget, variantManager); 112 } else { 113 throw new RuntimeException ("Unexpected type of widget: " + widget.getClass().getName()); 114 } 115 } 116 117 private static boolean validateLink(String link, ValidationErrorAware widget, VariantManager variantManager) { 118 link = link.trim(); 119 Matcher matcher = Constants.DAISY_LINK_PATTERN.matcher(link); 120 if (matcher.matches()) { 121 String branchInput = matcher.group(3); 122 String languageInput = matcher.group(5); 123 124 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 [] {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 [] {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 [] {link})); 151 return false; 152 } 153 return true; 154 } 155 156 } 157 | Popular Tags |