• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / sitemesh2 / 59

22 Mar 2026 02:30AM UTC coverage: 40.347%. Remained the same
59

push

github

hazendaz
[mvn] Update maven wrapper

698 of 1891 branches covered (36.91%)

Branch coverage included in aggregate %.

1555 of 3693 relevant lines covered (42.11%)

0.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.16
/src/main/java/com/opensymphony/module/sitemesh/SitemeshBufferFragment.java
1
/*
2
 * SPDX-License-Identifier: Apache-2.0
3
 * Copyright 2011-2026 Hazendaz
4
 */
5
package com.opensymphony.module.sitemesh;
6

7
import com.opensymphony.module.sitemesh.html.util.StringSitemeshBuffer;
8

9
import java.io.IOException;
10
import java.io.StringWriter;
11
import java.io.Writer;
12
import java.util.Map;
13
import java.util.TreeMap;
14

15
/**
16
 * A fragment of a sitemesh buffer. This includes a start and a length, and may contain a list of deleted sections of
17
 * the buffer.
18
 */
19
public class SitemeshBufferFragment {
20

21
    /** The buffer. */
22
    private final SitemeshBuffer buffer;
23

24
    /** The start. */
25
    private final int start;
26

27
    /** The length. */
28
    private final int length;
29

30
    /** The deletions. */
31
    private final TreeMap<Integer, Integer> deletions;
32

33
    /**
34
     * Instantiates a new sitemesh buffer fragment.
35
     *
36
     * @param buffer
37
     *            the buffer
38
     * @param start
39
     *            the start
40
     * @param length
41
     *            the length
42
     */
43
    public SitemeshBufferFragment(SitemeshBuffer buffer, int start, int length) {
44
        this(buffer, start, length, new TreeMap<Integer, Integer>());
1✔
45
    }
1✔
46

47
    /**
48
     * Create a sitemesh buffer fragment.
49
     *
50
     * @param buffer
51
     *            The buffer that this is a fragment of
52
     * @param start
53
     *            The start of the fragment
54
     * @param length
55
     *            The length of the fragment
56
     * @param deletions
57
     *            Deleted parts of the fragment, as a map of positions to the length to be deleted.
58
     */
59
    public SitemeshBufferFragment(SitemeshBuffer buffer, int start, int length, TreeMap<Integer, Integer> deletions) {
1✔
60
        this.buffer = buffer;
1✔
61
        this.start = start;
1✔
62
        this.length = length;
1✔
63
        this.deletions = deletions;
1✔
64
    }
1✔
65

66
    /**
67
     * Write the fragment to the given writer.
68
     *
69
     * @param writer
70
     *            The writer to write the fragment to
71
     *
72
     * @throws IOException
73
     *             If an error occured
74
     */
75
    public void writeTo(Writer writer) throws IOException {
76
        int pos = start;
1✔
77
        for (Map.Entry<Integer, Integer> delete : deletions.entrySet()) {
1✔
78
            int deletePos = delete.getKey();
1✔
79
            if (deletePos >= pos) {
1✔
80
                buffer.writeTo(writer, pos, deletePos - pos);
1✔
81
            }
82
            pos = Math.max(deletePos + delete.getValue(), start);
1✔
83
        }
1✔
84
        int remain = start + length - pos;
1✔
85
        if (remain >= 0) {
1!
86
            buffer.writeTo(writer, pos, remain);
1✔
87
        }
88
    }
1✔
89

90
    /**
91
     * Get the total length of the fragment, taking deletions and chained buffers of the buffer.
92
     *
93
     * @return The total length of the fragment
94
     */
95
    public int getTotalLength() {
96
        int total = 0;
1✔
97
        int pos = start;
1✔
98
        for (Map.Entry<Integer, Integer> delete : deletions.entrySet()) {
1!
99
            int deletePos = delete.getKey();
×
100
            if (deletePos > pos) {
×
101
                total += buffer.getTotalLength(pos, deletePos - pos);
×
102
            }
103
            pos = deletePos + delete.getValue();
×
104
        }
×
105
        int remain = start + length - pos;
1✔
106
        if (remain > 0) {
1!
107
            total += buffer.getTotalLength(pos, remain);
1✔
108
        }
109
        return total;
1✔
110
    }
111

112
    /**
113
     * Gets the string content.
114
     *
115
     * @return the string content
116
     */
117
    public String getStringContent() {
118
        StringWriter writer = new StringWriter();
1✔
119
        try {
120
            writeTo(writer);
1✔
121
        } catch (IOException e) {
×
122
            throw new RuntimeException("Exception writing to buffer", e);
×
123
        }
1✔
124
        return writer.toString();
1✔
125
    }
126

127
    @Override
128
    public String toString() {
129
        return "SitemeshBufferFragment{" +
×
130
        // Here we generate our own ID, because if the underlying writer is a CharArrayWriter, we'll end up
131
        // with its entire contents, which we don't really want in this method.
132
                "buffer=" + buffer.getClass().getName() + "@" + Integer.toHexString(hashCode()) + ", start=" + start
×
133
                + ", length=" + length + ", deletions=" + deletions + '}';
134
    }
135

136
    /**
137
     * Gets the start.
138
     *
139
     * @return the start
140
     */
141
    public int getStart() {
142
        return start;
×
143
    }
144

145
    /**
146
     * Gets the length.
147
     *
148
     * @return the length
149
     */
150
    public int getLength() {
151
        return length;
×
152
    }
153

154
    /**
155
     * Builder.
156
     *
157
     * @return the builder
158
     */
159
    public static Builder builder() {
160
        return new Builder();
1✔
161
    }
162

163
    /**
164
     * Builder.
165
     *
166
     * @param fragment
167
     *            the fragment
168
     *
169
     * @return the builder
170
     */
171
    public static Builder builder(SitemeshBufferFragment fragment) {
172
        return new Builder(fragment);
×
173
    }
174

175
    /**
176
     * A builder for fragments.
177
     */
178
    public static class Builder {
179

180
        /** The buffer. */
181
        private DefaultSitemeshBuffer.Builder buffer;
182

183
        /** The start. */
184
        private int start;
185

186
        /** The length. */
187
        private int length;
188

189
        /** The deletions. */
190
        private final TreeMap<Integer, Integer> deletions;
191

192
        /** The start delete. */
193
        private Integer startDelete;
194

195
        /**
196
         * Instantiates a new builder.
197
         */
198
        private Builder() {
1✔
199
            this.deletions = new TreeMap<>();
1✔
200
        }
1✔
201

202
        /**
203
         * Instantiates a new builder.
204
         *
205
         * @param fragment
206
         *            the fragment
207
         */
208
        private Builder(SitemeshBufferFragment fragment) {
×
209
            this.buffer = DefaultSitemeshBuffer.builder(fragment.buffer);
×
210
            this.start = fragment.start;
×
211
            this.length = fragment.length;
×
212
            this.deletions = new TreeMap<>(fragment.deletions);
×
213
        }
×
214

215
        /**
216
         * Sets the start.
217
         *
218
         * @param start
219
         *            the start
220
         *
221
         * @return the builder
222
         */
223
        public Builder setStart(int start) {
224
            this.start = start;
1✔
225
            return this;
1✔
226
        }
227

228
        /**
229
         * Sets the length.
230
         *
231
         * @param length
232
         *            the length
233
         *
234
         * @return the builder
235
         */
236
        public Builder setLength(int length) {
237
            this.length = length;
1✔
238
            return this;
1✔
239
        }
240

241
        /**
242
         * Delete length characters from pos in this buffer fragment.
243
         *
244
         * @param pos
245
         *            The position to delete from
246
         * @param length
247
         *            The number of characters to delete
248
         *
249
         * @return The builder
250
         */
251
        public Builder delete(int pos, int length) {
252
            this.deletions.put(pos, length);
1✔
253
            return this;
1✔
254
        }
255

256
        /**
257
         * Mark the start of the fragment.
258
         *
259
         * @param pos
260
         *            The start of the fragment
261
         *
262
         * @return The builder
263
         */
264
        public Builder markStart(int pos) {
265
            this.start = pos;
1✔
266
            this.length = 0;
1✔
267
            return this;
1✔
268
        }
269

270
        /**
271
         * End the fragment.
272
         *
273
         * @param pos
274
         *            The position of the end of the fragment
275
         *
276
         * @return The builder
277
         */
278
        public Builder end(int pos) {
279
            this.length = pos - this.start;
1✔
280
            return this;
1✔
281
        }
282

283
        /**
284
         * Mark the start of a deletion.
285
         *
286
         * @param pos
287
         *            The position to start deleting from
288
         *
289
         * @return The builder
290
         *
291
         * @throws IllegalStateException
292
         *             If markStartDelete() has already been called and endDelete() hasn't been called
293
         */
294
        public Builder markStartDelete(int pos) {
295
            if (startDelete != null) {
1!
296
                throw new IllegalStateException("Can't nested delete...");
×
297
            }
298
            startDelete = pos;
1✔
299
            return this;
1✔
300
        }
301

302
        /**
303
         * End the current deletion.
304
         *
305
         * @param pos
306
         *            The position to delete to
307
         *
308
         * @return The builder
309
         *
310
         * @throws IllegalStateException
311
         *             If markStartDelete() hasn't been called
312
         */
313
        public Builder endDelete(int pos) {
314
            if (startDelete == null) {
1!
315
                throw new IllegalStateException("Ending delete with no start delete...");
×
316
            }
317
            delete(startDelete, pos - startDelete);
1✔
318
            startDelete = null;
1✔
319
            return this;
1✔
320
        }
321

322
        /**
323
         * Insert the given fragment to the given position.
324
         *
325
         * @param position
326
         *            The position to insert the fragment to
327
         * @param fragment
328
         *            The fragment to insert
329
         *
330
         * @return The builder
331
         */
332
        public Builder insert(int position, SitemeshBufferFragment fragment) {
333
            buffer.insert(position, fragment);
1✔
334
            return this;
1✔
335
        }
336

337
        /**
338
         * Insert the given string fragment to the given position.
339
         *
340
         * @param position
341
         *            The position to insert at
342
         * @param fragment
343
         *            The fragment to insert
344
         *
345
         * @return The builder
346
         */
347
        public Builder insert(int position, String fragment) {
348
            buffer.insert(position, StringSitemeshBuffer.createBufferFragment(fragment));
1✔
349
            return this;
1✔
350
        }
351

352
        /**
353
         * Set the buffer. This resets both start and length to be that of the buffer.
354
         *
355
         * @param sitemeshBuffer
356
         *            The buffer to set.
357
         *
358
         * @return The builder
359
         */
360
        public Builder setBuffer(SitemeshBuffer sitemeshBuffer) {
361
            this.buffer = DefaultSitemeshBuffer.builder(sitemeshBuffer);
1✔
362
            this.start = 0;
1✔
363
            this.length = sitemeshBuffer.getBufferLength();
1✔
364
            return this;
1✔
365
        }
366

367
        /**
368
         * Build the fragment.
369
         *
370
         * @return The built fragment
371
         */
372
        public SitemeshBufferFragment build() {
373
            return new SitemeshBufferFragment(buffer.build(), start, length, deletions);
1✔
374
        }
375
    }
376
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc