Difference between revisions of "Documentation/DevGuide/OfficeDev/The Importer"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Robot: Changing Category:Office Development)
 
(3 intermediate revisions by 2 users not shown)
Line 7: Line 7:
 
|NextPage=Documentation/DevGuide/OfficeDev/The Exporter
 
|NextPage=Documentation/DevGuide/OfficeDev/The Exporter
 
}}
 
}}
{{DISPLAYTITLE:The Importer}}
+
{{Documentation/DevGuideLanguages|Documentation/DevGuide/OfficeDev/{{SUBPAGENAME}}}}
 +
{{DISPLAYTITLE:The Importer}}
 
=== Evaluating XImportFilter Parameters ===
 
=== Evaluating XImportFilter Parameters ===
  
The writing of an importer usually starts with extracting the required variables from the <code>Mediadescriptor</code> and the <code>userData</code>. 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 <code>MediaDescriptor</code>, referred to as <code>aSourceData</code> in the interface definition. This can be achieved as follows:
+
The writing of an importer usually starts with extracting the required variables from the <code>MediaDescriptor</code> and the <code>userData</code>. 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 <code>MediaDescriptor</code>, referred to as <code>aSourceData</code> in the interface definition. This can be achieved as follows:
  
 
Get the number of elements in the <code>MediaDescriptor</code>
 
Get the number of elements in the <code>MediaDescriptor</code>
<source lang="cpp">
+
<syntaxhighlight lang="cpp">
 
   sal_Int32 nLength = aSourceData.getLength();  
 
   sal_Int32 nLength = aSourceData.getLength();  
</source>
+
</syntaxhighlight>
 
Iterate through the <code>MediaDescriptor</code> to find the information needed: an input stream, a file name, or a URL.
 
Iterate through the <code>MediaDescriptor</code> to find the information needed: an input stream, a file name, or a URL.
<source lang="cpp">
+
<syntaxhighlight lang="cpp">
 
   for (sal_Int32 i = 0; i < nLength; i++) {
 
   for (sal_Int32 i = 0; i < nLength; i++) {
 
           if (pValue[i].Name.equalsAsciiL (RTL_CONSTASCII_STRINGPARAM("InputStream")))
 
           if (pValue[i].Name.equalsAsciiL (RTL_CONSTASCII_STRINGPARAM("InputStream")))
Line 26: Line 27:
 
                   pValue[i].Value >>= sURL;
 
                   pValue[i].Value >>= sURL;
 
   }
 
   }
</source>
+
</syntaxhighlight>
 
The <code>msUserData</code> parameter passed to <code>importer()</code> contains information that defines how the filter operates, so this information must be referenced as required.
 
The <code>msUserData</code> parameter passed to <code>importer()</code> contains information that defines how the filter operates, so this information must be referenced as required.
  
 
=== Importer Filtering ===
 
=== Importer Filtering ===
  
An <code>XInputStream</code> implementation has now been obtained that contains all of 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 {{PRODUCTNAME}} XML. Once this has been done, however, you need to write the result to where it can be parsed into {{PRODUCTNAME}}'s internal format. A <code>Pipe</code> can be used to achieve this. A <code>Pipe</code> is a form of buffer that can be written to and read from. For the <idlm>com.sun.star.xml.XImportFilter:importer</idlm>, read from the <code>XInputStream</code> that was extracted from the <code>MediaDescriptor</code>, and once the filtering has taken place, write to a <code>Pipe</code> that has been created. This <code>Pipe</code> can be read from when it comes to parsing. This is how the <code>Pipe</code> is created:
+
An <code>XInputStream</code> 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 {{AOo}} XML. Once this has been done, however, you need to write the result to where it can be parsed into {{AOo}}'s internal format. A <code>Pipe</code> can be used to achieve this. A <code>Pipe</code> is a form of buffer that can be written to and read from. For the <idlm>com.sun.star.xml.XImportFilter:importer</idlm>, read from the <code>XInputStream</code> that was extracted from the <code>MediaDescriptor</code>, and once the filtering has taken place, write to a <code>Pipe</code> that has been created. This <code>Pipe</code> can be read from when it comes to parsing. This is how the <code>Pipe</code> is created:
<source lang="cpp">
+
<syntaxhighlight lang="cpp">
 
   Reference <XInterface> xPipe;
 
   Reference <XInterface> xPipe;
 
    
 
    
Line 43: Line 44:
 
   // We get an OutputStream to our Pipe
 
   // We get an OutputStream to our Pipe
 
   Reference< com::sun::star::io::XOutputStream > xTmpOutputStream (xPipe,UNO_QUERY);
 
   Reference< com::sun::star::io::XOutputStream > xTmpOutputStream (xPipe,UNO_QUERY);
</source>
+
</syntaxhighlight>
 
The <code>XInputStream</code> can be read from, and the <code>XOutputstream</code> can be written to.
 
The <code>XInputStream</code> can be read from, and the <code>XOutputstream</code> can be written to.
  
 
=== Parsing the Result ===
 
=== Parsing the Result ===
  
Once the desired {{PRODUCTNAME}} XML has been produced and written to the <code>XOutputStream</code> of the <code>Pipe</code>, the <code>XinputStream</code> of the <code>Pipe</code> can be parsed with the aid of the <code>XdocumentHandler</code>.
+
Once the desired {{AOo}} XML has been produced and written to the <code>XOutputStream</code> of the <code>Pipe</code>, the <code>XinputStream</code> of the <code>Pipe</code> can be parsed with the aid of the <code>XDocumentHandler</code>.
<source lang="cpp">
+
<syntaxhighlight lang="cpp">
 
   // Create a Parser
 
   // Create a Parser
 
   const OUString sSaxParser(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser"));
 
   const OUString sSaxParser(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser"));
Line 66: Line 67:
 
     xSaxParser->parseStream(aInput);
 
     xSaxParser->parseStream(aInput);
 
   }
 
   }
   catch( Exception &exc){
+
   catch (Exception &exc) {
 
     // Perform exception handling  
 
     // Perform exception handling  
 
   }  
 
   }  
</source>
+
</syntaxhighlight>
 
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.
 
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.
  
 
{{PDL1}}
 
{{PDL1}}
  
[[Category:Documentation/Developers Guide/Office Development]]
+
[[Category:Documentation/Developer's Guide/Office Development]]

Latest revision as of 12:27, 3 January 2021



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