File: HwpParagraph.java

package info (click to toggle)
h2orestart 0.7.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,168 kB
  • sloc: java: 21,845; makefile: 36; xml: 19
file content (405 lines) | stat: -rw-r--r-- 16,509 bytes parent folder | download
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* Copyright (C) 2023 ebandal
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>
 */
/* 본 제품은 한글과컴퓨터의 ᄒᆞᆫ글 문서 파일(.hwp) 공개 문서를 참고하여 개발하였습니다.
 * 개방형 워드프로세서 마크업 언어(OWPML) 문서 구조 KS X 6101:2018 문서를 참고하였습니다.
 * 작성자 : 반희수 ebandal@gmail.com  
 * 작성일 : 2022.10
 */
package HwpDoc.paragraph;

import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import HwpDoc.IContext;
import HwpDoc.Exception.HwpParseException;
import HwpDoc.Exception.NotImplementedException;
import HwpDoc.paragraph.Ctrl_Character.CtrlCharType;

public class HwpParagraph {
    private static final Logger log = Logger.getLogger(HwpParagraph.class.getName());
    
    public short            paraShapeID;    // HWPTAG_PARA_HEADER
    public short            paraStyleID;    // HWPTAG_PARA_HEADER
    public byte             breakType;      // HWPTAG_PARA_HEADER
    // public List<CharShape>   charShapes;   // HWPTAG_PARA_CHAR_SHAPE
    public LineSeg          lineSegs;       // HWPTAG_PARA_LINE_SEG
    public List<RangeTag>   rangeTags;      // HWPTAG_PARA_RANGE_TAG
    
    public LinkedList<Ctrl> p;              // HWPTAG_PARA_TEXT + List<Ctrl>  V2
    
    public HwpParagraph() { }

    public HwpParagraph(Node node, int version, IContext context) throws NotImplementedException {
        
        NamedNodeMap attributes = node.getAttributes();
        
        // id값은 처리하지 않는다. List<HwpRecord_Style>에 순차적으로 추가한다.
        // String id = attributes.getNamedItem("id").getNodeValue();
        
        String numStr = attributes.getNamedItem("paraPrIDRef").getNodeValue();
        paraShapeID = (short) Integer.parseInt(numStr);
        
        numStr = attributes.getNamedItem("styleIDRef").getNodeValue();
        paraStyleID = (short) Integer.parseInt(numStr);
        
        switch(attributes.getNamedItem("pageBreak").getNodeValue()) {
        case "0":
            breakType &= 0b11111011;   break;      // 0:구역나누기, 2:다단나누기, 4:쪽 나누기, 8:단 나누기
        case "1":
            breakType |= 0b00000100;   break;
        default:
            if (log.isLoggable(Level.FINE)) {
                throw new NotImplementedException("HwpParagraph");
            }
            break;
        }

        switch(attributes.getNamedItem("columnBreak").getNodeValue()) {
        case "0":
            breakType &= 0b11110111;   break;      // 0:구역나누기, 2:다단나누기, 4:쪽 나누기, 8:단 나누기
        case "1":
            breakType |= 0b00001000;   break;
        default:
            if (log.isLoggable(Level.FINE)) {
                throw new NotImplementedException("HwpParagraph");
            }
            break;
        }

        // attributes.getNamedItem("merged").getNodeValue();
        // attributes.getNamedItem("paraTcId").getNodeValue();
        int charShapeID = 0;

        NodeList nodeList = node.getChildNodes();
        for (int i=0; i<nodeList.getLength(); i++) {
            Node child = nodeList.item(i);
            switch(child.getNodeName()) {
            case "hp:run":
                {
                    NamedNodeMap childAttrs = child.getAttributes();
                    if (childAttrs.getNamedItem("charPrIDRef")!=null) {
                        numStr = childAttrs.getNamedItem("charPrIDRef").getNodeValue();
                        charShapeID = Integer.parseInt(numStr);
                        if (childAttrs.getNamedItem("charTcId")!=null) {
                            numStr = childAttrs.getNamedItem("charTcId").getNodeValue();
                        }
                    }

                    NodeList childNodeList = child.getChildNodes();
                    for (int j=0; j<childNodeList.getLength(); j++) {
                        Node grandChild = childNodeList.item(j);
                        parseHwpParagraph(grandChild, charShapeID, version, context);
                    }
                }
                break;
            case "hp:linesegarray":
                {
                    NodeList childNodeList = child.getChildNodes();
                    for (int j=0; j<childNodeList.getLength(); j++) {
                        Node grandChild = childNodeList.item(j);
                        switch(grandChild.getNodeName()) {
                        case "hp:lineseg":	
                            lineSegs = new LineSeg(grandChild, version);
                            break;
                        }
                    }
                }
                break;
            case "#text":
                break;
            default:
                if (log.isLoggable(Level.FINE)) {
                    throw new NotImplementedException("HwpParagraph");
                }
                break;
            }
        }

        // hp:t 없이 paragraph 끝난다면 
        if (p==null) {
            p = new LinkedList<Ctrl>();
            p.add(new Ctrl_Character("   _", CtrlCharType.PARAGRAPH_BREAK, charShapeID));
        }
        // 마지막에 PARA_BREAK로 끝나지 않았다면 PARA_BREAK를 삽입
        if (p.size()==0 || !(p.getLast() instanceof Ctrl_Character)) {
            p.add(new Ctrl_Character("   _", CtrlCharType.PARAGRAPH_BREAK, charShapeID));
        }
    }

    private void parseHwpParagraph(Node node, int charShapeId, int version, IContext context) throws NotImplementedException {

        if (p == null) {
            p = new LinkedList<Ctrl>();
        }

        String numStr;
        Ctrl ctrl;

        switch(node.getNodeName()) {
        case "hp:secPr":
            {
                ctrl = new Ctrl_SectionDef("dces", node, version, context);
                p.add(ctrl);
            }
            break;
        case "hp:ctrl":
            {
                NodeList nodeList = node.getChildNodes();
                for (int j=0; j<nodeList.getLength(); j++) {
                    Node child = nodeList.item(j);
                    ctrl = Ctrl.getCtrl(child, version, context);
                    p.add(ctrl);
                }
            }
            break;
        case "hp:t":
            {
                NamedNodeMap attrs = node.getAttributes();
                
                if (attrs.getNamedItem("charPrIDRef")!=null) {
                    CharShape charShape = new CharShape();
                    numStr = attrs.getNamedItem("charPrIDRef").getNodeValue();
                    charShape.charShapeID = Integer.parseInt(numStr);
                }
                
                NodeList nodeList = node.getChildNodes();
                for (int j=0; j<nodeList.getLength(); j++) {
                    Node child = nodeList.item(j);
                    switch(child.getNodeName()) {
                    case "#text":
                        p.add(new ParaText("____", child.getNodeValue(), 0, charShapeId));
                        break;
                    case "hp:lineBreak":
                        p.add(new Ctrl_Character("   _", CtrlCharType.LINE_BREAK));
                        break;
                    case "hp:hyphen":
                        p.add(new Ctrl_Character("   _", CtrlCharType.HARD_HYPHEN));
                        break;
                    case "hp:nbSpace":
                    case "hp:fwSpace":
                        p.add(new Ctrl_Character("   _", CtrlCharType.HARD_SPACE));
                        break;
                    case "hp:tab":
                        p.add(new ParaText("____", "\t", 0));
                        break;
                    case "hp:markpenBegin":    // 134 page
                    case "hp:markpenEnd":
                        break;
                    case "hp:titleMark":
                    case "hp:insertBegin":
                    case "hp:insertEnd":
                    case "hp:deleteBegin":
                    case "hp:deleteEnd":
                        if (log.isLoggable(Level.FINE)) {
                            throw new NotImplementedException("hp:t");
                        }
                        break;
                    }
                }
            }
            break;
        case "hp:tbl":
            ctrl = new Ctrl_Table(" lbt" , node, version, context);
            p.add(ctrl);
            break;
        case "hp:pic":
            ctrl = new Ctrl_ShapePic("cip$", node, version, context);
            p.add(ctrl);
            break;
        case "hp:container":
            ctrl = new Ctrl_Container("noc$", node, version, context);
            p.add(ctrl);
            break;
        case "hp:ole":
            ctrl = new Ctrl_ShapeOle("elo$", node, version, context);
            p.add(ctrl);
            break;
        case "hp:equation":
            ctrl = new Ctrl_EqEdit("deqe", node, version, context);
            p.add(ctrl);
            break;
        case "hp:line":
            ctrl = new Ctrl_ShapeLine("nil$", node, version, context);
            p.add(ctrl);
            break;
        case "hp:rect":
            ctrl = new Ctrl_ShapeRect("cer$", node, version, context);
            p.add(ctrl);
            break;
        case "hp:ellipse":
            ctrl = new Ctrl_ShapeEllipse("lle$", node, version, context);
            p.add(ctrl);
            break;
        case "hp:arc":
            ctrl = new Ctrl_ShapeArc("cra$", node, version, context);
            p.add(ctrl);
            break;
        case "hp:polygon":
            ctrl = new Ctrl_ShapePolygon("lop$", node, version, context);
            p.add(ctrl);
            break;
        case "hp:curve":
            ctrl = new Ctrl_ShapeCurve("ruc$", node, version, context);
            p.add(ctrl);
            break;
        case "hp:connectLine":
            ctrl = new Ctrl_ShapeConnectLine("loc$", node, version, context);
            p.add(ctrl);
            break;
        case "hp:textart":
            ctrl = new Ctrl_ShapeTextArt("tat$", node, version, context);
            p.add(ctrl);
            break;
        case "hp:video":
            ctrl = new Ctrl_ShapeVideo("div$", node, version, context);
            p.add(ctrl);
            break;
        case "hp:compose":
        case "hp:dutmal":
        case "hp:btn":
        case "hp:radioBtn":
        case "hp:checkBtn":
        case "hp:comboBox":
        case "hp:edit":
        case "hp:listBox":
        case "hp:scrollBar":
            break;
        case "hp:switch":
            {
                NodeList nodeList = node.getChildNodes();
                for (int j=0; j<nodeList.getLength(); j++) {
                    Node child = nodeList.item(j);
                    switch(child.getNodeName()) {
                    case "hp:case":
                        break;
                    case "hp:default":
                        break;
                    }
                }
            }
            break;
        case "#text":
            break;
        default:
            if (log.isLoggable(Level.FINE)) {
                throw new NotImplementedException("parseHwpPargraph");
            }
            break;
        }
    }

    public static HwpParagraph parse(int tagNum, int level, int size, byte[] buf, int off, int version) throws HwpParseException {
        int offset = off;
        
        HwpParagraph para = new HwpParagraph();
        
        int nchars          = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0x00FF0000 | buf[offset+1]<<8&0x0000FF00 | buf[offset]&0x000000FF;
        offset += 4;
        if ((nchars&0x80000000)!=0) {
            nchars &= 0x7fffffff;
        }
        int controlMask     = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0x00FF0000 | buf[offset+1]<<8&0x0000FF00 | buf[offset]&0x000000FF;
        offset += 4;
        para.paraShapeID    = (short) (buf[offset+1]<<8&0xFF00 | buf[offset]&0x00FF);
        offset += 2;
        para.paraStyleID    = (short) (buf[offset++]&0x00FF);
        para.breakType      = buf[offset++];
        short nCharShapeInfo= (short) (buf[offset+1]<<8&0xFF00 | buf[offset]&0x00FF);
        offset += 2;
        short nRangeTags    = (short) (buf[offset+1]<<8&0xFF00 | buf[offset]&0x00FF);
        offset += 2;
        short nLineSeg      = (short) (buf[offset+1]<<8&0xFF00 | buf[offset]&0x00FF);
        offset += 2;
        int paraInstanceID  = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0x00FF0000 | buf[offset+1]<<8&0x0000FF00 | buf[offset]&0x000000FF;
        offset += 4;
        if (version>=5032 && offset-off<size) {
            short changeTrackingMerge= (short) (buf[offset+1]<<8&0xFF00 | buf[offset]&0x00FF);
            offset += 2;
        }
        log.fine("                                                  "
                +"instance="+String.format("0x%X", paraInstanceID)
                +",문단모양ID="+para.paraShapeID
                +",스타일ID="+para.paraStyleID
                +",단나누기종류="+para.breakType
                +",nchars="+nchars
                +",nLineSeg="+nLineSeg
                +",controlMask="+controlMask
                +",nCharShapeInfo="+nCharShapeInfo
                +",nRangeTags="+nRangeTags
                +",paraInstanceID="+paraInstanceID
                );
        
        if (offset-off-size != 0 && offset-off!=24) {
            log.severe("[TAG]=" + tagNum + ", size=" + size + ", but currentSize=" + (offset-off));
            // throw new HwpParseException();
        }
        
        return para;
    }
	
    public static int parse(HwpParagraph para, int size, byte[] buf, int off, int version) throws HwpParseException {
        int offset = off;
        
        int nchars          = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0x00FF0000 | buf[offset+1]<<8&0x0000FF00 | buf[offset]&0x000000FF;
        offset += 4;
        if ((nchars&0x80000000)!=0) {
            nchars &= 0x7fffffff;
        }
        int controlMask     = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0x00FF0000 | buf[offset+1]<<8&0x0000FF00 | buf[offset]&0x000000FF;
        offset += 4;
        para.paraShapeID    = (short) (buf[offset+1]<<8&0xFF00 | buf[offset]&0x00FF);
        offset += 2;
        para.paraStyleID    = (short) (buf[offset++]&0x00FF);
        para.breakType      = buf[offset++];
        short nCharShapeInfo= (short) (buf[offset+1]<<8&0xFF00 | buf[offset]&0x00FF);
        offset += 2;
        short nRangeTags    = (short) (buf[offset+1]<<8&0xFF00 | buf[offset]&0x00FF);
        offset += 2;
        short nLineSegs     = (short) (buf[offset+1]<<8&0xFF00 | buf[offset]&0x00FF);
        offset += 2;
        int paraInstanceID  = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0x00FF0000 | buf[offset+1]<<8&0x0000FF00 | buf[offset]&0x000000FF;
        offset += 4;
        if (version>=5032 && offset-off<size) {
            short changeTrackingMerge= (short) (buf[offset+1]<<8&0xFF00 | buf[offset]&0x00FF);
            offset += 2;
        }
        log.fine("                                                  "
                +"instance="+String.format("0x%X", paraInstanceID)
                +",문단모양ID="+para.paraShapeID
                +",스타일ID="+para.paraStyleID
                +",단나누기종류="+para.breakType
                +",nchars="+nchars
                +",nLineSeg="+nLineSegs
                +",controlMask="+controlMask
                +",nCharShapeInfo="+nCharShapeInfo
                +",nRangeTags="+nRangeTags
                +",paraInstanceID="+paraInstanceID
                );
        
        if (offset-off-size != 0 && offset-off!=24) {
            log.severe("[PARA] size=" + size + ", but currentSize=" + (offset-off));
            // throw new HwpParseException();
        }
        
        return size;
    }
}