1 17 18 19 20 package org.apache.lenya.cms.authoring; 21 22 import java.io.File ; 23 import java.io.FileNotFoundException ; 24 import java.util.Map ; 25 26 import org.apache.avalon.framework.configuration.Configuration; 27 import org.apache.lenya.xml.DocumentHelper; 28 import org.apache.log4j.Category; 29 import org.w3c.dom.Document ; 30 31 public class DefaultCreator implements ParentChildCreatorInterface { 32 private static Category log = Category.getInstance(DefaultCreator.class); 33 public static final String RESOURCE_NAME = "resource-name"; 34 public static final String RESOURCE_META_NAME = "resource-meta-name"; 35 public static final String SAMPLE_NAME = "sample-name"; 36 public static final String SAMPLE_META_NAME = "sample-meta-name"; 37 38 private String resourceName = null; 39 private String resourceMetaName = null; 40 private String sampleResourceName = null; 41 private String sampleMetaName = null; 42 43 48 public void init(Configuration conf) { 49 if (conf == null) { 50 return; 51 } 52 53 if (conf.getChild(RESOURCE_NAME, false) != null) { 54 resourceName = conf.getChild(RESOURCE_NAME).getValue("index.xml"); 55 } 56 57 if (conf.getChild(RESOURCE_META_NAME, false) != null) { 58 resourceMetaName = 59 conf.getChild(RESOURCE_META_NAME).getValue("index-meta.xml"); 60 } 61 62 if (conf.getChild(SAMPLE_NAME, false) != null) { 63 sampleResourceName = 64 conf.getChild(SAMPLE_NAME).getValue("sampleindex.xml"); 65 } 66 67 if (conf.getChild(SAMPLE_META_NAME, false) != null) { 68 sampleMetaName = 69 conf.getChild(SAMPLE_META_NAME).getValue("samplemeta.xml"); 70 } 71 } 72 73 83 public String generateTreeId(String childId, short childType) 84 throws Exception { 85 return childId; 86 } 87 88 97 public short getChildType(short childType) throws Exception { 98 return childType; 99 } 100 101 110 public String getChildName(String childname) throws Exception { 111 if (childname.length() != 0) { 112 return childname; 113 } else { 114 return "abstract_default"; 115 } 116 } 117 118 132 public void create( 133 File samplesDir, 134 File parentDir, 135 String childId, 136 short childType, 137 String childName, 138 String language, 139 Map parameters) 140 throws Exception { 141 String id = generateTreeId(childId, childType); 143 String filename = getChildFileName(parentDir, id, language); 144 String filenameMeta = getChildMetaFileName(parentDir, id, language); 145 146 String doctypeSample = samplesDir + File.separator + sampleResourceName; 147 String doctypeMeta = samplesDir + File.separator + sampleMetaName; 148 149 File sampleFile = new File (doctypeSample); 150 if (!sampleFile.exists()) { 151 log.error("No such sample file: " + sampleFile + " Have you configured the sample within doctypes.xconf?"); 152 throw new FileNotFoundException ("" + sampleFile); 153 } 154 155 log.debug("Read sample file: " + doctypeSample); 157 158 Document doc = DocumentHelper.readDocument(new File (doctypeSample)); 159 160 log.debug("sample document: " + doc); 161 162 log.debug("transform sample file: "); 164 transformXML(doc, id, childType, childName, parameters); 165 166 File parent = new File (new File (filename).getParent()); 169 170 if (!parent.exists()) { 171 parent.mkdirs(); 172 } 173 174 log.debug("write file: " + filename); 176 DocumentHelper.writeDocument(doc, new File (filename)); 177 178 if (sampleMetaName != null) { 181 doc = DocumentHelper.readDocument(new File (doctypeMeta)); 182 183 transformMetaXML(doc, id, childType, childName, parameters); 184 185 parent = new File (new File (filenameMeta).getParent()); 186 187 if (!parent.exists()) { 188 parent.mkdirs(); 189 } 190 191 DocumentHelper.writeDocument(doc, new File (filenameMeta)); 192 } 193 } 194 195 206 protected void transformXML( 207 Document doc, 208 String childId, 209 short childType, 210 String childName, 211 Map parameters) 212 throws Exception {} 213 214 225 protected void transformMetaXML( 226 Document doc, 227 String childId, 228 short childType, 229 String childName, 230 Map parameters) 231 throws Exception {} 232 233 242 protected String getChildFileName( 243 File parentDir, 244 String childId, 245 String language) { 246 return null; 247 } 248 249 258 protected String getChildMetaFileName( 259 File parentDir, 260 String childId, 261 String language) { 262 return null; 263 } 264 265 272 protected String getLanguageSuffix(String language) { 273 return (language != null) ? "_" + language : ""; 274 } 275 276 279 public String getSampleResourceName() { 280 return sampleResourceName; 281 } 282 } 283 | Popular Tags |