Created
November 11, 2014 01:39
-
-
Save OneRaynyDay/a3ddff2db97083bcdc9c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class DataParser { | |
public final static int XML_FORMAT = 1; | |
public final static int CSV_FORMAT = 2; | |
//stub for extended class | |
public final static int JSON_FORMAT = 3; | |
private String iString; | |
private Destructurer init; | |
private String iRoot; | |
private String[] iFields; | |
private static int initialType = CSV_FORMAT; | |
private String fString; | |
private Destructurer fin; | |
private String fRoot; | |
private String[] fFields; | |
private static int finalType = XML_FORMAT; | |
public static void setParseMode(int start, int end){ | |
initialType = start; | |
finalType = end; | |
} | |
/** | |
* This constructor assumes CSV_FORMAT is initial format. | |
* @param initial string for CSV | |
*/ | |
public DataParser(String initial, String root){ | |
initialType = CSV_FORMAT; | |
iString = initial; | |
init = new Destructurer(iString); | |
iRoot = root; | |
iFields = null; | |
} | |
/** | |
* | |
* @param initial string for XML | |
* @param root for selected subtree of XML | |
* @param sfields for selected fields wanted from the XML | |
*/ | |
public DataParser(String initial, String root, String[] sfields){ | |
initialType = XML_FORMAT; | |
iString = initial; | |
init = new Destructurer(iString); | |
iRoot = root; | |
iFields = sfields; | |
} | |
public String parse(){ | |
String[][] masterKey = null; | |
if(initialType == finalType) | |
return iString; | |
if(initialType == CSV_FORMAT){ | |
masterKey = init.destructureCSV(); | |
} | |
else if(initialType == XML_FORMAT){ | |
try{ | |
masterKey = init.destructureXML(iRoot, iFields); | |
}catch(Exception e){ | |
System.out.println("Initialized DataParser with no root or fields. XML parsing is unsuccessful."); | |
} | |
} | |
else; //JSON Stub | |
Structurer ending = new Structurer(masterKey); | |
if(finalType == CSV_FORMAT){ | |
return fString = ending.structureCSV(); | |
} | |
else if(finalType == XML_FORMAT){ | |
return fString = ending.structureXML("document" , iRoot); | |
} | |
else; //JSON Stub | |
return iString; | |
} | |
public String getInitial(){ | |
return iString; | |
} | |
public String getFinal(){ | |
return fString; | |
} | |
} |
This file contains hidden or 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
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.Scanner; | |
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; | |
public class Destructurer { | |
private String input; | |
public final static String DELIM_CSV = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"; | |
public final static String DELIM_XML = "<([^<>]+)>([^<>]+)</\\1>"; | |
// DELIM_JSON must be left undefined for extended classes | |
public Destructurer(String arg) { | |
input = arg; | |
} | |
/** | |
* @param root for the XML document to find, sfields for subtree leaf elements | |
* required | |
*/ | |
public String[][] destructureXML(String root, String[] sfields) { | |
XMLReader reader = new XMLReader(); | |
Document doc = null; | |
try { | |
doc = reader.loadXMLFromString(input); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
NodeList nodeList = reader.GetNodes(doc, root); | |
String[][] returnArr = new String[][] { sfields, | |
(String[]) (reader.GetNodeStrings(nodeList, sfields)).toArray() }; | |
return returnArr; | |
} | |
/** | |
* @return String[CSVKey][Each Element] | |
*/ | |
public String[][] destructureCSV() { | |
String product = ""; | |
Scanner reader = new Scanner(input); | |
String[] CSVKey = null; | |
String[] elements = new String[0]; | |
// skip white lines | |
while (reader.nextLine() == "") | |
; | |
CSVKey = reader.nextLine().split(DELIM_CSV); | |
while (reader.hasNext()) { | |
String line = reader.nextLine(); | |
String[] temp = line.split(DELIM_CSV); | |
try { | |
elements = concatArrays(elements, temp); | |
} catch (Exception e) { | |
System.out.println("Last elements tried to access : "); | |
for (String s : temp) | |
System.out.println(s); | |
e.printStackTrace(); | |
} | |
} | |
String[][] returnArr = new String[][] { CSVKey, elements }; | |
return returnArr; | |
} | |
// Stub | |
public String[] destructureJSON(String arg) { | |
return null; | |
} | |
private String[] concatArrays(String[] e1, String[] e2) { | |
Collection<String> collection = new ArrayList<String>(); | |
collection.addAll(Arrays.asList(e1)); | |
collection.addAll(Arrays.asList(e2)); | |
String[] e3 = collection.toArray(new String[] {}); | |
return e3; | |
} | |
} |
This file contains hidden or 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
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.Scanner; | |
public class ServerDelegate implements Runnable { | |
Socket socket; | |
public ServerDelegate(Socket s) { | |
socket = s; | |
} | |
public void Connect(String mFile) { | |
try { | |
System.out.println("Server initialized!"); | |
DataInputStream dis = new DataInputStream(new BufferedInputStream( | |
socket.getInputStream())); | |
FileOutputStream fos = new FileOutputStream(mFile); | |
BufferedOutputStream bos = new BufferedOutputStream(fos); | |
int bytesToRead = (int) dis.readLong(); | |
byte[] bytearray = new byte[bytesToRead]; | |
dis.read(bytearray, 0, bytearray.length); | |
bos.write(bytearray, 0, bytesToRead); | |
bos.flush(); | |
System.out.println("Calling parseXML"); | |
parseXML(mFile, "Object"); | |
sendXML(socket, new File("temp.xml")); | |
dis.close(); | |
socket.close(); | |
} catch (IOException e) { | |
System.out.println("Unable to connect server"); | |
e.printStackTrace(); | |
} | |
} | |
public void parseXML(String mFile, String indivNode) { | |
Scanner reader = null; | |
System.out.println("Parsing XML"); | |
try { | |
String mString = ""; | |
reader = new Scanner(new File(mFile)); | |
while (reader.hasNext()) | |
mString += ("\n" + reader.nextLine()); | |
PrintWriter transfer = new PrintWriter("temp.xml"); | |
DataParser dp = new DataParser(mString, indivNode); | |
String parsedResult = dp.parse(); | |
transfer.print(dp.parse()); | |
transfer.flush(); | |
transfer.close(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void sendXML(Socket socket, File file) { | |
System.out.println("Sending XML"); | |
try { | |
BufferedInputStream bis = new BufferedInputStream(new FileInputStream( | |
file)); | |
byte[] mByteArray = new byte[(int) file.length()]; | |
bis.read(mByteArray, 0, mByteArray.length); | |
DataOutputStream os = new DataOutputStream(new BufferedOutputStream( | |
socket.getOutputStream())); | |
// header for size of file | |
os.writeLong(file.length()); | |
os.write(mByteArray, 0, mByteArray.length); | |
os.flush(); | |
} catch (Exception e) { | |
System.out.println("File not able to be read"); | |
} | |
} | |
@Override | |
public void run() { | |
Connect("newCSV.csv"); | |
} | |
} |
This file contains hidden or 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
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
public class ServerPopulator { | |
public void go(int port) { | |
try { | |
ExecutorService pool = Executors.newCachedThreadPool(); | |
ServerSocket servsock = new ServerSocket(port); | |
while (true) {// keep accepting connections, forever | |
Socket s = servsock.accept(); | |
ServerDelegate sd = new ServerDelegate(s); | |
pool.submit(sd); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains hidden or 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
public class ServerStarter { | |
public static void main(String[] args){ | |
ServerPopulator server = new ServerPopulator(); | |
server.go(1234); | |
} | |
} |
This file contains hidden or 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
public class Structurer { | |
String[][] masterKey; | |
public Structurer(String[][] masterInput){ | |
masterKey = masterInput; | |
} | |
public String structureXML(String baseRoot, String indivNode){ | |
String[] CSVKey = masterKey[0]; | |
String[] temp = masterKey[1]; | |
String body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<" + baseRoot + ">"; | |
for(int j = 0; j < temp.length/CSVKey.length; j++){ | |
body += "\n\t<" + indivNode + ">"; | |
for(int i = 0; i < CSVKey.length; i++){ | |
body += "\n\t\t<" + CSVKey[i] + ">" + temp[i + j*CSVKey.length] + "</" + CSVKey[i] + ">"; | |
} | |
body += "\n\t</" + indivNode + ">"; | |
} | |
body += "\n</" + baseRoot + ">"; | |
return body; | |
} | |
public String structureCSV(){ | |
String[] header = masterKey[0]; | |
String[] body = masterKey[1]; | |
String head = ""; | |
String bod = ""; | |
for(String element : header){ | |
head += element + ","; | |
} | |
//cut off comma | |
head = head.substring(0, head.length()); | |
int counter = 0; | |
for(String element : body){ | |
counter++; | |
bod += element + ", "; | |
if(counter % header.length == 0){ | |
//cut off comma | |
bod = bod.substring(0, bod.length()); | |
bod += "\n"; | |
} | |
} | |
return head+bod; | |
} | |
//JSON stub | |
public void structureJSON(){ | |
} | |
} |
This file contains hidden or 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
import java.io.File; | |
import java.io.IOException; | |
import java.io.StringReader; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
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; | |
public class XMLReader | |
{ | |
public Document ReadXML(String sfile) throws ParserConfigurationException, SAXException, IOException | |
{ | |
File file = new File(sfile); | |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder db = dbf.newDocumentBuilder(); | |
Document doc = db.parse(file); | |
return doc; | |
} | |
public Document loadXMLFromString(String xml) throws Exception | |
{ | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder builder = factory.newDocumentBuilder(); | |
InputSource is = new InputSource(new StringReader(xml)); | |
Document doc = builder.parse(is); | |
return doc; | |
} | |
public NodeList GetNodes(Document opendoc, String selement) | |
{ | |
opendoc.getDocumentElement().normalize(); | |
NodeList nodelist = opendoc.getElementsByTagName(selement); | |
return nodelist; | |
} | |
public Node GetNode(NodeList nodelist, int index) | |
{ | |
Node node = null; | |
int length = nodelist.getLength(); | |
if (index > -1 && index < length) | |
node = nodelist.item(index); | |
return node; | |
} | |
public ArrayList<String> GetNodeStrings(NodeList nodelist, String[] selements) | |
{ | |
ArrayList<String> slist = new ArrayList<String>(); | |
for(int s=0; s<nodelist.getLength() ; s++) | |
{ | |
Node firstNode = GetNode(nodelist,s); | |
if(firstNode.getNodeType() == Node.ELEMENT_NODE) | |
{ | |
Element element = (Element)firstNode; | |
for (int x=0; x<selements.length; x++) | |
{ | |
NodeList nlist = element.getElementsByTagName(selements[x]); | |
Element item = (Element)nlist.item(0); | |
NodeList tlist = item.getChildNodes(); | |
slist.add(((Node)tlist.item(0)).getNodeValue().trim()); | |
} | |
} | |
} | |
return slist; | |
} | |
public boolean TestXML(String sfile,String selement,String[] sfields) throws ParserConfigurationException, SAXException, IOException | |
{ | |
boolean breturn = false; | |
try | |
{ | |
Document doc = ReadXML(sfile); | |
NodeList nodelist = GetNodes(doc,selement); | |
ArrayList<String> slist = GetNodeStrings(nodelist,sfields); | |
for (String s : slist) | |
System.out.println(s); | |
} | |
finally | |
{ | |
//System.out.println("Error in xml file: " + sfile); | |
} | |
breturn = true; | |
return breturn; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment