⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a_cmsxmlcontent.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    private Object handleMethodTagForSure(Element n, Object callingObject, Object userObj) throws CmsException {
        processNode(n, m_mainProcessTags, null, callingObject, userObj);
        String tagcontent = getTagValue(n);
        String method = n.getAttribute("name");
        Object result = null;
        try {
            result = callUserMethod(method, tagcontent, callingObject, userObj, true);
        }
        catch(Throwable e1) {
            if(e1 instanceof CmsException) {
                throw (CmsException)e1;
            }
            else {
                throwException("handleMethodTagForSure() received an exception from callUserMethod() while calling \"" + method + "\" requested by class " + callingObject.getClass().getName() + ": " + e1);
            }
        }
        return result;
    }

    /**
     * Handling of the "PROCESS" tags.
     * Looks up the requested datablocks in the internal hashtable and
     * returns its subnodes.
     *
     * @param n XML element containing the <code>&lt;PROCESS&gt;</code> tag.
     * @param callingObject Reference to the object requesting the node processing.
     * @param userObj Customizable user object that will be passed through to handling and user methods.
     */
    private Object handleProcessTag(Element n, Object callingObject, Object userObj) {
        String blockname = getTagValue(n).toLowerCase();
        Element datablock = null;
        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() && C_DEBUG) {
            A_OpenCms.log(C_OPENCMS_DEBUG, getClassName() + "handleProcessTag() started. Request for datablock \"" + blockname + "\".");
        }
        datablock = (Element)((Element)m_blocks.get(blockname));
        if(datablock == null) {
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
                A_OpenCms.log(C_OPENCMS_CRITICAL, getClassName() + "Requested datablock  \"" + blockname + "\" not found!");
            }
            return C_ERR_NODATABLOCK + blockname;
        }
        else {
            return datablock.getChildNodes();
        }
    }

    /**
     * Checks if this Template owns a datablock with the given key.
     * @param key Datablock key to be checked.
     * @return true if a datablock is found, false otherwise.
     */
    protected boolean hasData(String key) {
        return m_blocks.containsKey(key.toLowerCase());
    }

    /**
     * Initialize the XML content class.
     * Load and parse the content of the given CmsFile object.
     * @param cms CmsObject Object for accessing resources.
     * @param file CmsFile object of the file to be loaded and parsed.
     * @exception CmsException
     */
    public void init(CmsObject cms, CmsFile file) throws CmsException {
        String filename = file.getAbsolutePath();
        String currentProject = cms.getRequestContext().currentProject().getName();
        Document parsedContent = null;
        m_cms = cms;
        m_filename = filename;
        parsedContent = loadCachedDocument(filename);
        if(parsedContent == null) {
            String fileContent = new String(file.getContents());
            if(fileContent == null || "".equals(fileContent.trim())) {
                // The file content is empty. Possibly the file object is only
                // a file header. Re-read the file object and try again
                file = cms.readFile(filename);
                fileContent = new String(file.getContents()).trim();
            }
            if(fileContent == null || "".equals(fileContent.trim())) {

                // The file content is still emtpy.
                // Start with an empty XML document.
                try {
                    parsedContent = getXmlParser().createEmptyDocument(getXmlDocumentTagName());
                }
                catch(Exception e) {
                    throwException("Could not initialize now XML document " + filename + ". " + e, CmsException.C_XML_PARSING_ERROR);
                }
            } else {
                // TODO: really re-read file content???
                // parsedContent = parse(new String(file.getContents()));
                parsedContent = parse(fileContent);
            }
            m_filecache.put(currentProject + ":" + filename, parsedContent.cloneNode(true));
        }
        init(cms, parsedContent, filename);
    }

    /**
     * Initialize the XML content class.
     * Load and parse the content of the given CmsFile object.
     * <P>
     * If a previously cached parsed content exists, it will be re-used.
     * <P>
     * If no absolute file name ist given,
     * template files will be searched a hierachical order using
     * <code>lookupAbsoluteFilename</code>.
     *
     * @param cms CmsObject Object for accessing resources.
     * @param file CmsFile object of the file to be loaded and parsed.
     * @exception CmsException
     * @see #lookupAbsoluteFilename
     */
    public void init(CmsObject cms, String filename) throws CmsException {

        if(!filename.startsWith("/")) {
            throw new CmsException("A relative path has entered the A_CmsXmlContent class. filename="+filename+"");
        }
        String currentProject = cms.getRequestContext().currentProject().getName();
        Document parsedContent = null;
        m_cms = cms;
        m_filename = filename;
        parsedContent = loadCachedDocument(filename);
        if(parsedContent == null) {
            CmsFile file = cms.readFile(filename);
            parsedContent = parse(new String(file.getContents()));
            m_filecache.put(currentProject + ":" + filename, parsedContent.cloneNode(true));
        }
        else {

            // File was found in cache.

            // We have to read the file header to check access rights.
            cms.readFileHeader(filename);
        }
        init(cms, parsedContent, filename);
    }

    /**
     * Initialize the class with the given parsed XML DOM document.
     * @param cms CmsObject Object for accessing system resources.
     * @param document DOM document object containing the parsed XML file.
     * @param filename OpenCms filename of the XML file.
     * @exception CmsException
     */
    public void init(CmsObject cms, Document content, String filename) throws CmsException {
        m_cms = cms;
        m_content = content;
        m_filename = filename;

        // First check the document tag. Is this the right document type?
        Element docRootElement = m_content.getDocumentElement();
        String docRootElementName = docRootElement.getNodeName().toLowerCase();
        if(!docRootElementName.equals(getXmlDocumentTagName().toLowerCase())) {

            // Hey! This is a wrong XML document!

            // We will throw an execption and the document away :-)
            removeFromFileCache();
            m_content = null;
            String errorMessage = "XML document " + getAbsoluteFilename() + " is not of the expected type. This document is \"" + docRootElementName + "\", but it should be \"" + getXmlDocumentTagName() + "\" (" + getContentDescription() + ").";
            throwException(errorMessage, CmsException.C_XML_WRONG_CONTENT_TYPE);
        }

        // OK. Document tag is fine. Now get the DATA tags and collect them

        // in a Hashtable (still in DOM representation!)
        try {
            processNode(m_content, m_firstRunTags, A_CmsXmlContent.class.getDeclaredMethod("handleDataTag", C_PARAMTYPES_HANDLING_METHODS), null, null);
        }
        catch(CmsException e) {
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
                A_OpenCms.log(C_OPENCMS_INFO, "Error while scanning for DATA and INCLUDE tags in file " + getAbsoluteFilename() + ".");
            }
            throw e;
        }
        catch(NoSuchMethodException e2) {
            String errorMessage = "XML tag process method \"handleDataTag\" could not be found";
            throwException(errorMessage, CmsException.C_XML_NO_PROCESS_METHOD);
        }
    }

    /**
     * Internal method for creating a new datablock.
     * <P>
     * This method is called by setData() if a new, not existing
     * datablock must be created.
     * <P>
     * <B>Functionality:</B> If a non-hierarchical datablock is given,
     * it is inserted at the end of the DOM document.
     * If a hierarchical datablock is given, all possible parent
     * names are checked in a backward oriented order. If a
     * datablock with a name that equals a part of the hierarchy is
     * found, the new datablock will be created as a (sub)child
     * of this datablock.
     *
     * @param tag Key for this datablock.
     * @param data DOM element node for this datablock.
     */
    private void insertNewDatablock(String tag, Element data) {

        // First check, if this is an extended datablock
        // in <NAME1 name="name2>... format, that has to be inserted
        // as name1.name2
        String nameAttr = data.getAttribute("name");
        String workTag = null;
        if((!data.getNodeName().toLowerCase().equals("data")) && nameAttr != null && (!"".equals(nameAttr))) {
            // this is an extended datablock
            workTag = tag.substring(0, tag.lastIndexOf("."));
        }else {
            workTag = tag;
        }
        // Import the node for later inserting
        Element importedNode = (Element)parser.importNode(m_content, data);

        // Check, if this is a simple datablock without hierarchy.
        if(workTag.indexOf(".") == -1) {
            // Fine. We can insert the new Datablock at the of the document
            m_content.getDocumentElement().appendChild(importedNode);
            m_blocks.put(tag, importedNode);
        }else {
            // This is a hierachical datablock tag. We have to search for
            // an appropriate place to insert first.
            boolean found = false;
            String match = "." + workTag;
            int dotIndex = match.lastIndexOf(".");
            Vector newBlocks = new Vector();
            while((!found) && (dotIndex > 1)) {
                match = match.substring(0, dotIndex);
                if(hasData(match.substring(1))) {
                    found = true;
                }else {
                    dotIndex = match.lastIndexOf(".");
                    newBlocks.addElement(match.substring(dotIndex + 1));
                }
            }
            // newBlocks now contains a (backward oriented) list
            // of all datablocks that have to be created, before
            // the new datablock named "tag" can be inserted.
            String datablockPrefix = "";
            if(found) {
                datablockPrefix = match.substring(1) + ".";
            }
            // number of new elements to be created
            int numNewBlocks = newBlocks.size();
            // used to create the required new elements
            Element newElem = null;
            // Contains the last existing Element in the hierarchy.
            Element lastElem = null;
            // now create the new elements backwards
            for(int i = numNewBlocks - 1;i >= 0;i--) {
                newElem = m_content.createElement("DATA");
                newElem.setAttribute("name", (String)newBlocks.elementAt(i));
                m_blocks.put(datablockPrefix + (String)newBlocks.elementAt(i), newElem);
                if(lastElem != null) {
                    lastElem.appendChild(newElem);
                }else {
                    lastElem = newElem;
                }
            }
            // Now all required parent datablocks are created.
            // Finally the given datablock can be inserted.
            if(lastElem != null) {
                lastElem.appendChild(importedNode);
            }else {
                lastElem = importedNode;
            }
            m_blocks.put(datablockPrefix + tag, importedNode);

            // lastElem now contains the hierarchical tree of all DATA tags to be
            // inserted.
            // If we have found an existing part of the hierarchy, get
            // this part and append the tree. If no part was found, append the
            // tree at the end of the document.
            if(found) {
                Element parent = (Element)m_blocks.get(match.substring(1));
                parent.appendChild(lastElem);
            }else {
                m_content.getDocumentElement().appendChild(lastElem);
            }
        }
    }

    /**
     * Reloads a previously cached parsed content.
     *
     * @param filename Absolute pathname of the file to look for.
     * @return DOM parsed document or null if the cached content was not found.
     */
    private Document loadCachedDocument(String filename) {
        Document cachedDoc = null;
        String currentProject = m_cms.getRequestContext().currentProject().getName();
        Document lookup = (Document)m_filecache.get(currentProject + ":" + filename);
        if(lookup != null) {
            try {

                //cachedDoc = lookup.cloneNode(true).getOwnerDocument();
                cachedDoc = (Document)lookup.cloneNode(true);
            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -