cmsresourcewrapperutils.java
来自「找了很久才找到到源代码」· Java 代码 · 共 436 行 · 第 1/2 页
JAVA
436 行
content.append(propName);
content.append(SUFFIX_PROP_INDIVIDUAL);
content.append("=");
content.append(individualValue);
content.append("\n");
content.append(propName);
content.append(SUFFIX_PROP_SHARED);
content.append("=");
content.append(sharedValue);
content.append("\n\n");
}
CmsWrappedResource wrap = new CmsWrappedResource(res);
wrap.setRootPath(addFileExtension(cms, path, EXTENSION_PROPERTIES));
wrap.setTypeId(CmsResourceTypePlain.getStaticTypeId());
wrap.setFolder(false);
CmsFile ret = wrap.getFile();
try {
ret.setContents(content.toString().getBytes(CmsEncoder.ENCODING_UTF_8));
} catch (UnsupportedEncodingException e) {
// this will never happen since UTF-8 is always supported
ret.setContents(content.toString().getBytes());
}
return ret;
}
/**
* Removes an added file extension from the resource name.<p>
*
* <ul>
* <li>If there is only one extension, nothing will be removed.</li>
* <li>If there are two extensions, the last one will be removed.</li>
* <li>If there are more than two extensions the last one will be removed and
* if then the last extension is a number, the extension with the number
* will be removed too.</li>
* </ul>
*
* @see #addFileExtension(CmsObject, String, String)
*
* @param cms the initialized CmsObject
* @param resourcename the resource name to remove the file extension from
* @param extension the extension to remove
*
* @return the resource name without the removed file extension
*/
public static String removeFileExtension(CmsObject cms, String resourcename, String extension) {
if (resourcename.equals("")) {
resourcename = "/";
}
// get the filename without the path
String name = CmsResource.getName(resourcename);
String[] tokens = name.split("\\.");
String suffix = null;
// check if there is more than one extension
if (tokens.length > 2) {
// check if last extension is "jsp"
if (extension.equalsIgnoreCase(tokens[tokens.length - 1])) {
suffix = "." + extension;
// check if there is another extension with a numeric index
if (tokens.length > 3) {
try {
int index = Integer.valueOf(tokens[tokens.length - 2]).intValue();
suffix = "." + index + suffix;
} catch (NumberFormatException ex) {
// noop
}
}
}
} else if (tokens.length == 2) {
// there is only one extension!!
// only remove the last extension, if the resource without the extension exists
// and the extension fits
if ((cms.existsResource(CmsResource.getFolderPath(resourcename) + tokens[0]))
&& (extension.equals(tokens[1]))) {
suffix = "." + tokens[1];
}
}
if (suffix != null) {
String path = resourcename.substring(0, resourcename.length() - suffix.length());
return path;
}
return resourcename;
}
/**
* Removes the UTF-8 marker from the beginning of the byte array.<p>
*
* @param content the byte array where to remove the UTF-8 marker
*
* @return the byte with the removed UTF-8 marker at the beginning
*/
public static byte[] removeUtf8Marker(byte[] content) {
if ((content != null)
&& (content.length >= 3)
&& (content[0] == UTF8_MARKER[0])
&& (content[1] == UTF8_MARKER[1])
&& (content[2] == UTF8_MARKER[2])) {
byte[] ret = new byte[content.length - UTF8_MARKER.length];
System.arraycopy(content, 3, ret, 0, content.length - UTF8_MARKER.length);
return ret;
}
return content;
}
/**
* Takes the content which should be formatted as a property file and set them
* as properties to the resource.<p>
*
* @see #createPropertyFile(CmsObject, CmsResource, String)
*
* @param cms the initialized CmsObject
* @param resourcename the name of the resource where to set the properties
* @param content the properties to set (formatted as a property file)
*
* @throws CmsException if something goes wrong
*/
public static void writePropertyFile(CmsObject cms, String resourcename, byte[] content) throws CmsException {
Properties properties = new Properties();
try {
String props = CmsEncoder.createString(content, CmsEncoder.ENCODING_UTF_8);
props = unescapeString(props);
props = CmsEncoder.encodeJavaEntities(props, CmsEncoder.ENCODING_ISO_8859_1);
byte[] modContent = props.getBytes(CmsEncoder.ENCODING_ISO_8859_1);
properties.load(new ByteArrayInputStream(modContent));
List propList = new ArrayList();
Iterator it = properties.entrySet().iterator();
while (it.hasNext()) {
Map.Entry e = (Map.Entry)it.next();
String key = (String)e.getKey();
String value = (String)e.getValue();
if (key.endsWith(SUFFIX_PROP_SHARED)) {
propList.add(new CmsProperty(
key.substring(0, key.length() - SUFFIX_PROP_SHARED.length()),
null,
value));
} else if (key.endsWith(SUFFIX_PROP_INDIVIDUAL)) {
propList.add(new CmsProperty(
key.substring(0, key.length() - SUFFIX_PROP_INDIVIDUAL.length()),
value,
null));
}
}
cms.writePropertyObjects(resourcename, propList);
} catch (IOException e) {
// noop
}
}
/**
* Escapes the value of a property in OpenCms to be displayed
* correctly in a property file.<p>
*
* Mainly handles all escaping sequences that start with a backslash.<p>
*
* @see #unescapeString(String)
*
* @param value the value with the string to be escaped
*
* @return the escaped string
*/
private static String escapeString(String value) {
Map substitutions = new HashMap();
substitutions.put("\n", "\\n");
substitutions.put("\t", "\\t");
substitutions.put("\r", "\\r");
return CmsStringUtil.substitute(value, substitutions);
}
/**
* Unescapes the value of a property in a property file to
* be saved correctly in OpenCms.<p>
*
* Mainly handles all escaping sequences that start with a backslash.<p>
*
* @see #escapeString(String)
*
* @param value the value taken form the property file
*
* @return the unescaped string value
*/
private static String unescapeString(String value) {
Matcher matcher = PATTERN_UNESCAPE.matcher(value);
return matcher.replaceAll("\\\\\\\\$1");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?