001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.editors.codemirror; 029 030import org.opencms.file.CmsResource; 031import org.opencms.file.types.CmsResourceTypeJsp; 032import org.opencms.file.types.CmsResourceTypeXmlContent; 033import org.opencms.file.types.CmsResourceTypeXmlPage; 034import org.opencms.jsp.CmsJspActionElement; 035import org.opencms.main.CmsException; 036import org.opencms.main.OpenCms; 037import org.opencms.util.CmsStringUtil; 038import org.opencms.workplace.editors.CmsSimpleEditor; 039 040import java.util.Arrays; 041import java.util.List; 042import java.util.Locale; 043 044/** 045 * Provides helper methods for the usage of the CodeMirror editor that can be used 046 * for syntax highlighting of text based files.<p> 047 */ 048public class CmsCodeMirror extends CmsSimpleEditor { 049 050 /** Syntax highlight type name: CSS. */ 051 public static final String HIGHLIGHT_TYPE_CSS = "css"; 052 053 /** Syntax highlight type name: HTML. */ 054 public static final String HIGHLIGHT_TYPE_HTML = "html"; 055 056 /** Syntax highlight type name: JavaScript. */ 057 public static final String HIGHLIGHT_TYPE_JAVASCRIPT = "js"; 058 059 /** Syntax highlight type name: JSP. */ 060 public static final String HIGHLIGHT_TYPE_JSP = "jsp"; 061 062 /** Syntax highlight type name: XML. */ 063 public static final String HIGHLIGHT_TYPE_XML = "xml"; 064 065 /** Request parameter name for the close function parameter. */ 066 public static final String PARAM_CLOSEFUNCTION = "closefunction"; 067 068 /** Possible type suffix names. */ 069 protected static final String[] HIGHLIGHT_TYPES = { 070 HIGHLIGHT_TYPE_CSS, 071 HIGHLIGHT_TYPE_HTML, 072 HIGHLIGHT_TYPE_JAVASCRIPT, 073 HIGHLIGHT_TYPE_JSP, 074 HIGHLIGHT_TYPE_XML}; 075 076 /** Possible type suffix names as list. */ 077 protected static final List<String> HIGHLIGHT_TYPES_LIST = Arrays.asList(HIGHLIGHT_TYPES); 078 079 /** Sub path fragment to the editor resources. */ 080 protected static final String SUBPATH_CODEMIRROR = "editors/codemirror/"; 081 082 /** Path to the editor resources. */ 083 protected static final String VFS_PATH_EDITOR = CmsStringUtil.joinPaths( 084 OpenCms.getSystemInfo().getStaticResourceContext(), 085 SUBPATH_CODEMIRROR); 086 087 /** Path to the editor distribution resources. */ 088 protected static final String VFS_PATH_EDITOR_DIST = VFS_PATH_EDITOR + "dist/"; 089 090 /** The close function parameter. */ 091 private String m_paramCloseFunction; 092 093 /** 094 * Public constructor.<p> 095 * 096 * @param jsp an initialized JSP action element 097 */ 098 public CmsCodeMirror(CmsJspActionElement jsp) { 099 100 super(jsp); 101 } 102 103 /** 104 * Returns the editor language to use according to the current users workplace settings.<p> 105 * 106 * @return the editor language to use 107 */ 108 public String getEditorLanguage() { 109 110 String testLocale = getLocale().toString(); 111 if (getCms().existsResource(VFS_PATH_EDITOR + "js/lang-" + testLocale + ".js")) { 112 return testLocale; 113 } 114 return Locale.ENGLISH.toString(); 115 } 116 117 /** 118 * @see org.opencms.workplace.editors.CmsSimpleEditor#getEditorResourceUri() 119 */ 120 @Override 121 public String getEditorResourceUri() { 122 123 return CmsStringUtil.joinPaths(OpenCms.getSystemInfo().getStaticResourceContext(), SUBPATH_CODEMIRROR); 124 } 125 126 /** 127 * Returns the syntax highlighting type for the currently edited resource.<p> 128 * 129 * @return the syntax highlighting type 130 */ 131 public String getHighlightMode() { 132 133 // read edited resource 134 CmsResource resource = null; 135 try { 136 resource = getCms().readResource(getParamResource()); 137 } catch (CmsException e) { 138 // ignore 139 } 140 141 if (resource != null) { 142 // determine resource type 143 int type = resource.getTypeId(); 144 if (CmsResourceTypeJsp.isJspTypeId(type)) { 145 // JSP file 146 return HIGHLIGHT_TYPE_JSP; 147 } 148 if (CmsResourceTypeXmlContent.isXmlContent(resource) || CmsResourceTypeXmlPage.isXmlPage(resource)) { 149 // XML content file or XML page file 150 return HIGHLIGHT_TYPE_XML; 151 } 152 // all other files will be matched according to their suffix 153 int dotIndex = getParamResource().lastIndexOf('.'); 154 if (dotIndex != -1) { 155 String suffix = getParamResource().substring(dotIndex + 1); 156 if (CmsStringUtil.isNotEmpty(suffix)) { 157 // there is a suffix, determine matching syntax highlighting 158 int typeIndex = HIGHLIGHT_TYPES_LIST.indexOf(suffix.toLowerCase()); 159 if (typeIndex != -1) { 160 return HIGHLIGHT_TYPES_LIST.get(typeIndex); 161 } 162 } 163 } 164 } 165 // return HTML type as default 166 return HIGHLIGHT_TYPE_HTML; 167 } 168 169 /** 170 * Returns the close function parameter.<p> 171 * 172 * @return the close function parameter 173 */ 174 public String getParamCloseFunction() { 175 176 return m_paramCloseFunction; 177 } 178 179 /** 180 * Sets the close function parameter.<p> 181 * 182 * @param closeFunction the close function parameter 183 */ 184 public void setParamCloseFunction(String closeFunction) { 185 186 m_paramCloseFunction = closeFunction; 187 } 188 189 /** 190 * @see org.opencms.workplace.editors.CmsEditor#initMessages() 191 */ 192 @Override 193 protected void initMessages() { 194 195 addMessages(Messages.get().getBundleName()); 196 // TODO: Auto-generated method stub 197 super.initMessages(); 198 } 199}