-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from 2023-2-Design-Pattern/hyh/XMLImpoter
feat: XMLImporter
- Loading branch information
Showing
39 changed files
with
173 additions
and
26 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,4 +102,4 @@ public Iterator loadRow() throws IOException | |
} | ||
|
||
public void endTable() throws IOException {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.holub.database; | ||
|
||
import com.holub.tools.ArrayIterator; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.Node; | ||
import org.w3c.dom.NodeList; | ||
import org.xml.sax.InputSource; | ||
import org.xml.sax.SAXException; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
import java.io.*; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
public class XMLImporter implements Table.Importer { | ||
private BufferedReader in; | ||
private String tableName; | ||
private List<String> columnNames; | ||
|
||
private Document xmlDoc; | ||
private List<Node> rowList; | ||
private int curIdx = 0; | ||
|
||
public XMLImporter(File xmlFile) throws IOException, SAXException, ParserConfigurationException { | ||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
DocumentBuilder builder = factory.newDocumentBuilder(); | ||
xmlDoc = builder.parse(xmlFile); | ||
xmlDoc.getDocumentElement().normalize(); | ||
} | ||
|
||
public XMLImporter(Reader in) throws IOException, SAXException, ParserConfigurationException { | ||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
DocumentBuilder builder = factory.newDocumentBuilder(); | ||
xmlDoc = builder.parse(new InputSource(in)); | ||
xmlDoc.getDocumentElement().normalize(); | ||
} | ||
|
||
public void startTable() throws IOException { | ||
Element root = xmlDoc.getDocumentElement(); // root: <tableName> | ||
tableName = root.getNodeName(); | ||
NodeList list = root.getChildNodes(); | ||
rowList = new ArrayList<>(); // <data> ... </data> List | ||
for(int i = 0; i < list.getLength(); i++){ | ||
if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { | ||
rowList.add(list.item(i)); | ||
} | ||
} | ||
|
||
Node n = rowList.get(0); // <data> ... </data> | ||
columnNames = new ArrayList<>(); | ||
list = n.getChildNodes(); | ||
for(int i = 0; i < list.getLength(); i++){ | ||
if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { | ||
columnNames.add(list.item(i).getNodeName()); | ||
} | ||
} | ||
} | ||
|
||
public String loadTableName() throws IOException { | ||
return tableName; | ||
} | ||
|
||
public int loadWidth() throws IOException { | ||
return columnNames.size(); | ||
} | ||
|
||
public Iterator loadColumnNames() throws IOException { | ||
return columnNames.iterator(); | ||
} | ||
|
||
public Iterator loadRow() throws IOException { | ||
Iterator row = null; | ||
|
||
if(curIdx < rowList.size()){ | ||
Node n = rowList.get(curIdx); | ||
NodeList list = n.getChildNodes(); | ||
List<String> data = new ArrayList<>(); | ||
for(int i=0; i<list.getLength(); i++) { | ||
if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { | ||
data.add(list.item(i).getTextContent().trim()); | ||
} | ||
} | ||
row = data.iterator(); | ||
curIdx++; | ||
} | ||
return row; | ||
} | ||
|
||
public void endTable() throws IOException { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.holub.database; | ||
|
||
import org.junit.Test; | ||
import org.xml.sax.SAXException; | ||
|
||
import javax.xml.parsers.ParserConfigurationException; | ||
import java.io.*; | ||
import java.util.Iterator; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class XMLImporterTest { | ||
@Test | ||
public void XML() throws IOException, ParserConfigurationException, SAXException { | ||
|
||
Reader in = new FileReader("c:/dp2023/people.xml"); | ||
XMLImporter importer = new XMLImporter(in); | ||
importer.startTable(); | ||
|
||
String tableName = importer.loadTableName(); | ||
assertEquals(tableName, "people"); | ||
|
||
|
||
int width = importer.loadWidth(); | ||
assertEquals(width, 3); | ||
|
||
Iterator columns = importer.loadColumnNames(); | ||
String[] columnNames = new String[width]; | ||
for (int i = 0; columns.hasNext();){ | ||
columnNames[i++] = (String) columns.next(); | ||
} | ||
assertEquals(columnNames[0], "last"); | ||
assertEquals(columnNames[1], "first"); | ||
assertEquals(columnNames[2], "addrId"); | ||
|
||
int idx = 0; | ||
String[][] rowValues = new String[2][width]; | ||
while ((columns = importer.loadRow()) != null) { | ||
for (int i = 0; columns.hasNext();) | ||
rowValues[idx][i++] = (String) columns.next(); | ||
idx++; | ||
} | ||
|
||
assertEquals(rowValues[0][0], "Holub"); | ||
assertEquals(rowValues[0][1], "Allen"); | ||
assertEquals(rowValues[0][2], "1"); | ||
assertEquals(rowValues[1][0], "Flintstone"); | ||
assertEquals(rowValues[1][1], "Wilma"); | ||
assertEquals(rowValues[1][2], "2"); | ||
|
||
importer.endTable(); | ||
} | ||
} |