File: WriterContext.java

package info (click to toggle)
h2orestart 0.7.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,168 kB
  • sloc: java: 21,845; makefile: 36; xml: 19
file content (444 lines) | stat: -rw-r--r-- 15,017 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* 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 soffice;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.zip.DataFormatException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.sun.star.frame.XDesktop;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.text.XText;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.uno.XComponentContext;

import HwpDoc.HanType;
import HwpDoc.HwpDetectException;
import HwpDoc.HwpDocInfo;
import HwpDoc.HwpFile;
import HwpDoc.HwpSection;
import HwpDoc.HwpxFile;
import HwpDoc.IContext;
import HwpDoc.Exception.CompoundDetectException;
import HwpDoc.Exception.CompoundParseException;
import HwpDoc.Exception.HwpParseException;
import HwpDoc.Exception.NotImplementedException;
import HwpDoc.Exception.OwpmlParseException;
import HwpDoc.HwpElement.HwpRecord;
import HwpDoc.HwpElement.HwpRecord_BinData;
import HwpDoc.HwpElement.HwpRecord_BorderFill;
import HwpDoc.HwpElement.HwpRecord_Bullet;
import HwpDoc.HwpElement.HwpRecord_CharShape;
import HwpDoc.HwpElement.HwpRecord_FaceName;
import HwpDoc.HwpElement.HwpRecord_Numbering;
import HwpDoc.HwpElement.HwpRecord_ParaShape;
import HwpDoc.HwpElement.HwpRecord_Style;
import HwpDoc.HwpElement.HwpRecord_TabDef;
import HwpDoc.HwpElement.HwpRecord_BinData.Type;

public class WriterContext implements IContext {
    private static final Logger log = Logger.getLogger(WriterContext.class.getName());

    private static HanType hType;
    public static HwpFile hwp = null;
    public static HwpxFile hwpx = null;
    public static int version;
    public static Set<String> fontNameSet = new HashSet<String>();

    public XDesktop mDesktop = null;
    public XComponentContext mContext = null;
    public XMultiComponentFactory mMCF = null;
    public XMultiServiceFactory mMSF = null;
    public XTextDocument mMyDocument = null;
    public XText mText = null;
    public XTextCursor mTextCursor = null;
    public Path userHomeDir = null;

    public WriterContext() {
    }

    public List<HwpSection> getSections() throws HwpDetectException {
        List<HwpSection> sections = null;
        switch (hType) {
        case HWP:
            sections = hwp.getSections();
            break;
        case HWPX:
            sections = hwpx.getSections();
            break;
        case NONE:
            throw new HwpDetectException();
        }
        return sections;
    }

    public static String detectHancom(File file) {
        String detectingType = null;

        try {
            HwpxFile hwpxTemp = new HwpxFile(file);
            hwpxTemp.detect();
            detectingType = "HWPX";
            hwpxTemp.close();
            log.info("file detected as HWPX");
        } catch (IOException | HwpDetectException e1) {
            log.info("file detected not HWPX");

            try {
                HwpFile hwpTemp = new HwpFile(file);
                hwpTemp.detect();
                detectingType = "HWP";
                hwpTemp.close();
                log.info("file detected as HWP");
            } catch (IOException | HwpDetectException e2) {
                log.info("file detected neither HWPX nor HWP");
            }
        }

        return detectingType;
    }

    public void detect() throws HwpDetectException, CompoundDetectException, NotImplementedException, IOException,
                                CompoundParseException, ParserConfigurationException, SAXException, DataFormatException {
        switch (hType) {
        case HWP:
            hwp.detect();
            break;
        case HWPX:
            hwpx.detect();
            break;
        case NONE:
            throw new HwpDetectException();
        }
    }

    public void open(String inputFile, String hanTypeStr) throws HwpDetectException, CompoundDetectException,
                                                                IOException, DataFormatException, HwpParseException, 
                                                                NotImplementedException, CompoundParseException, ParserConfigurationException,
                                                                SAXException, OwpmlParseException {
        switch (hanTypeStr) {
        case "HWP":
            hType = HanType.HWP;
            hwp = new HwpFile(inputFile);
            hwp.open();
            break;
        case "HWPX":
            hType = HanType.HWPX;
            hwpx = new HwpxFile(inputFile);
            hwpx.open(this);
            break;
        default:
            throw new HwpDetectException();
        }
    }

    public void open(File inputFile, String hanTypeStr) throws HwpDetectException, CompoundDetectException,
                                                                 IOException, DataFormatException, HwpParseException, 
                                                                 NotImplementedException, CompoundParseException, ParserConfigurationException,
                                                                 SAXException, OwpmlParseException {
        switch (hanTypeStr) {
        case "HWP":
            hType = HanType.HWP;
            hwp = new HwpFile(inputFile);
            hwp.open();
            break;
        case "HWPX":
            hType = HanType.HWPX;
            hwpx = new HwpxFile(inputFile);
            hwpx.open(this);
            break;
        default:
            throw new HwpDetectException();
        }
    }

    public void close() throws IOException, HwpDetectException {
        if (hType != null) {
            switch (hType) {
            case HWP:
                hwp.close();
                break;
            case HWPX:
                hwpx.close();
                break;
            case NONE:
                throw new HwpDetectException();
            }
        }
        fontNameSet.clear();
    }

    public HwpDocInfo getDocInfo() {
        HwpDocInfo docInfo = null;
        switch (hType) {
        case HWP:
            docInfo = hwp.getDocInfo();
            break;
        case HWPX:
            docInfo = hwpx.getDocInfo();
            break;
        }
        return docInfo;
    }

    public static HwpRecord_BorderFill getBorderFill(short id) {
        HwpDocInfo docInfo = null;
        switch (hType) {
        case HWP:
            docInfo = hwp.getDocInfo();
            break;
        case HWPX:
            docInfo = hwpx.getDocInfo();
            break;
        }

        return (id > 0 ? (HwpRecord_BorderFill) docInfo.borderFillList.get(id - 1) : null);
    }

    public HwpRecord_ParaShape getParaShape(int id) {
        HwpDocInfo docInfo = null;
        switch (hType) {
        case HWP:
            docInfo = hwp.getDocInfo();
            break;
        case HWPX:
            docInfo = hwpx.getDocInfo();
            break;
        }
        if (id >= 0 && id < docInfo.paraShapeList.size()) {
            return (HwpRecord_ParaShape) docInfo.paraShapeList.get(id);
        } else {
            return null;
        }
    }

    public HwpRecord_Style getParaStyle(short id) {
        HwpDocInfo docInfo = null;
        switch (hType) {
        case HWP:
            docInfo = hwp.getDocInfo();
            break;
        case HWPX:
            docInfo = hwpx.getDocInfo();
            break;
        }

        return (HwpRecord_Style) docInfo.styleList.get(id);
    }

    public HwpRecord_CharShape getCharShape(int id) {
        HwpDocInfo docInfo = null;
        switch (hType) {
        case HWP:
            docInfo = hwp.getDocInfo();
            break;
        case HWPX:
            docInfo = hwpx.getDocInfo();
            break;
        }
        if (id >= 0 && id < docInfo.charShapeList.size()) {
            return (HwpRecord_CharShape) docInfo.charShapeList.get(id);
        } else {
            return null;
        }
    }

    public HwpRecord_Numbering getNumbering(short id) {
        HwpDocInfo docInfo = null;
        switch (hType) {
        case HWP:
            docInfo = hwp.getDocInfo();
            break;
        case HWPX:
            docInfo = hwpx.getDocInfo();
            break;
        }

        return (HwpRecord_Numbering) docInfo.numberingList.get(id);
    }

    public HwpRecord_Bullet getBullet(short id) {
        HwpDocInfo docInfo = null;
        switch (hType) {
        case HWP:
            docInfo = hwp.getDocInfo();
            break;
        case HWPX:
            docInfo = hwpx.getDocInfo();
            break;
        }

        return (HwpRecord_Bullet) docInfo.bulletList.get(id - 1);
    }

    public String getBinFilename(String id) {
        HwpRecord_BinData binData = null;
        String retString = "";
        HwpDocInfo docInfo = null;
        switch (hType) {
        case HWP:
            docInfo = hwp.getDocInfo();
            ArrayList<String> keyList = new ArrayList<String>(docInfo.binDataList.keySet());
            String key = keyList.get(Integer.parseInt(id));
            binData = (HwpRecord_BinData) docInfo.binDataList.get(key);
            break;
        case HWPX:
            docInfo = hwpx.getDocInfo();
            binData = (HwpRecord_BinData) docInfo.binDataList.get(id);
            break;
        }

        retString = binData.aPath;
        /*
         * String compoundFileName = String.format("BIN%04X.%s", binData.binDataID,
         * binData.format); try { retString = hwp.saveChildEntry(getWorkingFolder(),
         * compoundFileName, binData.compressed); } catch (IOException e) {
         * e.printStackTrace(); }
         */
        return retString;
    }

    public byte[] getBinBytes(String id) {
        byte[] imageBytes = null;
        HwpDocInfo docInfo = null;
        switch (hType) {
        case HWP: {
            docInfo = hwp.getDocInfo();
            ArrayList<String> keyList = new ArrayList<String>(docInfo.binDataList.keySet());
            String key = keyList.get(Integer.parseInt(id));
            HwpRecord_BinData binData = (HwpRecord_BinData) docInfo.binDataList.get(key);
            if (binData != null) {
                if (binData.type == Type.LINK) {
                    File file = new File(binData.aPath);
                    try {
                        imageBytes = Files.readAllBytes(file.toPath());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    String compoundFileName = String.format("BIN%04X.%s", binData.binDataID, binData.format);
                    try {
                        imageBytes = hwp.getChildBytes(compoundFileName, binData.compressed);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
            break;
        case HWPX: {
            docInfo = hwpx.getDocInfo();
            HwpRecord_BinData binData = (HwpRecord_BinData) docInfo.binDataList.get(id);
            if (binData != null) {
                try {
                    String binShortName = binData.aPath.replaceAll("BinData/(.*)\\..*", "$1");
                    imageBytes = hwpx.getBinDataByIDRef(binShortName);
                } catch (IOException | DataFormatException e) {
                    e.printStackTrace();
                }
            }
        }
            break;
        }

        return imageBytes;
    }

    public String getBinFormat(String id) {
        HwpRecord_BinData binData = null;
        HwpDocInfo docInfo = null;
        switch (hType) {
        case HWP: {
            docInfo = hwp.getDocInfo();
            ArrayList<String> keyList = new ArrayList<String>(docInfo.binDataList.keySet());
            String key = keyList.get(Integer.parseInt(id));
            binData = (HwpRecord_BinData) docInfo.binDataList.get(key);
        }
            break;
        case HWPX: {
            docInfo = hwpx.getDocInfo();
            binData = (HwpRecord_BinData) docInfo.binDataList.get(id);
        }
            break;
        }

        return binData==null ? null : binData.format;
    }

    public static HwpRecord_TabDef getTabDef(short id) {
        HwpDocInfo docInfo = null;
        switch (hType) {
        case HWP:
            docInfo = hwp.getDocInfo();
            break;
        case HWPX:
            docInfo = hwpx.getDocInfo();
            break;
        }
        return (HwpRecord_TabDef) docInfo.tabDefList.get(id);
    }
    
    public static List<HwpRecord_FaceName> getFontNames() {
        HwpDocInfo docInfo = null;
        switch (hType) {
        case HWP:
            docInfo = hwp.getDocInfo();
            break;
        case HWPX:
            docInfo = hwpx.getDocInfo();
            break;
        }
        return docInfo.faceNameList.stream().map(r -> (HwpRecord_FaceName)r).collect(Collectors.toList());
    }

    public static void setFontNameLineSpaceAlpha(String faceName, double fontLineSpaceAlpha) {
        HwpDocInfo docInfo = null;
        switch (hType) {
        case HWP:
            docInfo = hwp.getDocInfo();
            break;
        case HWPX:
            docInfo = hwpx.getDocInfo();
            break;
        }
        docInfo.setFontNameLineSpaceAlpha(faceName, fontLineSpaceAlpha);
    }
    
    @Override
    public HwpxFile getHwpx() {
    	return hwpx;
    }

}