1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <WW8StreamImpl.hxx>
25 
26 #include <com/sun/star/uno/Reference.h>
27 #include <com/sun/star/io/XSeekable.hpp>
28 #include <com/sun/star/io/XStream.hpp>
29 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
30 
31 #include <doctokLoggers.hxx>
32 
33 namespace writerfilter {
34 namespace doctok
35 {
36 using namespace ::com::sun::star;
37 
38 #ifdef DEBUG
39 TagLogger::Pointer_t debug_logger(TagLogger::getInstance("DEBUG"));
40 #endif
41 
~WW8Stream()42 WW8Stream::~WW8Stream()
43 {
44 }
45 
WW8StreamImpl(uno::Reference<uno::XComponentContext> rContext,uno::Reference<io::XInputStream> rStream)46 WW8StreamImpl::WW8StreamImpl(uno::Reference<uno::XComponentContext> rContext,
47               uno::Reference<io::XInputStream> rStream)
48 : mrComponentContext(rContext), mrStream(rStream)
49 {
50     xFactory = uno::Reference<lang::XMultiComponentFactory>
51         (mrComponentContext->getServiceManager());
52 
53     uno::Sequence<uno::Any> aArgs( 1 );
54     aArgs[0] <<= mrStream;
55 
56     xOLESimpleStorage = uno::Reference<container::XNameContainer>
57         (xFactory->createInstanceWithArgumentsAndContext
58          (::rtl::OUString::createFromAscii
59           ("com.sun.star.embed.OLESimpleStorage"),
60           aArgs, mrComponentContext ),
61          uno::UNO_QUERY );
62 
63 }
64 
~WW8StreamImpl()65 WW8StreamImpl::~WW8StreamImpl()
66 {
67 }
68 
get(sal_uInt32 nOffset,sal_uInt32 nCount) const69 WW8Stream::Sequence WW8StreamImpl::get(sal_uInt32 nOffset,
70                                        sal_uInt32 nCount) const
71 {
72     uno::Sequence<sal_Int8> aSequence;
73 
74     if (nCount > 0)
75     {
76         uno::Reference< io::XSeekable > xSeek( mrStream, uno::UNO_QUERY_THROW );
77 
78         xSeek->seek(nOffset);
79 
80         sal_Int32 nRead = mrStream->readBytes(aSequence, nCount);
81 
82         Sequence aReturnSequence(const_cast<const sal_uInt8 *>
83                                  (reinterpret_cast<sal_uInt8 *>
84                                   (&(aSequence[0]))), nRead);
85 
86         return aReturnSequence;
87     }
88 
89     return WW8Stream::Sequence();
90 }
91 
getSubStream(const::rtl::OUString & sId)92 WW8Stream::Pointer_t WW8StreamImpl::getSubStream(const ::rtl::OUString & sId)
93 {
94     WW8Stream::Pointer_t pResult;
95 
96     try
97     {
98         if (xOLESimpleStorage.is())
99         {
100             if (xOLESimpleStorage->hasByName(sId))
101             {
102                 uno::Reference<io::XStream> xNewStream;
103                 {
104                     uno::Any aValue = xOLESimpleStorage->getByName(sId);
105                     aValue >>= xNewStream;
106                 }
107 
108                 if (xNewStream.is())
109                 {
110                     WW8Stream::Pointer_t
111                         pNew(new WW8StreamImpl(mrComponentContext,
112                                                xNewStream->getInputStream()));
113 
114                     pResult = pNew;
115                 }
116             }
117         }
118     }
119     catch (...)
120     {
121         throw ExceptionNotFound("Stream not found");
122     }
123 
124 
125     return pResult;
126 }
127 
getSubStreamNames() const128 string WW8StreamImpl::getSubStreamNames() const
129 {
130     string sResult;
131 
132     if (xOLESimpleStorage.is())
133     {
134         uno::Sequence<rtl::OUString> aSeq = xOLESimpleStorage->getElementNames();
135 
136         for (sal_uInt32 n = 0;
137              n < sal::static_int_cast<sal_uInt32>(aSeq.getLength()); ++n)
138         {
139             rtl::OUString aOUStr = aSeq[n];
140 
141             if (n > 0)
142                 sResult += ", ";
143 
144 #if 0
145             rtl::OString aOStr;
146             aOUStr.convertToString(&aOStr, RTL_TEXTENCODING_ASCII_US,
147                                     OUSTRING_TO_OSTRING_CVTFLAGS);
148 
149 
150             sResult += aOStr.getStr();
151 #endif
152             char sBuffer[256];
153             for (sal_uInt32 j = 0;
154                  j < sal::static_int_cast<sal_uInt32>(aOUStr.getLength()); ++j)
155             {
156                 if (isprint(aOUStr[j]))
157                 {
158                     sal_Unicode nC = aOUStr[j];
159 
160                     if (nC < 255)
161                         sResult += sal::static_int_cast<char>(nC);
162                     else
163                         sResult += ".";
164                 }
165                 else
166                 {
167                     snprintf(sBuffer, sizeof(sBuffer), "\\u%x", aOUStr[j]);
168                     sResult += sBuffer;
169                 }
170             }
171         }
172     }
173 
174     return sResult;
175 }
176 
getSubStreamUNames() const177 uno::Sequence<rtl::OUString> WW8StreamImpl::getSubStreamUNames() const
178 {
179     return xOLESimpleStorage->getElementNames();
180 }
181 
dump(OutputWithDepth<string> & o) const182 void WW8StreamImpl::dump(OutputWithDepth<string> & o) const
183 {
184     o.addItem("<stream>");
185 
186     Sequence aSeq;
187     sal_uInt32 nOffset = 0;
188     sal_uInt32 nStep = 16;
189 
190     do
191     {
192         aSeq = get(nOffset, nStep);
193         dumpLine(o, aSeq, nOffset, nStep);
194 
195         nOffset += nStep;
196     }
197     while (aSeq.getCount() == nStep);
198 
199     o.addItem("</stream>");
200 }
201 
202 }}
203