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
|
/* 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;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.zip.DataFormatException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import HwpDoc.Exception.HwpParseException;
import HwpDoc.Exception.NotImplementedException;
import HwpDoc.Exception.OwpmlParseException;
import HwpDoc.OCFdoc.OwpmlFile;
import HwpDoc.OLEdoc.DirectoryEntry;
import HwpDoc.paragraph.HwpParagraph;
public class HwpxFile {
private static final Logger log = Logger.getLogger(HwpxFile.class.getName());
public String filename;
public OwpmlFile owplmFile;
public HwpFileHeader fileHeader;
public int version;
public HwpDocInfo docInfo;
public List<HwpSection> sections;
// Let's have member that are needed for showing in LibreOffice
public List<DirectoryEntry> directoryBinData;
public List<HwpParagraph> paraList;
public HwpxFile(String filename) throws FileNotFoundException {
this.filename = filename;
owplmFile = new OwpmlFile(this.filename);
fileHeader = new HwpFileHeader();
docInfo = new HwpDocInfo(this);
sections = new ArrayList<HwpSection>();
}
public HwpxFile(File file) throws FileNotFoundException {
owplmFile = new OwpmlFile(file);
this.filename = file.toString();
fileHeader = new HwpFileHeader();
docInfo = new HwpDocInfo(this);
sections = new ArrayList<HwpSection>();
}
public OwpmlFile getOwpmlFile() {
return owplmFile;
}
public List<HwpSection> getSections() {
return sections;
}
public boolean detect() throws HwpDetectException, IOException {
// read CompoundFile structure
try {
owplmFile.open();
if (getFileHeader() == false) {
owplmFile.close();
// throw new CompoundParseException();
}
} catch (ParserConfigurationException | SAXException | DataFormatException e) {
owplmFile.close();
throw new HwpDetectException(ErrCode.INVALID_ZIP_DATA_FORMAT);
} catch (HwpDetectException e) {
owplmFile.close();
throw new HwpDetectException(e.getReason());
}
log.fine("Header parsed");
return true;
}
public void open(IContext context) throws HwpDetectException, IOException, DataFormatException,
ParserConfigurationException, SAXException, OwpmlParseException,
HwpParseException, NotImplementedException {
if (fileHeader.version==null) {
detect();
}
version = Integer.parseInt(fileHeader.version);
if (getDocInfo(version)==false)
throw new OwpmlParseException();
log.fine("DocInfo parsed");
// Contents/SectionX.xml 을 읽는다.
for (String section: owplmFile.getSections()) {
readSection(section, version, context);
}
}
public boolean getFileHeader() throws HwpDetectException, IOException, ParserConfigurationException, SAXException, DataFormatException {
return fileHeader.parse(getDocument("version.xml"));
}
public HwpDocInfo getDocInfo() {
return docInfo;
}
public boolean getDocInfo(int version) throws IOException, DataFormatException, ParserConfigurationException, SAXException, HwpParseException, NotImplementedException {
if (docInfo.readContentHpf(getDocument("Contents/content.hpf"), version)) {
return docInfo.read(getDocument("Contents/header.xml"), version);
} else {
return false;
}
}
public boolean readSection(String name, int version, IContext context) throws IOException, DataFormatException,
ParserConfigurationException, SAXException, NotImplementedException {
Document document = getDocument(name);
HwpSection hwpSection = new HwpSection(this);
hwpSection.read(document, version, context);
sections.add(hwpSection);
return true;
}
public Document getDocument(String entryName) throws IOException, ParserConfigurationException, SAXException, DataFormatException {
InputStream is = owplmFile.getInputStream(entryName);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(is);
}
public void close() throws IOException {
owplmFile.close();
}
public String findBinData(String shortName) {
return owplmFile.getBinData(shortName);
}
public byte[] getBinDataByIDRef(String shortName) throws IOException, DataFormatException {
String entry = owplmFile.getBinData(shortName);
return owplmFile.getBytes(entry);
}
public byte[] getBinDataByEntry(String entry) throws IOException, DataFormatException {
return owplmFile.getBytes(entry);
}
public List<HwpParagraph> getParaList() {
return paraList;
}
public void addParaList(HwpParagraph para) {
if (this.paraList == null)
this.paraList = new ArrayList<HwpParagraph>();
this.paraList.add(para);
}
public static class Rand {
static int random_seed;
public static void srand(int seed) {
random_seed = seed;
}
public static int rand() {
random_seed = (random_seed * 214013 + 2531011) & 0xFFFFFFFF;
return ((random_seed >> 16) & 0x7FFF);
}
}
}
|