From bd1a1d56cbcf366f6035b48bc181689021196caa Mon Sep 17 00:00:00 2001 From: r-ca Date: Mon, 25 Dec 2023 15:43:03 +0900 Subject: [PATCH] =?UTF-8?q?XMLMeta=E3=81=AE=E3=83=91=E3=83=BC=E3=82=B5?= =?UTF-8?q?=E3=83=BC=E3=82=92=E4=BD=9C=E6=88=90=20Impl=20WIP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lacerta/utils/impl/XmlMetaParserImpl.java | 81 ++++++++++++++++++- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/utils/src/main/java/one/nem/lacerta/utils/impl/XmlMetaParserImpl.java b/utils/src/main/java/one/nem/lacerta/utils/impl/XmlMetaParserImpl.java index 40505677..368ea78b 100644 --- a/utils/src/main/java/one/nem/lacerta/utils/impl/XmlMetaParserImpl.java +++ b/utils/src/main/java/one/nem/lacerta/utils/impl/XmlMetaParserImpl.java @@ -1,16 +1,93 @@ package one.nem.lacerta.utils.impl; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.InputSource; + +import java.io.StringReader; +import java.nio.file.Path; +import java.util.ArrayList; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + import one.nem.lacerta.model.document.internal.XmlMetaModel; +import one.nem.lacerta.model.document.internal.XmlMetaPageModel; import one.nem.lacerta.utils.XmlMetaParser; public class XmlMetaParserImpl implements XmlMetaParser{ @Override public XmlMetaModel parse(String xml) { - return null; + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(new InputSource(new StringReader(xml))); + + Element rootElement = document.getDocumentElement(); + + XmlMetaModel meta = new XmlMetaModel(); + + meta.setTitle(rootElement.getElementsByTagName("title").item(0).getTextContent()); + meta.setAuthor(rootElement.getElementsByTagName("author").item(0).getTextContent()); + meta.setDescription(rootElement.getElementsByTagName("description").item(0).getTextContent()); + meta.setDefaultBranch(rootElement.getElementsByTagName("defaultBranch").item(0).getTextContent()); + + ArrayList pages = new ArrayList<>(); + for(int i = 0; i < rootElement.getElementsByTagName("pages").getLength(); i++) { + Element pageElement = (Element) rootElement.getElementsByTagName("page").item(i); + XmlMetaPageModel page = new XmlMetaPageModel(); + page.setIndex(Integer.parseInt(pageElement.getElementsByTagName("index").item(0).getTextContent())); + page.setFilename(pageElement.getElementsByTagName("filename").item(0).getTextContent()); + pages.add(page); + } + + meta.setPages(pages); + + return meta; + } catch (Exception e) { + e.printStackTrace(); + } + return null; // TODO-rca: 例外 } @Override public String serialize(XmlMetaModel meta) { - return null; + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.newDocument(); + + Element rootElement = document.createElement("meta"); + + appendElement(document, rootElement, "title", meta.getTitle()); + appendElement(document, rootElement, "author", meta.getAuthor()); + appendElement(document, rootElement, "description", meta.getDescription()); + appendElement(document, rootElement, "defaultBranch", meta.getDefaultBranch()); + + Element pagesElement = document.createElement("pages"); + for(XmlMetaPageModel page : meta.getPages()) { + Element pageElement = document.createElement("page"); + appendElement(document, pageElement, "index", String.valueOf(page.getIndex())); + appendElement(document, pageElement, "filename", page.getFilename()); + pagesElement.appendChild(pageElement); + } + + rootElement.appendChild(pagesElement); + + document.appendChild(rootElement); + + return document.toString(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; // TODO-rca: 例外 + } + + // Internal Methods + private Element appendElement(Document document, Element rootElement, String name, String textContent) { + Element element = document.createElement(name); + element.setTextContent(textContent); + rootElement.appendChild(element); + return element; } }