1 17 18 19 20 package org.apache.lenya.cms.publication; 21 22 import java.io.File ; 23 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 26 import org.apache.lenya.cms.authoring.ParentChildCreatorInterface; 27 28 29 32 public final class DocumentTypeBuilder { 33 34 private DocumentTypeBuilder() { 35 } 36 37 40 public static final String DOCTYPE_DIRECTORY = "config/doctypes".replace('/', File.separatorChar); 41 42 45 public static final String CONFIG_FILE = "doctypes.xconf".replace('/', File.separatorChar); 46 public static final String DOCTYPES_ELEMENT = "doctypes"; 47 public static final String DOCTYPE_ELEMENT = "doc"; 48 public static final String TYPE_ATTRIBUTE = "type"; 49 public static final String CREATOR_ELEMENT = "creator"; 50 public static final String SRC_ATTRIBUTE = "src"; 51 public static final String WORKFLOW_ELEMENT = "workflow"; 52 53 61 public static DocumentType buildDocumentType(String name, Publication publication) 62 throws DocumentTypeBuildException { 63 DocumentType type = new DocumentType(name); 64 65 File configDirectory = new File (publication.getDirectory(), DOCTYPE_DIRECTORY); 66 File configFile = new File (configDirectory, CONFIG_FILE); 67 68 try { 69 Configuration configuration = new DefaultConfigurationBuilder().buildFromFile(configFile); 70 71 Configuration[] doctypeConfigurations = configuration.getChildren(DOCTYPE_ELEMENT); 72 Configuration doctypeConf = null; 73 74 for (int i = 0; i < doctypeConfigurations.length; i++) { 75 if (doctypeConfigurations[i].getAttribute(TYPE_ATTRIBUTE).equals(name)) { 76 doctypeConf = doctypeConfigurations[i]; 77 } 78 } 79 80 if (doctypeConf == null) { 81 throw new DocumentTypeBuildException("No definition found for doctype '" + name + 82 "'!"); 83 } 84 85 ParentChildCreatorInterface creator; 86 Configuration creatorConf = doctypeConf.getChild(CREATOR_ELEMENT, false); 87 88 if (creatorConf != null) { 89 String creatorClassName = creatorConf.getAttribute(SRC_ATTRIBUTE); 90 Class creatorClass = Class.forName(creatorClassName); 91 creator = (ParentChildCreatorInterface) creatorClass.newInstance(); 92 creator.init(creatorConf); 93 } else { 94 creator = new org.apache.lenya.cms.authoring.DefaultBranchCreator(); 95 } 96 97 type.setCreator(creator); 98 99 Configuration workflowConf = doctypeConf.getChild(WORKFLOW_ELEMENT, false); 100 101 if (workflowConf != null) { 102 String workflowFileName = workflowConf.getAttribute(SRC_ATTRIBUTE); 103 type.setWorkflowFileName(workflowFileName); 104 } 105 } catch (Exception e) { 106 throw new DocumentTypeBuildException(e); 107 } 108 109 return type; 110 } 111 } 112 | Popular Tags |