]> err.no Git - sope/blob - sope-appserver/WEExtensions/WETreeMatrixElement.m
Add libxml2-dev to libsope-xml4.7-dev deps
[sope] / sope-appserver / WEExtensions / WETreeMatrixElement.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "WETreeMatrixElement.h"
23 #include "common.h"
24
25 @implementation _WETreeMatrixElement
26
27 static Class StrClass = Nil;
28
29 + (void)initialize {
30   StrClass = [NSString class];
31 }
32
33 - (id)initWithElement:(_WETreeMatrixElement *)_element {
34   if ((self = [super init])) {
35     int j;
36     
37     for (j = 0; j < MAX_TREE_DEPTH; j++)
38       self->elements[j] = 0;
39     
40     if (_element) {
41       int i;
42       
43       self->depth = _element->depth + 1;
44       for (i = 0; i < self->depth; i++) {
45         self->leaf         = _element->leaf;
46         self->elements[i]  = [_element->elements[i] retain];
47         self->indexPath[i] = _element->indexPath[i];
48         self->itemPath[i]  = [_element->itemPath[i] retain];
49       }
50     }
51     else
52       self->depth = 0;
53   }
54   return self;  
55 }
56
57 - (id)init {
58   return [self initWithElement:nil];
59 }
60
61 - (void)dealloc {
62   int i;
63
64   for (i = 0; i < self->depth; i++) {
65     [self->itemPath[i] release];
66     [self->elements[i] release];
67   }
68   [super dealloc];
69 }
70
71 /* accessors */
72   
73 - (void)setElement:(NSString *)_element {
74   ASSIGN(self->elements[self->depth], _element);
75 }
76 - (NSString *)elementAtIndex:(int)_index {
77   return self->elements[_index];
78 }
79
80 - (void)setItem:(id)_item {
81   ASSIGN(self->itemPath[self->depth], _item);
82 }
83 - (id)item {
84   return self->itemPath[self->depth-1];
85 }
86
87 - (void)setIndex:(int)_index {
88   self->indexPath[self->depth] = _index;
89 }
90 - (int)index {
91   return self->indexPath[self->depth-1];
92 }
93
94 - (void)setLeaf:(NSString *)_leaf {
95   ASSIGN(self->leaf, _leaf);
96 }
97 - (NSString *)leaf {
98   return self->leaf;
99 }
100
101 - (void)setColspan:(int)_colspan {
102   self->colspan = _colspan;
103 }
104
105 - (NSString *)colspanAsString {
106   switch (self->colspan) {
107   case 0: return @"0";
108   case 1: return @"1";
109   case 2: return @"2";
110   case 3: return @"3";
111   default: {
112     char buf[8];
113     sprintf(buf, "%d", self->colspan);
114     return [StrClass stringWithCString:buf];
115   }
116   }
117 }
118
119 - (int)depth {
120   return self->depth;
121 }
122
123 - (NSArray *)currentPath {
124   NSMutableArray *result;
125   int            i;
126
127   result = [NSMutableArray arrayWithCapacity:self->depth];
128   for (i = 0; i < self->depth+1; i++) {
129     if (self->itemPath[i] == nil)
130       break;
131     [result addObject:self->itemPath[i]];
132   }
133   return (NSArray *)result;
134 }
135
136 - (NSString *)elementID {
137   // TODO: improve performance
138   NSMutableArray *tmp;
139   NSString     *s;
140   unsigned int i;
141   
142   tmp = [[NSMutableArray alloc] initWithCapacity:self->depth];
143   
144   for (i = 0; i < self->depth; i++) {
145     char buf[8];
146     
147     sprintf(buf, "%d", self->indexPath[i]);
148     s = [[StrClass alloc] initWithCString:buf];
149     [tmp addObject:s];
150     [s release];
151   }
152   s = [tmp componentsJoinedByString:@"."];
153   [tmp release];
154   return s;
155 }
156
157 @end /* _WETreeMatrixElement */