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 #ifndef INCLUDED_X_NOTE_HELPER_IMPL_HXX
25 #define INCLUDED_X_NOTE_HELPER_IMPL_HXX
26 
27 namespace writerfilter {
28 namespace doctok
29 {
30 template <class T>
getCount() const31 sal_uInt32 XNoteHelper<T>::getCount() const
32 {
33     sal_uInt32 nResult = 0;
34 
35     if (mpCps.get() != NULL && mpCps->getCount() > 8)
36         nResult = ( mpCps->getCount() / 4) - 2;
37 
38     return nResult;
39 }
40 
41 template <class T>
getCpAndFc(sal_uInt32 nPos)42 CpAndFc XNoteHelper<T>::getCpAndFc(sal_uInt32 nPos)
43 {
44     // There are getCount() + 1 entries in mpOffsets => greater
45     if (nPos > getCount())
46         throw ExceptionNotFound("getCpAndFc");
47 
48     Cp aCp(mCpAndFcOffset.getCp() + mpCps->getU32(nPos * 4));
49     Fc aFc(mpPieceTable->cp2fc(aCp));
50     CpAndFc aCpAndFc(aCp, aFc, meType);
51 
52     return aCpAndFc;
53 }
54 
55 template <class T>
getRefCpAndFc(sal_uInt32 nPos)56 CpAndFc XNoteHelper<T>::getRefCpAndFc(sal_uInt32 nPos)
57 {
58     // There are getCount() entries in mpRefs => greater or equal
59     if (nPos >= getCount())
60         throw ExceptionNotFound("");
61 
62     Cp aCp(mpRefs->getFc(nPos));
63     Fc aFc(mpPieceTable->cp2fc(aCp));
64     CpAndFc aCpAndFc(aCp, aFc, meType);
65 
66     return aCpAndFc;
67 }
68 
69 template <class T>
70 writerfilter::Reference<Stream>::Pointer_t
get(sal_uInt32 nPos)71 XNoteHelper<T>::get(sal_uInt32 nPos)
72 {
73     // There are getCount() entries => greater or equal
74     if (nPos >= getCount())
75         throw ExceptionNotFound("get");
76 
77     writerfilter::Reference<Stream>::Pointer_t pResult;
78 
79     CpAndFc aCpAndFcStart(getCpAndFc(nPos));
80     CpAndFc aCpAndFcEnd(getCpAndFc(nPos + 1));
81 
82     if (aCpAndFcStart < aCpAndFcEnd)
83         pResult = writerfilter::Reference<Stream>::Pointer_t
84             (new WW8DocumentImpl(*mpDocument, aCpAndFcStart, aCpAndFcEnd));
85 
86     return pResult;
87 }
88 
89 template <class T>
getIndexOfCpAndFc(const CpAndFc & rCpAndFc)90 sal_uInt32 XNoteHelper<T>::getIndexOfCpAndFc(const CpAndFc & rCpAndFc)
91 {
92    sal_uInt32 nResult = getCount();
93 
94    sal_uInt32 n = nResult;
95    while (n > 0)
96    {
97        --n;
98 
99        Cp aCp(mpRefs->getFc(n));
100        Fc aFc(mpPieceTable->cp2fc(aCp));
101        CpAndFc aCpAndFc(aCp, aFc, meType);
102 
103        if (aCpAndFc <= rCpAndFc)
104        {
105            nResult = n;
106            break;
107        }
108     }
109 
110     return nResult;
111 }
112 
113 template <class T>
114 writerfilter::Reference<Stream>::Pointer_t
get(const CpAndFc & rCpAndFc)115 XNoteHelper<T>::get(const CpAndFc & rCpAndFc)
116 {
117     writerfilter::Reference<Stream>::Pointer_t pResult;
118 
119     sal_uInt32 n = getIndexOfCpAndFc(rCpAndFc);
120 
121     if (n < getCount())
122         pResult = get(n);
123 
124     return pResult;
125 }
126 
127 template <class T>
128 T *
getRef(sal_uInt32 nIndex)129 XNoteHelper<T>::getRef(sal_uInt32 nIndex)
130 {
131     return mpRefs->getEntryPointer(nIndex);
132 }
133 
134 template <class T>
135 T *
getRef(const CpAndFc & rCpAndFc)136 XNoteHelper<T>::getRef(const CpAndFc & rCpAndFc)
137 {
138     T * pResult = NULL;
139 
140     sal_uInt32 n = getIndexOfCpAndFc(rCpAndFc);
141 
142     if (n < getCount())
143     {
144         pResult = getRef(n);
145     }
146 
147     return pResult;
148 }
149 
150 template <class T>
init()151 void XNoteHelper<T>::init()
152 {
153     for (sal_uInt32 n = 0; n < getCount(); ++n)
154     {
155         CpAndFc aCpAndFc(getCpAndFc(n));
156         mpDocument->insertCpAndFc(aCpAndFc);
157 
158         CpAndFc aCpAndFcRef(getRefCpAndFc(n));
159         mpDocument->insertCpAndFc(aCpAndFcRef);
160     }
161 }
162 }}
163 
164 #endif // INCLUDED_X_NOTE_HELPER_IMPL_HXX
165