• R/O
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

作業部屋の使い方を試しています。


Commit MetaInfo

Revision124 (tree)
Time2015-03-09 11:56:22
Authortuna_p

Log Message

【仕様変更】XML読書き処理
旧:SearchDataRW.java → 新:ScrapingXml.java

Change Summary

Incremental Difference

--- branches/b3/WebScraping/src/WebScraping/utility/HtmlSearch.java (revision 123)
+++ branches/b3/WebScraping/src/WebScraping/utility/HtmlSearch.java (revision 124)
@@ -40,7 +40,7 @@
4040 * @author kgto
4141 */
4242 public class HtmlSearch extends javax.swing.JFrame {
43- private final SearchDataRW sio = new SearchDataRW();
43+ private final ScrapingXml xmlwriter = new ScrapingXml();
4444
4545 SearchDataTableModel sdatatblmodel;
4646
@@ -332,8 +332,9 @@
332332 int selected = jFileChooser1.showOpenDialog(this);
333333 if (selected == JFileChooser.APPROVE_OPTION) {
334334 File file = jFileChooser1.getSelectedFile();
335- sio.load(file);
336- jTxtUrl.setText(sio.geturl());
335+ xmlwriter.load(file);
336+ jTxtUrl.setText(xmlwriter.getTestUrl());
337+ xmlwriter.getSdata();
337338 sdatatblmodel.setRowCount(0);
338339 for(int i = 0; i < SearchData.size(); i++) {
339340 SearchData sdata = SearchData.get(i);
@@ -347,7 +348,7 @@
347348 int selected = jFileChooser1.showSaveDialog(this);
348349 if (selected == JFileChooser.APPROVE_OPTION) {
349350 File file = jFileChooser1.getSelectedFile();
350- sio.seturl(jTxtUrl.getText());
351+ xmlwriter.setTestUrl(jTxtUrl.getText());
351352
352353 SearchData.clear();
353354 for(int row = 0; row < sdatatblmodel.getRowCount(); row++) {
@@ -354,7 +355,8 @@
354355 SearchData sdata = sdatatblmodel.getSearchData(row);
355356 SearchData.add(sdata);
356357 }
357- sio.save(file);
358+ xmlwriter.setSdata();
359+ xmlwriter.save(file);
358360 }
359361 }//GEN-LAST:event_jMenuSaveActionPerformed
360362
--- branches/b3/WebScraping/src/WebScraping/utility/LibraryXml.java (nonexistent)
+++ branches/b3/WebScraping/src/WebScraping/utility/LibraryXml.java (revision 124)
@@ -0,0 +1,138 @@
1+/*
2+ * Copyright (C) 2014-2015 kgto.
3+ *
4+ * This library is free software; you can redistribute it and/or
5+ * modify it under the terms of the GNU Lesser General Public
6+ * License as published by the Free Software Foundation; either
7+ * version 2.1 of the License, or (at your option) any later version.
8+ *
9+ * This library is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+ * Lesser General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU Lesser General Public
15+ * License along with this library; if not, write to the Free Software
16+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+ * MA 02110-1301 USA
18+ */
19+/*
20+ * $Id:$
21+ */
22+
23+package webScraping.utility;
24+
25+import java.io.File;
26+import java.io.FileNotFoundException;
27+import java.io.FileOutputStream;
28+import java.io.IOException;
29+import java.util.logging.Level;
30+import java.util.logging.Logger;
31+
32+import javax.xml.parsers.DocumentBuilder;
33+import javax.xml.parsers.DocumentBuilderFactory;
34+import javax.xml.parsers.ParserConfigurationException;
35+import javax.xml.transform.Transformer;
36+import javax.xml.transform.TransformerConfigurationException;
37+import javax.xml.transform.TransformerException;
38+import javax.xml.transform.TransformerFactory;
39+import javax.xml.transform.dom.DOMSource;
40+import javax.xml.transform.stream.StreamResult;
41+
42+import org.w3c.dom.DOMImplementation;
43+import org.w3c.dom.Document;
44+import org.w3c.dom.Element;
45+import org.w3c.dom.Node;
46+import org.w3c.dom.NodeList;
47+import org.xml.sax.SAXException;
48+
49+public class LibraryXml {
50+
51+ String xmlrootname = "xmlcontainer";
52+
53+ DocumentBuilder builder;
54+ public Document readdoc, writedoc;
55+ Element xmlroot;
56+
57+ /* ---------------------------------------------------------------------- *
58+ * コンストラクタ
59+ * ---------------------------------------------------------------------- */
60+ public LibraryXml() {
61+ try {
62+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
63+ builder = factory.newDocumentBuilder();
64+
65+ } catch (ParserConfigurationException ex) {
66+ Logger.getLogger(LibraryXml.class.getName()).log(Level.SEVERE, null, ex);
67+ }
68+ }
69+
70+ /* ---------------------------------------------------------------------- *
71+ * メソッド
72+ * ---------------------------------------------------------------------- */
73+ /* 読込み処理 */
74+ public Element getwriteRoot(String elementName) {
75+ mainElement();
76+ Element element = writedoc.createElement(elementName);
77+ xmlroot.appendChild(element);
78+ return element;
79+ }
80+
81+ private void mainElement() {
82+ if(writedoc == null) {
83+ DOMImplementation domImpl = builder.getDOMImplementation();
84+ writedoc = domImpl.createDocument("", xmlrootname, null);
85+ xmlroot = writedoc.getDocumentElement();
86+ }
87+ }
88+
89+ /**
90+ * XML書込み.
91+ * @param file
92+ */
93+ public void write(File file) {
94+ try {
95+ TransformerFactory transFactory = TransformerFactory.newInstance();
96+ Transformer transformer = transFactory.newTransformer();
97+
98+ transformer.setOutputProperty("indent", "yes"); // 改行指定
99+ transformer.setOutputProperty("method", "xml");
100+
101+ FileOutputStream os = new FileOutputStream(file);
102+ DOMSource source = new DOMSource(writedoc);
103+ StreamResult result = new StreamResult(os);
104+ transformer.transform(source, result);
105+
106+ } catch (TransformerConfigurationException ex) {
107+ Logger.getLogger(LibraryXml.class.getName()).log(Level.SEVERE, null, ex);
108+ } catch (FileNotFoundException | TransformerException ex) {
109+ Logger.getLogger(LibraryXml.class.getName()).log(Level.SEVERE, null, ex);
110+ }
111+ }
112+
113+ /* ---------------------------------------------------------------------- */
114+ /* 書込み処理 */
115+
116+ Element getreadRoot(String elementName) {
117+ NodeList nodelist = xmlroot.getElementsByTagName(elementName);
118+ Node node = nodelist.item(0);
119+ return (node.getNodeType() == Node.ELEMENT_NODE ? (Element)node : null);
120+ }
121+
122+ /**
123+ * XML読込み.
124+ * @param file
125+ */
126+ public void read(File file) {
127+ try {
128+ readdoc = builder.parse(file);
129+ xmlroot = readdoc.getDocumentElement();
130+
131+ } catch (SAXException | IOException ex) {
132+ Logger.getLogger(LibraryXml.class.getName()).log(Level.SEVERE, null, ex);
133+ }
134+ }
135+
136+ /* ---------------------------------------------------------------------- */
137+
138+}
--- branches/b3/WebScraping/src/WebScraping/utility/ScrapingXml.java (nonexistent)
+++ branches/b3/WebScraping/src/WebScraping/utility/ScrapingXml.java (revision 124)
@@ -0,0 +1,198 @@
1+/*
2+ * Copyright (C) 2014-2015 kgto.
3+ *
4+ * This library is free software; you can redistribute it and/or
5+ * modify it under the terms of the GNU Lesser General Public
6+ * License as published by the Free Software Foundation; either
7+ * version 2.1 of the License, or (at your option) any later version.
8+ *
9+ * This library is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+ * Lesser General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU Lesser General Public
15+ * License along with this library; if not, write to the Free Software
16+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+ * MA 02110-1301 USA
18+ */
19+/*
20+ * $Id:$
21+ */
22+
23+package webScraping.utility;
24+
25+import webScraping.core.SearchData;
26+import java.io.File;
27+import java.util.ArrayList;
28+import org.w3c.dom.Element;
29+import org.w3c.dom.Node;
30+import org.w3c.dom.NodeList;
31+
32+public class ScrapingXml {
33+ /* ---------------------------------------------------------------------- *
34+ * フィールド
35+ * ---------------------------------------------------------------------- */
36+ String rootnameScraping = "webscraping";
37+
38+ private String testUrl;
39+ private SearchData[] sdata;
40+
41+ LibraryXml xlib = new LibraryXml();
42+ Element root;
43+
44+ /* ---------------------------------------------------------------------- *
45+ * コンストラクタ
46+ * ---------------------------------------------------------------------- */
47+ ScrapingXml() {
48+ }
49+
50+ /* ---------------------------------------------------------------------- *
51+ * Setter
52+ * ---------------------------------------------------------------------- */
53+ void setTestUrl(String testUrl) {
54+ this.testUrl = testUrl;
55+ }
56+
57+ void setSdata() {
58+ this.sdata = new SearchData[SearchData.size()];
59+ for(int i = 0; i < SearchData.size(); i++) {
60+ this.sdata[i] = SearchData.get(i);
61+ }
62+ }
63+
64+ /* ---------------------------------------------------------------------- *
65+ * Getter
66+ * ---------------------------------------------------------------------- */
67+ String getTestUrl() {
68+ return testUrl;
69+ }
70+
71+ void getSdata() {
72+ SearchData.clear();
73+ for(SearchData sdata1 : sdata) {
74+ SearchData.add(sdata1);
75+ }
76+ }
77+
78+ /* ---------------------------------------------------------------------- *
79+ * メソッド
80+ * ---------------------------------------------------------------------- */
81+ void save(File file) {
82+
83+ elementset();
84+
85+ xlib.write(file);
86+ }
87+
88+ public void elementset() {
89+ root = xlib.getwriteRoot(rootnameScraping);
90+ elementsetUrl();
91+ elementsetSearchdata();
92+ System.out.println("elementset XmlScraping");
93+ }
94+
95+ private void elementsetUrl() {
96+ Element url = xlib.writedoc.createElement("url");
97+ url.appendChild(xlib.writedoc.createTextNode(testUrl));
98+ root.appendChild(url);
99+ }
100+
101+ private void elementsetSearchdata() {
102+ int count = 0;
103+ for(SearchData sdat : sdata) {
104+ Element cslist = xlib.writedoc.createElement("searchlist");
105+ cslist.setAttribute("listNo", String.valueOf(++count));
106+
107+ addChild(cslist, "item" , sdat.getitem());
108+ addChild(cslist, "htmltag" , sdat.getHtmltag());
109+ addChild(cslist, "htmlid" , sdat.getHtmlid());
110+ addChild(cslist, "htmlclass", sdat.getHtmlclass());
111+ addChild(cslist, "around" , sdat.getaround());
112+ addChild(cslist, "regexp" , sdat.getregexp());
113+
114+ root.appendChild(cslist);
115+ }
116+ }
117+
118+ private void addChild(Element cslist, String keyword, String data) {
119+ if(!data.isEmpty()) {
120+ Element element = xlib.writedoc.createElement(keyword);
121+ element.appendChild(xlib.writedoc.createTextNode(data));
122+ cslist.appendChild(element);
123+ }
124+ }
125+
126+ /* ---------------------------------------------------------------------- */
127+
128+ void load(File file) {
129+ xlib.read(file);
130+ elementget();
131+ }
132+
133+ public void elementget() {
134+ root = xlib.getreadRoot(rootnameScraping);
135+ elementgetUrl();
136+ elementgetSearchdata();
137+ }
138+
139+ private void elementgetUrl() {
140+ NodeList nodelist = root.getElementsByTagName("url");
141+ Node node = nodelist.item(0);
142+ testUrl = node.getFirstChild().getNodeValue();
143+ }
144+
145+ private void elementgetSearchdata() {
146+ ArrayList<SearchData> slist = new ArrayList<>();
147+
148+ NodeList nodelist = root.getElementsByTagName("searchlist");
149+ for(int i = 0; i < nodelist.getLength(); i++) {
150+ Node childnode = nodelist.item(i);
151+
152+ boolean sdatflg = false;
153+ SearchData sdat = new SearchData();
154+ for (Node child = childnode.getFirstChild(); child != null; child = child.getNextSibling()) {
155+ if(child.getNodeType() == Node.ELEMENT_NODE) {
156+ String tag = child.getNodeName();
157+ String rtn = "";
158+ if(child.getFirstChild() != null) {
159+ rtn = child.getFirstChild().getNodeValue();
160+ }
161+ switch (tag) {
162+ case "item" :
163+ sdat.setitem(rtn);
164+ sdatflg = true;
165+ break;
166+ case "htmltag" :
167+ sdat.setHtmltag(rtn);
168+ sdatflg = true;
169+ break;
170+ case "htmlid" :
171+ sdat.setHtmlid(rtn);
172+ sdatflg = true;
173+ break;
174+ case "htmlclass" :
175+ sdat.setHtmlclass(rtn);
176+ sdatflg = true;
177+ break;
178+ case "around" :
179+ sdat.setaround(rtn);
180+ sdatflg = true;
181+ break;
182+ case "regexp" :
183+ sdat.setregexp(rtn);
184+ sdatflg = true;
185+ break;
186+ }
187+ }
188+ }
189+ if(sdatflg) slist.add(sdat);
190+ }
191+ // 配列化
192+ sdata = new SearchData[slist.size()];
193+ for(int i = 0; i < slist.size(); i++) {
194+ sdata[i] = slist.get(i);
195+ }
196+ }
197+
198+}