KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > sourcetype > SourceTypeAction


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation or its licensors,
3  * as applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.forrest.sourcetype;
18
19 import org.cyberneko.pull.XMLPullParser;
20 import org.cyberneko.pull.XMLEvent;
21 import org.cyberneko.pull.event.*;
22 import org.cyberneko.pull.parsers.Xerces2;
23 import org.apache.xerces.xni.parser.XMLInputSource;
24 import org.apache.avalon.framework.configuration.*;
25 import org.apache.avalon.framework.thread.ThreadSafe;
26 import org.apache.avalon.framework.parameters.Parameters;
27 import org.apache.avalon.framework.logger.AbstractLogEnabled;
28 import org.apache.cocoon.acting.Action;
29 import org.apache.cocoon.environment.SourceResolver;
30 import org.apache.cocoon.environment.Redirector;
31 import org.apache.excalibur.source.Source;
32 import org.apache.excalibur.source.SourceNotFoundException;
33
34 import java.io.InputStream JavaDoc;
35 import java.util.*;
36
37 /**
38  * An action that assigns a "sourcetype" to a source. See the external documentation for
39  * more information.
40  */

41 public class SourceTypeAction extends AbstractLogEnabled implements Configurable, ThreadSafe, Action
42 {
43     protected List sourceTypes = new ArrayList();
44     protected static final String JavaDoc XSI_NAMESPACE = "http://www.w3.org/2001/XMLSchema-instance";
45
46     public void configure(Configuration configuration) throws ConfigurationException
47     {
48         Configuration[] sourceTypeConfs = configuration.getChildren("sourcetype");
49         for (int i = 0; i < sourceTypeConfs.length; i++)
50         {
51             SourceType sourceType = new SourceType();
52             sourceType.configure(sourceTypeConfs[i]);
53             sourceTypes.add(sourceType);
54         }
55     }
56
57     public Map act(Redirector redirector, SourceResolver sourceResolver, Map objectModel, String JavaDoc src, Parameters parameters)
58             throws Exception JavaDoc
59     {
60         if (src == null || src.equals(""))
61             throw new Exception JavaDoc("SourceTypeAction: src attribute should be defined and non-empty.");
62         Source source = sourceResolver.resolveURI(src);
63         XMLPullParser parser = new Xerces2();
64         try {
65           InputStream JavaDoc is = source.getInputStream();
66           parser.setInputSource(new XMLInputSource(null, src, null, is, null));
67         } catch (SourceNotFoundException e) {
68           getLogger().warn("Source '"+source+"' not found");
69           return null;
70         }
71
72         // load nothing external
73
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
74         parser.setFeature("http://xml.org/sax/features/external-general-entities", false);
75         parser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
76
77         // note: namespace-aware parsing is by default true
78

79         SourceInfo sourceInfo = new SourceInfo();
80         // pull-parse the document until we reach the document element and put the collected information
81
// into the sourceInfo object
82
try
83         {
84             XMLEvent event;
85             while ((event = parser.nextEvent()) != null)
86             {
87                 if (event.type == XMLEvent.DOCTYPE_DECL)
88                 {
89                     DoctypeDeclEvent doctypeDeclEvent = (DoctypeDeclEvent)event;
90                     sourceInfo.setPublicId(doctypeDeclEvent.pubid);
91                 }
92                 else if (event.type == XMLEvent.PROCESSING_INSTRUCTION)
93                 {
94                     ProcessingInstructionEvent piEvent = (ProcessingInstructionEvent)event;
95                     sourceInfo.addProcessingInstruction(piEvent.target, piEvent.data != null ? piEvent.data.toString() : null);
96                 }
97                 else if (event.type == XMLEvent.ELEMENT)
98                 {
99                     ElementEvent elementEvent = (ElementEvent)event;
100                     sourceInfo.setDocumentElementLocalName(elementEvent.element.localpart);
101                     sourceInfo.setDocumentElementNamespace(elementEvent.element.uri);
102
103                     sourceInfo.setXsiSchemaLocation(elementEvent.attributes.getValue(XSI_NAMESPACE, "schemaLocation"));
104                     sourceInfo.setXsiNoNamespaceSchemaLocation(elementEvent.attributes.getValue(XSI_NAMESPACE, "noNamespaceSchemaLocation"));
105
106                     // stop parsing after the root element
107
break;
108                 }
109             }
110         }
111         finally
112         {
113             // this will also close the inputstream
114
parser.cleanup();
115         }
116
117         // Run over the SourceTypes until one is found that matches the information collected in sourceInfo
118
Iterator sourceTypeIt = sourceTypes.iterator();
119         while (sourceTypeIt.hasNext())
120         {
121             SourceType sourceType = (SourceType)sourceTypeIt.next();
122             if (sourceType.matches(sourceInfo))
123             {
124                 HashMap returnMap = new HashMap();
125                 returnMap.put("sourcetype", sourceType.getName());
126                 if (getLogger().isDebugEnabled())
127                     getLogger().debug("SourceTypeAction: found sourcetype " + sourceType.getName() + " for source " + src);
128                 return returnMap;
129             }
130         }
131         if (getLogger().isDebugEnabled())
132             getLogger().debug("SourceTypeAction: found no sourcetype for source " + src);
133         return null;
134     }
135 }
136
137
Popular Tags