The Importer

From Apache OpenOffice Wiki
Jump to: navigation, search



Evaluating XImportFilter Parameters

The writing of an importer usually starts with extracting the required variables from the MediaDescriptor and the userData. These variables are required for the filtering component to operate correctly. Depending on the requirements of the individual filter, the first thing to do is to extract the information from the MediaDescriptor, referred to as aSourceData in the interface definition. This can be achieved as follows:

Get the number of elements in the MediaDescriptor

  sal_Int32 nLength = aSourceData.getLength();

Iterate through the MediaDescriptor to find the information needed: an input stream, a file name, or a URL.

  for (sal_Int32 i = 0; i < nLength; i++) {
          if (pValue[i].Name.equalsAsciiL (RTL_CONSTASCII_STRINGPARAM("InputStream")))
                  pValue[i].Value >>= xInputStream;
          else if (pValue[i].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("FileName")
                  pValue[i].Value >>= sFileName;
          else if (pValue[i].Name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("URL")))
                  pValue[i].Value >>= sURL;
  }

The msUserData parameter passed to importer() contains information that defines how the filter operates, so this information must be referenced as required.

Importer Filtering

An XInputStream implementation has now been obtained that contains all the information you want to process. From the filtering perspective, you can just read from this stream and carry out whatever processing is required in order for the input to be transformed into Apache OpenOffice XML. Once this has been done, however, you need to write the result to where it can be parsed into Apache OpenOffice's internal format. A Pipe can be used to achieve this. A Pipe is a form of buffer that can be written to and read from. For the importer, read from the XInputStream that was extracted from the MediaDescriptor, and once the filtering has taken place, write to a Pipe that has been created. This Pipe can be read from when it comes to parsing. This is how the Pipe is created:

  Reference <XInterface> xPipe;
 
  // We Create our pipe
  xPipe= XflatXml::xMSF->createInstance(OUString::createFromAscii("com.sun.star.io.Pipe"));
 
  // We get an inputStream to our Pipe
  Reference< com::sun::star::io::XInputStream > xPipeInput (xPipe,UNO_QUERY);
 
  // We get an OutputStream to our Pipe
  Reference< com::sun::star::io::XOutputStream > xTmpOutputStream (xPipe,UNO_QUERY);

The XInputStream can be read from, and the XOutputstream can be written to.

Parsing the Result

Once the desired Apache OpenOffice XML has been produced and written to the XOutputStream of the Pipe, the XinputStream of the Pipe can be parsed with the aid of the XDocumentHandler.

  // Create a Parser
  const OUString sSaxParser(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser"));
  Reference < com::sun::star::xml::sax::XParser > xSaxParser(xMSF->createInstance(sSaxParser), UNO_QUERY);
 
  // Create an InputSource using the Pipe
  com::sun::star::xml::sax::InputSource aInput;
  aInput.sSystemId = sFileName;           // File Name
  aInput.aInputStream = xPipeInput;       // Pipe InputStream 
 
  // Set the SAX Event Handler
  xSaxParser->setDocumentHandler(xHandler);
 
  // Parse the result
  try {  
    xSaxParser->parseStream(aInput);
  }
  catch (Exception &exc) {
    // Perform exception handling 
  }

Assuming that the XML was valid, no exceptions will be thrown and the importer will return true. At this stage, the filtering is complete and the imported document will be displayed.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages