1 16 package org.apache.cocoon.bean.helpers; 17 18 import java.io.BufferedReader ; 19 import java.io.FileReader ; 20 import java.util.ArrayList ; 21 import java.util.List ; 22 23 import org.apache.cocoon.bean.CocoonBean; 24 import org.apache.cocoon.bean.helpers.OutputStreamListener; 25 import org.apache.commons.lang.BooleanUtils; 26 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.NamedNodeMap ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 32 38 public class BeanConfigurator { 39 40 private static final String NODE_ROOT = "cocoon"; 41 private static final String ATTR_VERBOSE = "verbose"; 42 43 private static final String NODE_LOGGING = "logging"; 44 private static final String ATTR_LOG_KIT = "log-kit"; 45 private static final String ATTR_LOG_LEVEL = "level"; 46 private static final String ATTR_LOGGER = "logger"; 47 48 private static final String NODE_CONTEXT_DIR = "context-dir"; 49 private static final String NODE_DEST_DIR = "dest-dir"; 50 private static final String NODE_WORK_DIR = "work-dir"; 51 private static final String NODE_CONFIG_FILE = "config-file"; 52 private static final String NODE_URI_FILE = "uri-file"; 53 private static final String NODE_CHECKSUMS_URI = "checksums-uri"; 54 55 private static final String ATTR_CONTEXT_DIR = "context-dir"; 56 private static final String ATTR_DEST_DIR = "dest-dir"; 57 private static final String ATTR_WORK_DIR = "work-dir"; 58 private static final String ATTR_CONFIG_FILE = "config-file"; 59 private static final String ATTR_URI_FILE = "uri-file"; 60 private static final String ATTR_CHECKSUMS_URI = "checksums-uri"; 61 private static final String ATTR_AGENT = "user-agent"; 62 private static final String ATTR_ACCEPT = "accept"; 63 private static final String ATTR_DEFAULT_FILENAME = "default-filename"; 64 65 private static final String NODE_BROKEN_LINKS = "broken-links"; 66 private static final String ATTR_BROKEN_LINK_REPORT_TYPE = "type"; 67 private static final String ATTR_BROKEN_LINK_REPORT_FILE = "file"; 68 private static final String ATTR_BROKEN_LINK_GENERATE = "generate"; 69 private static final String ATTR_BROKEN_LINK_EXTENSION = "extension"; 70 71 private static final String NODE_AGENT = "user-agent"; 72 private static final String NODE_ACCEPT = "accept"; 73 74 private static final String ATTR_FOLLOW_LINKS = "follow-links"; 75 private static final String ATTR_PRECOMPILE_ONLY = "precompile-only"; 76 private static final String ATTR_CONFIRM_EXTENSIONS = "confirm-extensions"; 77 private static final String NODE_LOAD_CLASS = "load-class"; 78 private static final String NODE_DEFAULT_FILENAME = "default-filename"; 79 80 private static final String NODE_INCLUDE = "include"; 81 private static final String NODE_EXCLUDE = "exclude"; 82 private static final String ATTR_INCLUDE_EXCLUDE_PATTERN = "pattern"; 83 84 private static final String NODE_INCLUDE_LINKS = "include-links"; 85 private static final String ATTR_LINK_EXTENSION = "extension"; 86 87 private static final String NODE_URI = "uri"; 88 private static final String ATTR_URI_TYPE = "type"; 89 private static final String ATTR_URI_SOURCEPREFIX = "src-prefix"; 90 private static final String ATTR_URI_SOURCEURI = "src"; 91 private static final String ATTR_URI_DESTURI = "dest"; 92 93 private static final String NODE_URIS = "uris"; 94 private static final String ATTR_NAME = "name"; 95 96 public static String configure(Document xconf, CocoonBean cocoon, String destDir, String uriGroup, OutputStreamListener listener) 97 throws IllegalArgumentException { 98 99 Node root = xconf.getDocumentElement(); 100 if (!NODE_ROOT.equals(root.getNodeName())) { 101 throw new IllegalArgumentException ("Expected root node of "+ NODE_ROOT); 102 } 103 104 if (hasAttribute(root, ATTR_VERBOSE)) { 105 cocoon.setVerbose(getBooleanAttributeValue(root, ATTR_VERBOSE)); 106 } 107 if (hasAttribute(root, ATTR_FOLLOW_LINKS)) { 108 cocoon.setFollowLinks(getBooleanAttributeValue(root, ATTR_FOLLOW_LINKS)); 109 } 110 if (hasAttribute(root, ATTR_PRECOMPILE_ONLY)) { 111 cocoon.setPrecompileOnly(getBooleanAttributeValue(root, ATTR_PRECOMPILE_ONLY)); 112 } 113 if (hasAttribute(root, ATTR_CONFIRM_EXTENSIONS)) { 114 cocoon.setConfirmExtensions(getBooleanAttributeValue(root, ATTR_CONFIRM_EXTENSIONS)); 115 } 116 if (hasAttribute(root, ATTR_CONTEXT_DIR)) { 117 cocoon.setContextDir(getAttributeValue(root, ATTR_CONTEXT_DIR)); 118 } 119 if (hasAttribute(root, ATTR_DEST_DIR)) { 120 destDir = getAttributeValue(root, ATTR_DEST_DIR); 121 } 122 if (hasAttribute(root, ATTR_WORK_DIR)) { 123 cocoon.setWorkDir(getAttributeValue(root, ATTR_WORK_DIR)); 124 } 125 if (hasAttribute(root, ATTR_CONFIG_FILE)) { 126 cocoon.setConfigFile(getAttributeValue(root, ATTR_CONFIG_FILE)); 127 } 128 if (hasAttribute(root, ATTR_URI_FILE)) { 129 cocoon.addTargets(processURIFile(getAttributeValue(root, ATTR_URI_FILE)), destDir); 130 } 131 if (hasAttribute(root, ATTR_CHECKSUMS_URI)) { 132 cocoon.setChecksumURI(getAttributeValue(root, ATTR_CHECKSUMS_URI)); 133 } 134 if (hasAttribute(root, ATTR_AGENT)) { 135 cocoon.setAgentOptions(getAttributeValue(root, ATTR_AGENT)); 136 } 137 if (hasAttribute(root, ATTR_ACCEPT)) { 138 cocoon.setAcceptOptions(getAttributeValue(root, ATTR_ACCEPT)); 139 } 140 if (hasAttribute(root, ATTR_DEFAULT_FILENAME)) { 141 cocoon.setDefaultFilename(getAttributeValue(root, ATTR_DEFAULT_FILENAME)); 142 } 143 144 if (destDir == null || destDir.length() == 0) { 145 destDir = getNodeValue(root, NODE_DEST_DIR); 146 } 147 148 NodeList nodes = root.getChildNodes(); 149 for (int i = 0; i < nodes.getLength(); i++) { 150 Node node = nodes.item(i); 151 if (node.getNodeType()== Node.ELEMENT_NODE) { 152 String nodeName = node.getNodeName(); 153 if (nodeName.equals(NODE_BROKEN_LINKS)) { 154 parseBrokenLinkNode(cocoon, node, listener); 155 156 } else if (nodeName.equals(NODE_LOAD_CLASS)) { 157 cocoon.addLoadedClass(getNodeValue(node)); 158 159 } else if (nodeName.equals(NODE_LOGGING)) { 160 parseLoggingNode(cocoon, node); 161 162 } else if (nodeName.equals(NODE_CONTEXT_DIR)) { 163 if (hasAttribute(root, ATTR_CONTEXT_DIR)) { 164 throw new IllegalArgumentException ("Cannot have "+NODE_CONTEXT_DIR+" as both element and attribute"); 165 } else { 166 cocoon.setContextDir(getNodeValue(node)); 167 } 168 169 } else if (nodeName.equals(NODE_CONFIG_FILE)) { 170 if (hasAttribute(root, ATTR_CONFIG_FILE)) { 171 throw new IllegalArgumentException ("Cannot have "+NODE_CONFIG_FILE+" as both element and attribute"); 172 } else { 173 cocoon.setConfigFile(getNodeValue(node)); 174 } 175 } else if (nodeName.equals(NODE_DEST_DIR)) { 176 178 } else if (nodeName.equals(NODE_WORK_DIR)) { 179 if (hasAttribute(root, ATTR_WORK_DIR)) { 180 throw new IllegalArgumentException ("Cannot have "+NODE_WORK_DIR+" as both element and attribute"); 181 } else { 182 cocoon.setWorkDir(getNodeValue(node)); 183 } 184 } else if (nodeName.equals(NODE_CHECKSUMS_URI)) { 185 if (hasAttribute(root, ATTR_CHECKSUMS_URI)) { 186 throw new IllegalArgumentException ("Cannot have "+NODE_CHECKSUMS_URI+" as both element and attribute"); 187 } else { 188 cocoon.setChecksumURI(getNodeValue(node)); 189 } 190 } else if (nodeName.equals(NODE_AGENT)) { 191 cocoon.setAgentOptions(getNodeValue(node)); 192 193 } else if (nodeName.equals(NODE_ACCEPT)) { 194 cocoon.setAcceptOptions(getNodeValue(node)); 195 196 } else if (nodeName.equals(NODE_DEFAULT_FILENAME)) { 197 cocoon.setDefaultFilename(getNodeValue(node)); 198 199 } else if (nodeName.equals(NODE_INCLUDE)) { 200 String pattern = parseIncludeExcludeNode(cocoon, node, NODE_INCLUDE); 201 cocoon.addIncludePattern(pattern); 202 203 } else if (nodeName.equals(NODE_EXCLUDE)) { 204 String pattern = parseIncludeExcludeNode(cocoon, node, NODE_EXCLUDE); 205 cocoon.addExcludePattern(pattern); 206 207 } else if (nodeName.equals(NODE_INCLUDE_LINKS)) { 208 parseIncludeLinksNode(cocoon, node); 209 210 } else if (nodeName.equals(NODE_URI)) { 211 parseURINode(cocoon, node, destDir); 212 213 } else if (nodeName.equals(NODE_URIS)) { 214 parseURIsNode(cocoon, node, destDir, uriGroup); 215 216 } else if (nodeName.equals(NODE_URI_FILE)) { 217 if (hasAttribute(root, ATTR_URI_FILE)) { 218 throw new IllegalArgumentException ("Cannot have "+NODE_URI_FILE+" as both element and attribute"); 219 } else { 220 cocoon.addTargets(processURIFile(getNodeValue(node)), destDir); 221 } 222 } else { 223 throw new IllegalArgumentException ("Unknown element: <" + nodeName + ">"); 224 } 225 } 226 } 227 return destDir; 228 } 229 230 private static void parseLoggingNode(CocoonBean cocoon, Node node) throws IllegalArgumentException { 231 if (hasAttribute(node, ATTR_LOG_KIT)) { 232 cocoon.setLogKit(getAttributeValue(node, ATTR_LOG_KIT)); 233 } 234 if (hasAttribute(node, ATTR_LOGGER)) { 235 cocoon.setLogger(getAttributeValue(node, ATTR_LOGGER)); 236 } 237 if (hasAttribute(node, ATTR_LOG_LEVEL)) { 238 cocoon.setLogLevel(getAttributeValue(node, ATTR_LOG_LEVEL)); 239 } 240 NodeList nodes = node.getChildNodes(); 241 if (nodes.getLength()!=0) { 242 throw new IllegalArgumentException ("Unexpected children of <" + NODE_LOGGING + "> node"); 243 } 244 } 245 246 private static void parseIncludeLinksNode(CocoonBean cocoon, Node node) throws IllegalArgumentException { 247 if (hasAttribute(node, ATTR_LINK_EXTENSION)) { 248 cocoon.addIncludeLinkExtension(getAttributeValue(node, ATTR_LINK_EXTENSION)); 249 } 250 } 251 252 private static void parseBrokenLinkNode(CocoonBean cocoon, Node node, OutputStreamListener listener) throws IllegalArgumentException { 253 if (hasAttribute(node, ATTR_BROKEN_LINK_REPORT_FILE)) { 254 listener.setReportFile(getAttributeValue(node, ATTR_BROKEN_LINK_REPORT_FILE)); 255 } 256 if (hasAttribute(node, ATTR_BROKEN_LINK_REPORT_TYPE)) { 257 listener.setReportType(getAttributeValue(node, ATTR_BROKEN_LINK_REPORT_TYPE)); 258 } 259 if (hasAttribute(node, ATTR_BROKEN_LINK_GENERATE)) { 260 cocoon.setBrokenLinkGenerate(getBooleanAttributeValue(node, ATTR_BROKEN_LINK_GENERATE)); 261 } 262 if (hasAttribute(node, ATTR_BROKEN_LINK_EXTENSION)) { 263 cocoon.setBrokenLinkExtension(getAttributeValue(node, ATTR_BROKEN_LINK_EXTENSION)); 264 } 265 NodeList nodes = node.getChildNodes(); 266 if (nodes.getLength()!=0) { 267 throw new IllegalArgumentException ("Unexpected children of <" + NODE_BROKEN_LINKS + "> node"); 268 } 269 } 270 271 private static String parseIncludeExcludeNode(CocoonBean cocoon, Node node, final String NODE_TYPE) throws IllegalArgumentException { 272 NodeList nodes = node.getChildNodes(); 273 if (nodes.getLength() != 0) { 274 throw new IllegalArgumentException ("Unexpected children of <" + NODE_INCLUDE + "> node"); 275 } 276 277 if (hasAttribute(node, ATTR_INCLUDE_EXCLUDE_PATTERN)) { 278 return getAttributeValue(node, ATTR_INCLUDE_EXCLUDE_PATTERN); 279 } else { 280 throw new IllegalArgumentException ("Expected a "+ATTR_INCLUDE_EXCLUDE_PATTERN+" attribute for <"+NODE_TYPE+"> node"); 281 } 282 } 283 284 private static void parseURIsNode(CocoonBean cocoon, Node node, String destDir, String uriGroup) throws IllegalArgumentException { 285 286 boolean followLinks = cocoon.followLinks(); 287 boolean confirmExtensions = cocoon.confirmExtensions(); 288 String logger = cocoon.getLoggerName(); 289 String destURI = destDir; 290 String root = null; 291 String type = null; 292 String name = null; 293 294 if (hasAttribute(node, ATTR_FOLLOW_LINKS)) { 295 followLinks = getBooleanAttributeValue(node, ATTR_FOLLOW_LINKS); 296 } 297 if (hasAttribute(node, ATTR_CONFIRM_EXTENSIONS)) { 298 confirmExtensions = getBooleanAttributeValue(node, ATTR_CONFIRM_EXTENSIONS); 299 } 300 if (hasAttribute(node, ATTR_URI_TYPE)) { 301 type = getAttributeValue(node, ATTR_URI_TYPE); 302 } 303 if (hasAttribute(node, ATTR_URI_SOURCEPREFIX)) { 304 root = getAttributeValue(node, ATTR_URI_SOURCEPREFIX); 305 } 306 if (hasAttribute(node, ATTR_URI_DESTURI)) { 307 destURI = getAttributeValue(node, ATTR_URI_DESTURI); 308 } 309 if (hasAttribute(node, ATTR_LOGGER)) { 310 logger = getAttributeValue(node, ATTR_LOGGER); 311 } 312 if (hasAttribute(node, ATTR_NAME)) { 313 name = getAttributeValue(node, ATTR_NAME); 314 if (name != null && uriGroup != null && !name.equals(uriGroup)) { 315 return; 317 } 318 } 319 NodeList nodes = node.getChildNodes(); 320 for (int i = 0; i < nodes.getLength(); i++) { 321 Node child = nodes.item(i); 322 if (child.getNodeType()== Node.ELEMENT_NODE) { 323 String childName = child.getNodeName(); 324 if (childName.equals(NODE_URI)) { 325 String _sourceURI = null; 326 String _destURI = destURI; 327 String _root = root; 328 String _type = type; 329 330 if (child.getAttributes().getLength() == 0) { 331 _sourceURI = getNodeValue(child); 332 } else { 334 _sourceURI = getAttributeValue(child, ATTR_URI_SOURCEURI); 335 336 if (hasAttribute(child, ATTR_URI_TYPE)) { 337 _type = getAttributeValue(child, ATTR_URI_TYPE); 338 } 339 if (hasAttribute(child, ATTR_URI_SOURCEPREFIX)) { 340 _root = getAttributeValue(child, ATTR_URI_SOURCEPREFIX); 341 } 342 if (hasAttribute(child, ATTR_URI_DESTURI)) { 343 _destURI = getAttributeValue(child, ATTR_URI_DESTURI); 344 } 345 } 346 cocoon.addTarget(_type, _root, _sourceURI, _destURI, followLinks, confirmExtensions, logger); 347 } else { 348 throw new IllegalArgumentException ("Unknown element: <" + childName + ">"); 349 } 350 } 351 } 352 } 353 354 private static void parseURINode(CocoonBean cocoon, Node node, String destDir) throws IllegalArgumentException { 355 NodeList nodes = node.getChildNodes(); 356 357 if (node.getAttributes().getLength() == 0 && nodes.getLength() != 0) { 358 cocoon.addTarget(getNodeValue(node), destDir); 359 } else if (node.getAttributes().getLength() !=0 && nodes.getLength() ==0){ 360 361 String src = getAttributeValue(node, ATTR_URI_SOURCEURI); 362 363 String type = null; 364 if (hasAttribute(node, ATTR_URI_TYPE)) { 365 type = getAttributeValue(node, ATTR_URI_TYPE); 366 } 367 String root = null; 368 if (hasAttribute(node, ATTR_URI_SOURCEPREFIX)) { 369 root = getAttributeValue(node, ATTR_URI_SOURCEPREFIX); 370 } 371 String dest = null; 372 if (hasAttribute(node, ATTR_URI_DESTURI)) { 373 dest = getAttributeValue(node, ATTR_URI_DESTURI); 374 } 375 376 if (root != null && type != null && dest != null) { 377 cocoon.addTarget(type, root, src, dest); 378 } else if (root != null && dest != null) { 379 cocoon.addTarget(root, src, dest); 380 } else if (dest != null) { 381 cocoon.addTarget(src, dest); 382 } else { 383 cocoon.addTarget(src, destDir); 384 } 385 } else if (node.getAttributes().getLength() !=0 && nodes.getLength() != 0) { 386 throw new IllegalArgumentException ("Unexpected children of <" + NODE_URI + "> node"); 387 } else { 388 throw new IllegalArgumentException ("Not enough information for <"+ NODE_URI + "> node"); 389 } 390 } 391 392 private static String getNodeValue(Node root, String name) throws IllegalArgumentException { 393 NodeList nodes = root.getChildNodes(); 394 for (int i = 0; i < nodes.getLength(); i++) { 395 Node node = nodes.item(i); 396 if (node.getNodeType()== Node.ELEMENT_NODE) { 397 String nodeName = node.getNodeName(); 398 if (nodeName.equals(name)) { 399 return getNodeValue(node); 400 } 401 } 402 } 403 return null; 404 } 405 406 private static String getNodeValue(Node node) throws IllegalArgumentException { 407 StringBuffer s = new StringBuffer (); 408 NodeList children = node.getChildNodes(); 409 boolean found = false; 410 411 for (int i = 0; i < children.getLength(); i++) { 412 Node child = children.item(i); 413 if (child.getNodeType() == Node.TEXT_NODE) { 414 s.append(child.getNodeValue()); 415 found = true; 416 } else { 417 throw new IllegalArgumentException ("Unexpected node:" + child.getLocalName()); 418 } 419 } 420 if (!found) { 421 throw new IllegalArgumentException ("Expected value for " + node.getLocalName() + " node"); 422 } 423 return s.toString(); 424 } 425 426 private static String getAttributeValue(Node node, String attr) throws IllegalArgumentException { 427 NamedNodeMap nodes = node.getAttributes(); 428 if (nodes != null) { 429 Node attribute = nodes.getNamedItem(attr); 430 if (attribute != null && attribute.getNodeValue() != null) { 431 return attribute.getNodeValue(); 432 } 433 } 434 throw new IllegalArgumentException ("Missing " + attr + " attribute on <" + node.getNodeName() + "> node"); 435 } 436 437 private static boolean hasAttribute(Node node, String attr) { 438 NamedNodeMap nodes = node.getAttributes(); 439 if (nodes != null) { 440 Node attribute = nodes.getNamedItem(attr); 441 return (attribute != null); 442 } 443 return false; 444 } 445 446 private static boolean getBooleanAttributeValue(Node node, String attr) { 447 NamedNodeMap nodes = node.getAttributes(); 448 if (nodes != null) { 449 Node attribute = nodes.getNamedItem(attr); 450 451 if (attribute != null) { 452 String value = attribute.getNodeValue(); 453 return BooleanUtils.toBoolean(value); 454 } 455 } 456 return false; 457 } 458 459 465 public static List processURIFile(String filename) { 466 List uris = new ArrayList (); 467 try { 468 BufferedReader uriFile = new BufferedReader (new FileReader (filename)); 469 470 while (true) { 471 String uri = uriFile.readLine(); 472 473 if (null == uri) { 474 break; 475 } 476 477 uri = uri.trim(); 478 if (!uri.equals("") && !uri.startsWith("#")){ 479 uris.add(uri.trim()); 480 } 481 } 482 483 uriFile.close(); 484 } catch (Exception e) { 485 } 487 return uris; 488 } 489 490 } 491 | Popular Tags |