package com.justep.ops.processor; import java.io.File; import java.io.IOException; import org.orbeon.oxf.common.OXFException; import org.orbeon.oxf.pipeline.api.ExternalContext; import org.orbeon.oxf.pipeline.api.PipelineContext; import org.orbeon.oxf.processor.ProcessorInputOutputInfo; import org.orbeon.oxf.processor.SimpleProcessor; import org.orbeon.oxf.resources.ResourceManager; import org.orbeon.oxf.resources.ResourceManagerWrapper; import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; public class GeneratorPageFlowProcessor extends SimpleProcessor { public GeneratorPageFlowProcessor(){ addOutputInfo(new ProcessorInputOutputInfo(OUTPUT_DATA)); } public void generateData(PipelineContext context, ContentHandler contentHandler) throws SAXException, IOException { final ExternalContext.Request request = getRequest(context); String path = getPageFlowPath(request.getRequestPath()); ResourceManager rm = ResourceManagerWrapper.instance(); rm.getContentAsSAX(path, contentHandler); } private String getPageFlowPath(String requestPath){ if (!requestPath.endsWith("/")) requestPath += "/"; while (requestPath != null){ String path = "/apps" + requestPath + "page-flow.xml"; if (isExists(path)){ return path; }else{ requestPath = getNextRequestPath(requestPath); } } throw new RuntimeException(requestPath + " don't match page-flow.xml!"); } private boolean isExists(String path){ String realPath = ResourceManagerWrapper.instance().getRealPath(path); File file = new File(realPath); return file.exists(); } private String getNextRequestPath(String requestPath){ requestPath = requestPath.substring(0, requestPath.length()-1); int index = requestPath.lastIndexOf("/"); if ((index != -1) && (index != 0)){ return requestPath.substring(0, index + 1); }else{ return null; } } private ExternalContext.Request getRequest(PipelineContext context) { ExternalContext externalContext = (ExternalContext) context.getAttribute(PipelineContext.EXTERNAL_CONTEXT); if (externalContext == null) throw new OXFException("Missing external context"); return externalContext.getRequest(); } }