01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: **/package org.araneaframework.http.extension;
16:
17: import org.araneaframework.core.AraneaRuntimeException;
18: import org.araneaframework.http.extension.ExternalResource.FileGroup;
19: import org.xml.sax.Attributes;
20: import org.xml.sax.helpers.DefaultHandler;
21:
22: /**
23: * SAX handler for parsing external resources' configuration files.
24: * See 'etc/aranea-resources.xml' for a sample configuration file.
25: *
26: * @author "Toomas Römer" <toomas@webmedia.ee>
27: */
28: public class ExternalResourceConfigurationHandler extends
29: DefaultHandler {
30: private static final String TAG_FILES = "files";
31: private static final String TAG_FILE = "file";
32:
33: private static final String ATTRIB_CONTENT_TYPE = "content-type";
34: private static final String ATTRIB_GROUP = "group";
35: private static final String ATTRIB_PATH = "path";
36:
37: private ExternalResource result = new ExternalResource();
38: private FileGroup fileGroup;
39:
40: public ExternalResourceConfigurationHandler() {
41: this (new ExternalResource());
42: }
43:
44: public ExternalResourceConfigurationHandler(
45: ExternalResource resource) {
46: super ();
47: result = resource;
48: }
49:
50: public void startElement(String uri, String localName,
51: String qName, Attributes attributes) {
52: if (TAG_FILES.equalsIgnoreCase(qName)) {
53: fileGroup = new FileGroup();
54: boolean contentTypeSet = false;
55:
56: for (int i = 0; i < attributes.getLength(); i++) {
57: if (ATTRIB_CONTENT_TYPE.equalsIgnoreCase(attributes
58: .getQName(i))) {
59: fileGroup.setContentType(attributes.getValue(i));
60: contentTypeSet = true;
61: } else if (ATTRIB_GROUP.equalsIgnoreCase(attributes
62: .getQName(i))) {
63: fileGroup.setName(attributes.getValue(i));
64: }
65: }
66: if (!contentTypeSet)
67: throw new AraneaRuntimeException(
68: "No content type set for files in resource configuration");
69: } else if (TAG_FILE.equalsIgnoreCase(qName)) {
70: int i = attributes.getIndex(ATTRIB_PATH);
71:
72: if (i != -1) {
73: fileGroup.addFile(attributes.getValue(i));
74: }
75: }
76: }
77:
78: public void endElement(String uri, String localName, String qName) {
79: if (TAG_FILES.equalsIgnoreCase(qName)) {
80: result.addGroup(fileGroup);
81: }
82: }
83:
84: public ExternalResource getResource() {
85: return result;
86: }
87: }
|