mirror of
https://github.com/lacerta-doc/Lacerta.git
synced 2024-11-22 16:03:15 +00:00
XMLMetaのパーサーを作成 Impl WIP
This commit is contained in:
parent
b98fa18cae
commit
bd1a1d56cb
|
@ -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<XmlMetaPageModel> 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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user