Write a file programatically

From OpenCms Wiki
(Difference between revisions)
Jump to: navigation, search
(added sample code)
m (added Syntax High.)
 
(One intermediate revision by one user not shown)
Line 1: Line 1:
 
How to write a file programatically
 
How to write a file programatically
  
 +
<source lang="java">
 
  //ensure we're in the offline project.
 
  //ensure we're in the offline project.
 
  CmsRequestContext cmsContext = cmsObj.getRequestContext();
 
  CmsRequestContext cmsContext = cmsObj.getRequestContext();
Line 10: Line 11:
  
 
  byte [] content = your data
 
  byte [] content = your data
  List properties = list of CmsProperties
+
 
 +
// OpenCms properties creation example
 +
// by David Sánchez Hernández [http://lists.opencms.org/pipermail/opencms-dev/2007q2/028211.html]
 +
  List properties = new ArrayList();
 +
 
 +
// Setting template_elements property
 +
CmsProperty prop = new CmsProperty();
 +
prop.setName(CmsPropertyDefinition.PROPERTY_TEMPLATE_ELEMENTS);
 +
prop.setValue("/system/modules/xxxxx.jsp",CmsProperty.TYPE_SHARED);
 +
properties.add(prop);
 +
 
 +
// Setting title property
 +
prop = new CmsProperty();
 +
prop.setName(CmsPropertyDefinition.PROPERTY_TITLE);
 +
prop.setValue("Title",CmsProperty.TYPE_SHARED);
 +
properties.add(prop);
  
 
  //get the fixed up filename as shown in CmsNewResourceUpload
 
  //get the fixed up filename as shown in CmsNewResourceUpload
  String newResname = cmsObj.getRequestContext().getFileTranslator().translateResource(path + filename);
+
  String newResname = cmsObj.getRequestContext().getFileTranslator()
 +
                          .translateResource(path + filename);
  
  int resTypeId = OpenCms.getResourceManager().getDefaultTypeForName(newResname).getTypeId();
+
  int resTypeId = OpenCms.getResourceManager()
 +
                    .getDefaultTypeForName(newResname).getTypeId();
  
 
  cmsObj.createResource(newResname, resTypeId, content, properties);
 
  cmsObj.createResource(newResname, resTypeId, content, properties);
 
  cmsObj.unlockResource(newResname);
 
  cmsObj.unlockResource(newResname);
 
  cmsObj.publishResource(newResname);
 
  cmsObj.publishResource(newResname);
 
+
</source>
 
+
 
+
  
 
You can also try looking at CmsNewResourceUpload.java from the OpenCms sourcecode.  It's available from opencms.org via an interactive html browser.
 
You can also try looking at CmsNewResourceUpload.java from the OpenCms sourcecode.  It's available from opencms.org via an interactive html browser.

Latest revision as of 22:50, 6 October 2011

How to write a file programatically

 //ensure we're in the offline project.
 CmsRequestContext cmsContext = cmsObj.getRequestContext();
 CmsProject curProject = cmsContext.currentProject();
 if(curProject.isOnlineProject()){
       CmsProject offlineProject = cmsObj.readProject("Offline");
       cmsContext.setCurrentProject(offlineProject);
 }
 
 byte [] content = your data
 
 // OpenCms properties creation example
 // by David Sánchez Hernández [http://lists.opencms.org/pipermail/opencms-dev/2007q2/028211.html]
 List properties = new ArrayList();
 
 // Setting template_elements property
 CmsProperty prop = new CmsProperty();
 prop.setName(CmsPropertyDefinition.PROPERTY_TEMPLATE_ELEMENTS);
 prop.setValue("/system/modules/xxxxx.jsp",CmsProperty.TYPE_SHARED);
 properties.add(prop);
 
 // Setting title property
 prop = new CmsProperty();
 prop.setName(CmsPropertyDefinition.PROPERTY_TITLE);
 prop.setValue("Title",CmsProperty.TYPE_SHARED);
 properties.add(prop);
 
 //get the fixed up filename as shown in CmsNewResourceUpload
 String newResname = cmsObj.getRequestContext().getFileTranslator()
                           .translateResource(path + filename);
 
 int resTypeId = OpenCms.getResourceManager()
                    .getDefaultTypeForName(newResname).getTypeId();
 
 cmsObj.createResource(newResname, resTypeId, content, properties);
 cmsObj.unlockResource(newResname);
 cmsObj.publishResource(newResname);

You can also try looking at CmsNewResourceUpload.java from the OpenCms sourcecode. It's available from opencms.org via an interactive html browser.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox