1 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 ; 35 import java.util.*; 36 37 41 public class SourceTypeAction extends AbstractLogEnabled implements Configurable, ThreadSafe, Action 42 { 43 protected List sourceTypes = new ArrayList(); 44 protected static final String 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 src, Parameters parameters) 58 throws Exception 59 { 60 if (src == null || src.equals("")) 61 throw new Exception ("SourceTypeAction: src attribute should be defined and non-empty."); 62 Source source = sourceResolver.resolveURI(src); 63 XMLPullParser parser = new Xerces2(); 64 try { 65 InputStream 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 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 79 SourceInfo sourceInfo = new SourceInfo(); 80 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 break; 108 } 109 } 110 } 111 finally 112 { 113 parser.cleanup(); 115 } 116 117 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 |