use of org.opencms.file.CmsProperty in project opencms-core by alkacon.
the class CmsVfsSitemapService method createSitemapBrokenLinkBean.
/**
* Creates a "broken link" bean based on a resource.<p>
*
* @param resource the resource
*
* @return the "broken link" bean with the data from the resource
*
* @throws CmsException if something goes wrong
*/
protected CmsBrokenLinkBean createSitemapBrokenLinkBean(CmsResource resource) throws CmsException {
CmsObject cms = getCmsObject();
CmsProperty titleProp = cms.readPropertyObject(resource, CmsPropertyDefinition.PROPERTY_TITLE, true);
String defaultTitle = "";
String title = titleProp.getValue(defaultTitle);
String path = cms.getSitePath(resource);
String subtitle = path;
return new CmsBrokenLinkBean(resource.getStructureId(), title, subtitle);
}
use of org.opencms.file.CmsProperty in project opencms-core by alkacon.
the class CmsCloneModuleThread method cloneResourceTypes.
/**
* Clones/copies the resource types.<p>
*
* @param sourceModule the source module
* @param targetModule the target module
* @param sourcePathPart the source path part
* @param targetPathPart the target path part
* @param keys the map where to put in the messages of the resource type
*
* @return a map with source resource types as key and the taregt resource types as value
*/
private Map<I_CmsResourceType, I_CmsResourceType> cloneResourceTypes(CmsModule sourceModule, CmsModule targetModule, String sourcePathPart, String targetPathPart, Map<String, String> keys) {
Map<I_CmsResourceType, I_CmsResourceType> resourceTypeMapping = new HashMap<I_CmsResourceType, I_CmsResourceType>();
List<I_CmsResourceType> targetResourceTypes = new ArrayList<I_CmsResourceType>();
for (I_CmsResourceType sourceResType : targetModule.getResourceTypes()) {
// get the class name attribute
String className = sourceResType.getClassName();
// create the class instance
I_CmsResourceType targetResType;
try {
if (className != null) {
className = className.trim();
}
int newId = -1;
boolean exists = true;
do {
newId = new Random().nextInt((99999)) + 10000;
try {
OpenCms.getResourceManager().getResourceType(newId);
} catch (CmsLoaderException e) {
exists = false;
}
} while (exists);
targetResType = (I_CmsResourceType) Class.forName(className).newInstance();
for (String mapping : sourceResType.getConfiguredMappings()) {
targetResType.addMappingType(mapping);
}
targetResType.setAdjustLinksFolder(sourceResType.getAdjustLinksFolder());
if (targetResType instanceof A_CmsResourceType) {
A_CmsResourceType concreteTargetResType = (A_CmsResourceType) targetResType;
for (CmsProperty prop : sourceResType.getConfiguredDefaultProperties()) {
if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(prop.getValue())) {
prop.setStructureValue(prop.getStructureValue().replaceAll(sourceModule.getName(), targetModule.getName()).replaceAll(sourcePathPart, targetPathPart));
prop.setResourceValue(prop.getResourceValue().replaceAll(sourceModule.getName(), targetModule.getName()).replaceAll(sourcePathPart, targetPathPart));
}
concreteTargetResType.addDefaultProperty(prop);
}
for (CmsConfigurationCopyResource conres : sourceResType.getConfiguredCopyResources()) {
concreteTargetResType.addCopyResource(conres.getSource(), conres.getTarget(), conres.getTypeString());
}
}
for (Map.Entry<String, String> entry : sourceResType.getConfiguration().entrySet()) {
targetResType.addConfigurationParameter(entry.getKey(), entry.getValue().replaceAll(sourceModule.getName(), targetModule.getName()));
}
targetResType.setAdditionalModuleResourceType(true);
targetResType.initConfiguration(alterPrefix(sourceResType.getTypeName(), m_cloneInfo.getSourceNamePrefix(), m_cloneInfo.getTargetNamePrefix()), newId + "", sourceResType.getClassName());
keys.put(sourceResType.getTypeName(), targetResType.getTypeName());
targetResourceTypes.add(targetResType);
resourceTypeMapping.put(sourceResType, targetResType);
} catch (Exception e) {
// resource type is unknown, use dummy class to import the module resources
targetResType = new CmsResourceTypeUnknown();
// write an error to the log
LOG.error(org.opencms.configuration.Messages.get().getBundle().key(org.opencms.configuration.Messages.ERR_UNKNOWN_RESTYPE_CLASS_2, className, targetResType.getClass().getName()), e);
}
}
targetModule.setResourceTypes(targetResourceTypes);
return resourceTypeMapping;
}
use of org.opencms.file.CmsProperty in project opencms-core by alkacon.
the class CmsResourceWrapperUtils method writePropertyFile.
/**
* 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<CmsProperty> propList = new ArrayList<CmsProperty>();
Iterator<Map.Entry<Object, Object>> it = properties.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Object, Object> e = 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);
String newType = properties.getProperty(PROPERTY_RESOURCE_TYPE);
if (newType != null) {
newType = newType.trim();
if (OpenCms.getResourceManager().hasResourceType(newType)) {
I_CmsResourceType newTypeObj = OpenCms.getResourceManager().getResourceType(newType);
cms.chtype(resourcename, newTypeObj.getTypeId());
}
}
} catch (IOException e) {
// noop
}
}
use of org.opencms.file.CmsProperty in project opencms-core by alkacon.
the class CmsResourceWrapperUtils method createPropertyFile.
/**
* Creates a virtual CmsFile with the individual and shared properties as content.<p>
*
* For example looks like this:<br/>
* Title.i=The title of the resource set as individual property<br/>
* Title.s=The title of the resource set as shared property<br/>
*
* @see #writePropertyFile(CmsObject, String, byte[])
*
* @param cms the initialized CmsObject
* @param res the resource where to read the properties from
* @param path the full path to set for the created property file
*
* @return the created CmsFile with the individual and shared properties as the content
*
* @throws CmsException if something goes wrong
*/
public static CmsFile createPropertyFile(CmsObject cms, CmsResource res, String path) throws CmsException {
StringBuffer content = new StringBuffer();
// header
content.append("# Properties for resource ");
content.append(res.getRootPath());
content.append("\n");
content.append("#\n");
content.append("# ${property_name}.i : individual property\n");
content.append("# ${property_name}.s : shared property\n\n");
List<CmsPropertyDefinition> propertyDef = cms.readAllPropertyDefinitions();
Map<String, CmsProperty> activeProperties = CmsProperty.getPropertyMap(cms.readPropertyObjects(res, false));
String resourceType = OpenCms.getResourceManager().getResourceType(res).getTypeName();
content.append("resourceType=");
content.append(resourceType);
content.append("\n\n");
// iterate over all possible properties for the resource
Iterator<CmsPropertyDefinition> i = propertyDef.iterator();
while (i.hasNext()) {
CmsPropertyDefinition currentPropertyDef = i.next();
String propName = currentPropertyDef.getName();
CmsProperty currentProperty = activeProperties.get(propName);
if (currentProperty == null) {
currentProperty = new CmsProperty();
}
String individualValue = currentProperty.getStructureValue();
String sharedValue = currentProperty.getResourceValue();
if (individualValue == null) {
individualValue = "";
}
if (sharedValue == null) {
sharedValue = "";
}
individualValue = escapeString(individualValue);
sharedValue = escapeString(sharedValue);
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));
int plainId = OpenCms.getResourceManager().getResourceType(CmsResourceTypePlain.getStaticTypeName()).getTypeId();
wrap.setTypeId(plainId);
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;
}
use of org.opencms.file.CmsProperty in project opencms-core by alkacon.
the class A_CmsResourceType method processDefaultProperties.
/**
* Returns a list of property objects that are attached to the resource on creation.<p>
*
* It's possible to use OpenCms macros for the property values.
* Please see {@link CmsMacroResolver} for allowed macro values.<p>
*
* @param properties the (optional) properties provided by the user
* @param resolver the resolver used to resolve the macro values
*
* @return a list of property objects that are attached to the resource on creation
*/
protected List<CmsProperty> processDefaultProperties(List<CmsProperty> properties, CmsMacroResolver resolver) {
if ((m_defaultProperties == null) || (m_defaultProperties.size() == 0)) {
// no default properties are defined
return properties;
}
// the properties must be copied since the macros could contain macros that are
// resolved differently for every user / context
ArrayList<CmsProperty> result = new ArrayList<CmsProperty>();
Iterator<CmsProperty> i = m_defaultProperties.iterator();
while (i.hasNext()) {
// create a clone of the next property
CmsProperty property = (i.next()).clone();
// resolve possible macros in the property values
if (property.getResourceValue() != null) {
property.setResourceValue(resolver.resolveMacros(property.getResourceValue()));
}
if (property.getStructureValue() != null) {
property.setStructureValue(resolver.resolveMacros(property.getStructureValue()));
}
// save the new property in the result list
result.add(property);
}
// add the original properties
if (properties != null) {
result.addAll(properties);
}
// return the result
return result;
}
Aggregations