This file is indexed.

/usr/include/crystalspace-2.0/csutil/documenthelper.h is in libcrystalspace-dev 2.0+dfsg-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
  Copyright (C) 2005,2007 by Marten Svanfeldt

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library 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
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef __CSUTIL_DOCUMENTHELPER_H__
#define __CSUTIL_DOCUMENTHELPER_H__

/**\file
 * Helper functions and classes which operate on iDocumentNode and 
 * iDocumentNodeIterator.
 */

#include "csutil/csstring.h"
#include "csutil/refarr.h"
#include "csutil/regexp.h"
#include "csutil/scf_implementation.h"
#include "csutil/util.h"

#include "iutil/document.h"

namespace CS
{
namespace DocSystem
{
  namespace Implementation
  {
    /**
    * Filtering iDocumentNodeIterator.
    * Filters another iterator with a functor.
    */
    template<class T>
    class FilterDocumentNodeIterator : public 
      scfImplementation1 <FilterDocumentNodeIterator<T>, 
	  iDocumentNodeIterator>
    {
    public:
      FilterDocumentNodeIterator (csRef<iDocumentNodeIterator> parent,
        T filter) : scfImplementation1<FilterDocumentNodeIterator<T>, 
	    iDocumentNodeIterator> (this), parent (parent), filter (filter)
      {
        ForwardIterator ();
      }

      // -- iDocumentNodeIterator
      /// Are there more elements?
      virtual bool HasNext ()
      {
        return nextElement.IsValid ();
      }

      /// Get next element.
      virtual csRef<iDocumentNode> Next ()
      {
        csRef<iDocumentNode> current = nextElement;
        ForwardIterator ();
        return current;
      }

      virtual size_t GetNextPosition () 
      { 
        if (nextElement.IsValid ())
          return parent->GetNextPosition (); 
        else
          return parent->GetEndPosition (); 
      }

      virtual size_t GetEndPosition ()
      { return parent->GetEndPosition (); }

    private:
      void ForwardIterator ()
      {
        if (!parent) nextElement = 0;

        while (parent->HasNext ())
        {
          csRef<iDocumentNode> parentNext = parent->Next ();
          if (filter (parentNext))
          {
            nextElement = parentNext;
            return;
          }
        }
        nextElement = 0;
        parent = 0;
      }

      csRef<iDocumentNodeIterator> parent;
      T filter;
      csRef<iDocumentNode> nextElement;
    };
  } // namespace Implementation
  
  /**
   * Remove duplicate child-nodes.
   * The functor T is used to determine what should be seen
   * as equal nodes.
   * This is potentially an O(n^2) operation!
   */
  template<class T>
  void RemoveDuplicateChildren (iDocumentNode *rootNode, T eq)
  {
    csRef<iDocumentNodeIterator> it = rootNode->GetNodes ();
    RemoveDuplicateChildren (rootNode, it, eq);
  }

  /**
   * Remove duplicate child-nodes.
   * The functor T is used to determine what should be seen
   * as equal nodes.
   * This is potentially an O(n^2) operation!
   */
  template<class T>
  void RemoveDuplicateChildren (iDocumentNode *rootNode,
    csRef<iDocumentNodeIterator> childIt, T eq)
  {
    typedef csRefArray<iDocumentNode> NodeListType;
    NodeListType nodesToRemove;
    NodeListType nodesToKeep;

    if (!childIt) return;

    while (childIt->HasNext ())
    {
      csRef<iDocumentNode> node = childIt->Next ();
      //compare it to those we already have
      bool keep = true;

      NodeListType::Iterator it = nodesToKeep.GetIterator ();
      while (it.HasNext ())
      {
        csRef<iDocumentNode> keepNode = it.Next ();
        if (keepNode->Equals (node))
        {
          keep = false; 
          break;
        }
        if (eq (node, keepNode))
        {
          keep = false;
          break;
        }
      }

      if (keep)
      {
        nodesToKeep.Push (node);
      }
      else
      {
        nodesToRemove.Push (node);
      }
    }

    while (nodesToRemove.GetSize ())
    {
      csRef<iDocumentNode> node = nodesToRemove.Pop ();
      rootNode->RemoveNode (node);
    }
  }

  /**
   * Copy the attributes of a node to another node.
   * \param from Source node
   * \param to Destination node
   */
  inline void CloneAttributes (iDocumentNode* from, iDocumentNode* to)
  {
    csRef<iDocumentAttributeIterator> atit = from->GetAttributes ();
    while (atit->HasNext ())
    {
      csRef<iDocumentAttribute> attr = atit->Next ();
      to->SetAttribute (attr->GetName (), attr->GetValue ());
    }
  }

  /**
   * Recursively clone a node with all its attributes and child-nodes.
   * \param from Source root node
   * \param to Destination root node
   */
  inline void CloneNode (iDocumentNode* from, iDocumentNode* to)
  {
    to->SetValue (from->GetValue ());
    csRef<iDocumentNodeIterator> it = from->GetNodes ();
    while (it->HasNext ())
    {
      csRef<iDocumentNode> child = it->Next ();
      csRef<iDocumentNode> child_clone = to->CreateNodeBefore (
        child->GetType (), 0);
      CloneNode (child, child_clone);
    }
    CloneAttributes (from, to);
  }

  /**\name Functors 
   * @{ */

  /**
   * Node comparator. Compares the names of the nodes (case-insensitive).
   */
  struct NodeNameCompare
  {
    bool operator () (iDocumentNode *node1, iDocumentNode *node2) const
    {
      if (node1->GetType () != CS_NODE_ELEMENT) return false;
      if (node2->GetType () != CS_NODE_ELEMENT) return false;

      const char* name1 = node1->GetValue ();
      const char* name2 = node2->GetValue ();
      if (!csStrCaseCmp (name1, name2)) return true;
      return false;
    }
  };

  /**
   * Node comparator. Compares a given attribute between two nodes 
   * (case-insensitive).
   */
  struct NodeAttributeCompare
  {
    NodeAttributeCompare (const char* attributeName)
      : attributeName (attributeName)
    {
    }

    bool operator () (iDocumentNode *node1, iDocumentNode *node2) const
    {
      if (node1->GetType () != CS_NODE_ELEMENT) return false;
      if (node2->GetType () != CS_NODE_ELEMENT) return false;

      csRef<iDocumentAttribute> attribute1 = 
        node1->GetAttribute (attributeName.GetData ());
      csRef<iDocumentAttribute> attribute2 = 
        node2->GetAttribute (attributeName.GetData ());
      if (!attribute1 || !attribute2) return false;

      if (!csStrCaseCmp (attribute1->GetValue (), attribute2->GetValue ())) 
        return true;

      return false;
    }
  private:
    csString attributeName;
  };

  /**
   * Compare (case-sensitive) node value to given.
   */
  struct NodeValueTest
  {
    NodeValueTest (const char* value)
      : value (value)
    {}

    bool operator () (iDocumentNode *node)
    {
      if (!node) return false;

      const char *nodeValue = node->GetValue ();
      return (value == nodeValue);
    }

  private:
    csString value;
  };

  /**
   * Compare (case-sensitive) node attribute to given.
   */
  struct NodeAttributeValueTest
  {
    NodeAttributeValueTest (const char *attribute, const char* value)
      : attribute (attribute), value (value)
    {}

    bool operator () (iDocumentNode *node)
    {
      if (!node) return false;

      const char* attributeValue = node->GetAttributeValue (
		attribute.GetData ());

      return (value == attributeValue);
    }

  private:
    csString attribute;
    csString value;
  };

  /**
   * Check if a regular expression matches(case-insensitive) with the value 
   * of the given attribute.
   */
  struct NodeAttributeRegexpTest
  {
    NodeAttributeRegexpTest (const char *attribute, const char* regexp)
      : attribute (attribute), valueMatcher (regexp)
    {
    }

    bool operator () (iDocumentNode *node)
    {
      if (!node) return false;

      const char* attributeValue = node->GetAttributeValue (
		attribute.GetData ());

      return (valueMatcher.Match (attributeValue, csrxIgnoreCase)
		== csrxNoError);
    }

  private:
    csString attribute;
    csRegExpMatcher valueMatcher;
  };
  /** @} */

  /** 
   * Get a filtering iDocumentNodeIterator. Only nodes matching the filter
   * are returned.
   * Example usage: 
   * \code
   * DocumentHelper::NodeAttributeValueTest test ("name", "Marten");
   * csRef<iDocumentNodeIterator> it = 
   *   FilterDocumentNodeIterator (node->GetNodes(), test);
   * while (it->HasNext ())
   * { ... }
   * \endcode
   */
  template<class T>
  csPtr<iDocumentNodeIterator> FilterDocumentNodeIterator(
    csRef<iDocumentNodeIterator> parent, T filter)
  {
    return new Implementation::FilterDocumentNodeIterator<T>
      (parent, filter);
  }
  
  /**
   * "Flatten" a document node structure into a string, suitable for e.g.
   * hashing.
   */
  CS_CRYSTALSPACE_EXPORT csString FlattenNode (iDocumentNode* node);
  /**
   * "Flatten" a document node structure into a string ignoring child nodes,
   * \sa FlattenNode
   */
  CS_CRYSTALSPACE_EXPORT csString FlattenNodeShallow (iDocumentNode* node);
  
  /**
   * Make a document changeable.
   * Not all documents can be changed in-place. This helper function checks
   * the document \a doc and either returns the original document, if
   * changeable, or a newly created, changeable document from \a docsys
   * with the same contents as \a doc.
   */
  CS_CRYSTALSPACE_EXPORT csPtr<iDocument> MakeChangeable (iDocument* doc,
							  iDocumentSystem* docsys);

  /**
   * Set the contents of a document node.
   * This is the converse of iDocumentNode::GetContentsValue(): if \a node
   * has a child of type #CS_NODE_TEXT, the value of that child is changed
   * to \a contents. If no such node exists one is created.
   * Returns \c false if node couldn't be changed or a child created.
   * (Typically if \a node was not of type #CS_NODE_ELEMENT.)
   */
  CS_CRYSTALSPACE_EXPORT bool SetContentsValue (iDocumentNode* node,
						const char* contents);
} // namespace DocSystem


} //namespace CS

#endif