Creating

From Apache OpenOffice Wiki
Jump to: navigation, search



A UCB content that implements the interface com.sun.star.ucb.XContentCreator acts as a factory for new resources. For example, a file system folder can be a creator for other file system folders and files.

A new content object created by the com.sun.star.ucb.XContentCreator implementation can be considered as an empty hull for a content object of a special type. This new content object has to be filled with some property values to become fully functional. For example, a file system folder could require a name, represented by the property Title in the UCB. The interface com.sun.star.ucb.XContentCreator offers ways to determine what contents can be created and what properties need to be set. Information can be obtained on the general type, such as FOLDER, DOCUMENT, or LINK, of the objects. After the required property values are set, the creation process needs to be committed by using the command "insert". Note that this command is always executed by the new content, not by the content creator, because the creator is not necessarily the parent of the new content. The flag ReplaceExisting in the "insert" argument com.sun.star.ucb.InsertCommandArgument usually is false, because the caller does not want to destroy an already existing resource. The "insert" command implementation makes the new content persistent in the appropriate storage medium.

To create a new resource:

  1. Obtain the interface com.sun.star.ucb.XContentCreator from a suitable UCB content.
  2. Call createNewContent() at the content creator. Supply information on the type of content to create with the arguments. The argument expected is a com.sun.star.ucb.ContentInfo struct.
  3. Obtain and set the property values that are mandatory for the content just created.
  4. Let the new content execute the command "insert" to complete the creation process.

Creating a new resource:

  import com.sun.star.uno.UnoRuntime;
  import com.sun.star.ucb.*;
  import com.sun.star.beans.PropertyValue;
  import com.sun.star.io.XInputStream;
 
  {
      XContent xContent = ...
 
      /////////////////////////////////////////////////////////////////////
      // Create a new file system file object...
      /////////////////////////////////////////////////////////////////////
 
      // Obtain content creator interface.
      XContentCreator xCreator = (XContentCreator)UnoRuntime.queryInterface( 
          XContentCreator.class, xContent);
 
      // Note: The data for aInfo may have been obtained using
      // XContentCreator::queryCreatableContentsInfo().
      // A number of possible types is listed below
 
      ContentInfo aInfo = new ContentInfo();
      aInfo.Type = "application/vnd.sun.staroffice.fsys-file";
      aInfo.Attributes = 0;
 
      // Create new, empty content.
      XContent xNewContent = xCreator.createNewContent(aInfo);
 
      if (xNewContent == null)
          ... error ...
 
      /////////////////////////////////////////////////////////////////////
      // Set mandatory properties...
      /////////////////////////////////////////////////////////////////////
 
      // Obtain a name for the new file.
      String aFilename = ...
 
      // Define property value sequence.
      PropertyValue[] aProps = new PropertyValue[1];
      PropertyValue aProp = new PropertyValue;
      aProp.Name = "Title";
      aProp.Handle = -1; // n/a
      aProp.Value = aFilename;
      aProps[ 0 ] = aProp;
      try {
          // Execute command "setPropertyValues".
          // using helper method executeCommand (see [CHAPTER:UCB.Using.Commands])
          executeCommand(xNewContent, "setPropertyValues",aProps);
      }
      catch (com.sun.star.ucb.CommandAbortedException e) {
          ... error ...
      }
      catch (com.sun.star.uno.Exception e) {
          ... error ...
      }
 
      /////////////////////////////////////////////////////////////////////
      // Write the new file to disk...
      /////////////////////////////////////////////////////////////////////
 
      // Obtain document data for the new file.
      XInputStream xData = ...
 
      // Fill argument structure...
      InsertCommandArgument aArg = new InsertCommandArgument();
      aArg.Data = xData;
      aArg.ReplaceExisting = false;
 
      try {
          // Execute command "insert".
          executeCommand(xNewContent, "insert", aArg);
      }
      catch (com.sun.star.ucb.CommandAbortedException e) {
          ... error ...
      }
      catch (com.sun.star.uno.Exception e) {
          ... error ...
      }
  }

Appendix C Universal Content Providers discusses the creation of contents for all available UCPs. The table below shows a number of com.sun.star.ucb.ContentInfo types for creatable contents. Additionally, you can ask the content creator for its creatable contents using queryCreatableContentsInfo(). The UCB reference in the SDK and on www.openoffice.org/ucb offers comprehensive information about creatable contents.

Data source Content Info Type Content Content Service that Creates the Contents
FILE "application/vnd.sun.staroffice.fsys-folder" folder com.sun.star.ucb.FileContent
"application/vnd.sun.staroffice.fsys-file" document
WebDAV and HTTP "application/vnd.sun.star.webdav-collection" folder com.sun.star.ucb.WebDAVFolderContent
"application/http-content" document
FTP "application/vnd.sun.staroffice.ftp-folder" folder com.sun.star.ucb.ChaosContent
"application/vnd.sun.staroffice.ftp-file" document
Hierarchy "application/vnd.sun.star.hier-folder" folder com.sun.star.ucb.HierarchyFolderContent
"application/vnd.sun.star.hier-link" Link
ZIP and JAR files "application/vnd.sun.star.pkg-folder" folder com.sun.star.ucb.PackageFolderContent
"application/vnd.sun.star.pkg-stream" document
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages