Search in sources :

Example 1 with ISheetContainer

use of com.twinsoft.convertigo.beans.core.ISheetContainer in project convertigo by convertigo.

the class GenericRequester method findStyleSheet.

protected void findStyleSheet(String browser) throws EngineException {
    // The sheet url may have been already set by error handling...
    if (context.absoluteSheetUrl != null)
        return;
    // TODO: chercher la feuille de style en fonction du locale du client
    RequestableObject requestedObject = context.requestedObject;
    ISheetContainer lastDetectedObject = context.lastDetectedObject;
    InputSource inputSource = null;
    try {
        if ((context.cacheEntry != null) && (context.cacheEntry.sheetUrl != null)) {
            context.sheetUrl = context.cacheEntry.sheetUrl;
            context.absoluteSheetUrl = context.cacheEntry.absoluteSheetUrl;
            context.contentType = context.cacheEntry.contentType;
            if (context.sheetUrl == null) {
                // without sheet context...
                return;
            }
            Engine.logContext.debug("Sheet built from the cache");
        } else {
            Sheet sheet = null;
            int sheetLocation = requestedObject.getSheetLocation();
            if (sheetLocation == Transaction.SHEET_LOCATION_FROM_LAST_DETECTED_OBJECT_OF_REQUESTABLE) {
                Engine.logContext.debug("Sheet location: from last detected screen class");
            } else if (sheetLocation == Transaction.SHEET_LOCATION_FROM_REQUESTABLE) {
                Engine.logContext.debug("Sheet location: from transaction");
            } else {
                Engine.logContext.debug("Sheet location: none");
                return;
            }
            Engine.logContext.debug("Searching specific sheet for browser '" + browser + "'...");
            switch(sheetLocation) {
                default:
                case Transaction.SHEET_LOCATION_NONE:
                    break;
                case Transaction.SHEET_LOCATION_FROM_REQUESTABLE:
                    Engine.logContext.debug("Searching in the transaction");
                    sheet = requestedObject.getSheet(browser);
                    break;
                case Transaction.SHEET_LOCATION_FROM_LAST_DETECTED_OBJECT_OF_REQUESTABLE:
                    if (lastDetectedObject != null) {
                        Engine.logContext.debug("Searching in the last detected screen class (" + ((DatabaseObject) lastDetectedObject).getQName() + ")");
                        sheet = lastDetectedObject.getSheet(browser);
                    }
                    break;
            }
            if (sheet == null) {
                Engine.logContext.debug("No specific sheet has been found; searching for common sheet...");
                switch(sheetLocation) {
                    default:
                    case Transaction.SHEET_LOCATION_FROM_REQUESTABLE:
                        Engine.logContext.debug("Searching in the transaction");
                        sheet = requestedObject.getSheet(Sheet.BROWSER_ALL);
                        break;
                    case Transaction.SHEET_LOCATION_FROM_LAST_DETECTED_OBJECT_OF_REQUESTABLE:
                        if (lastDetectedObject != null) {
                            Engine.logContext.debug("Searching in the last detected screen class");
                            sheet = lastDetectedObject.getSheet(Sheet.BROWSER_ALL);
                        }
                        break;
                }
            }
            if (sheet == null) {
                // without sheet context...
                return;
            } else {
                Engine.logContext.debug("Storing found sheet into the execution context (__currentSheet)");
                context.sheetUrl = sheet.getUrl();
            }
            Engine.logContext.debug("Using XSL data from \"" + context.sheetUrl + "\"");
            // Search relatively to the Convertigo servlet application base directory
            context.absoluteSheetUrl = context.getProjectDirectory() + "/" + (context.subPath.length() > 0 ? context.subPath + "/" : "") + context.sheetUrl;
            Engine.logContext.debug("Url: " + context.absoluteSheetUrl);
            File xslFile = new File(context.absoluteSheetUrl);
            if (!xslFile.exists()) {
                Engine.logContext.debug("The local xsl file (\"" + context.absoluteSheetUrl + "\") does not exist. Trying search in Convertigo XSL directory...");
                if (context.sheetUrl.startsWith("../../xsl/"))
                    context.absoluteSheetUrl = Engine.XSL_PATH + "/" + context.sheetUrl.substring(10);
                else
                    context.absoluteSheetUrl = Engine.XSL_PATH + "/" + context.sheetUrl;
                Engine.logContext.debug("Url: " + context.absoluteSheetUrl);
                xslFile = new File(context.absoluteSheetUrl);
                if (!xslFile.exists()) {
                    Engine.logContext.debug("The common xsl file (\"" + context.absoluteSheetUrl + "\") does not exist. Trying absolute search...");
                    context.absoluteSheetUrl = context.sheetUrl;
                    Engine.logContext.debug("Url: " + context.absoluteSheetUrl);
                }
            }
            Engine.logContext.debug("Retrieving content type from the XSL...");
            // inputSource = new InputSource(new FileInputStream(context.absoluteSheetUrl));
            inputSource = new InputSource(new File(context.absoluteSheetUrl).toURI().toASCIIString());
            DocumentBuilder documentBuilder = XMLUtils.getDefaultDocumentBuilder();
            documentBuilder.setEntityResolver(XMLUtils.getEntityResolver());
            Document document = documentBuilder.parse(inputSource);
            NodeList nodeList = document.getElementsByTagName("xsl:output");
            String contentType = MimeType.Html.value();
            if (nodeList.getLength() != 0) {
                Element element = (Element) nodeList.item(0);
                contentType = element.getAttribute("media-type");
                if (contentType.length() == 0) {
                    Engine.logContext.warn("No media type is specified into the XSL style sheet \"" + context.sheetUrl + "\"; using default (\"text/html\"). You should use <xsl:output media-type=\"...\" ...> directive.");
                    contentType = MimeType.Html.value();
                }
                Engine.logContext.debug("Content-type=" + contentType);
            } else {
                Engine.logContext.warn("No media type is specified into the XSL style sheet \"" + context.sheetUrl + "\"; using default (\"text/html\"). You should use <xsl:output media-type=\"...\" ...> directive.");
            }
            context.contentType = contentType;
        }
        // Updating the cache entry properties if needed
        if (context.cacheEntry != null) {
            context.cacheEntry.contentType = context.contentType;
            context.cacheEntry.sheetUrl = context.sheetUrl;
            context.cacheEntry.absoluteSheetUrl = context.absoluteSheetUrl;
            try {
                Engine.theApp.cacheManager.updateCacheEntry(context.cacheEntry);
                Engine.logContext.debug("Sheet stored into the cache entry; updating its XSL and content type properties...");
            } catch (Exception e) {
                Engine.logContext.error("(CacheManager) Unable to update the cache entry!", e);
            }
        }
    } catch (Exception e) {
        throw new EngineException("An unexpected error has occured while finding the sheet for the transaction \"" + requestedObject.getName() + "\".", e);
    } finally {
        try {
            if (inputSource != null) {
                InputStream inputStream = inputSource.getByteStream();
                if (inputStream != null)
                    inputStream.close();
            }
        } catch (IOException e) {
            throw new EngineException("Unable to close the sheet file for the transaction \"" + requestedObject.getName() + "\".", e);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) RequestableObject(com.twinsoft.convertigo.beans.core.RequestableObject) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) EngineException(com.twinsoft.convertigo.engine.EngineException) IOException(java.io.IOException) Document(org.w3c.dom.Document) EngineException(com.twinsoft.convertigo.engine.EngineException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ISheetContainer(com.twinsoft.convertigo.beans.core.ISheetContainer) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Sheet(com.twinsoft.convertigo.beans.core.Sheet) File(java.io.File)

Aggregations

ISheetContainer (com.twinsoft.convertigo.beans.core.ISheetContainer)1 RequestableObject (com.twinsoft.convertigo.beans.core.RequestableObject)1 Sheet (com.twinsoft.convertigo.beans.core.Sheet)1 EngineException (com.twinsoft.convertigo.engine.EngineException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1 InputSource (org.xml.sax.InputSource)1