ContentHandler.cpp 3.24 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1 2 3
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
Claudio Valerio's avatar
Claudio Valerio committed
4
 * the Free Software Foundation, either version 2 of the License, or
Claudio Valerio's avatar
Claudio Valerio committed
5 6 7 8 9 10 11 12 13 14
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Claudio Valerio's avatar
Claudio Valerio committed
15

16 17 18 19 20 21 22 23 24
#include "ContentHandler.h"
#include "Filter.h"
#include "FlateDecode.h"

#include <iostream>

#include <string>
#include <string.h>

25 26
#include "core/memcheck.h"

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
using namespace merge_lib;
using namespace std;
//concatenate stream of all objects which contain Content of Page
void ContentHandler::_processObjectContent(unsigned int startOfPageElement)
{

   unsigned int endOfPage = _findEndOfElementContent(startOfPageElement);
   _concatenatedStream = _retrieveStreamContent(_page, startOfPageElement, endOfPage);

   FlateDecode flate;   
   flate.encode(_concatenatedStream);
}

//write concatenated stream to Page object
void ContentHandler::_changeObjectContent(unsigned int startOfPageElement)
{

   unsigned int endOfPage = _findEndOfElementContent(startOfPageElement);
   _page->forgetAboutChildren(startOfPageElement, endOfPage);
   _page->eraseContent(startOfPageElement, endOfPage - startOfPageElement);
   unsigned int endOfObjectDescription = _pageContent.rfind(">>");
   const char * length = "/Filter /FlateDecode\n/Length ";
   unsigned int sizeOfLength = strlen(length);
   _page->insertToContent(endOfObjectDescription, length, sizeOfLength);
   _page->insertToContent(endOfObjectDescription + sizeOfLength, Utils::uIntToStr(_concatenatedStream.size()).c_str());
   _page->appendContent("\nstream\n");
   _page->appendContent(_concatenatedStream);
   _page->appendContent("endstream\n");
   _page->forgetStreamInFile();
}

//get content of stream 
// object - object with stream
//leftBound - left bound of object's content
//rightBound - right bound of object's content
string ContentHandler::_retrieveStreamContent(merge_lib::Object * object, unsigned int leftBound, unsigned int rightBound)
{   
   return (object->hasStream()) ? 
      _getStreamFromContent(object) :
      _getStreamFromReferencies(object, leftBound, rightBound);

}
//get stream from Array elements
string ContentHandler::_getStreamFromReferencies(merge_lib::Object * objectWithArray, unsigned int leftBound, unsigned int rightBound)
{
   std::string result;
   std::vector<Object *> referencies = objectWithArray->getSortedByPositionChildren(leftBound, rightBound);
   for(size_t i = 0; i < referencies.size(); ++i)
   {
      result.append(_retrieveStreamContent(referencies[i], 0, referencies[i]->getObjectContent().size()));      
   }
   objectWithArray->forgetAboutChildren(leftBound,rightBound);
   return result;
}

//get stream from Object
string ContentHandler::_getStreamFromContent(merge_lib::Object * objectWithStream)
{
   Filter filter(objectWithStream);    
   string decodedStream;
   filter.getDecodedStream(decodedStream);

   return decodedStream;
}