Skip to content

Commit

Permalink
Merge pull request #25 from computer-engineering-uniovi/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
thewillyhuman authored Mar 15, 2018
2 parents 7000d76 + 0f3bb23 commit 5d142ea
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 13 deletions.
6 changes: 4 additions & 2 deletions stdlib/Foundation/CSVFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
import java.util.List;

public class CSVFile {
private URL _urlToFile;

private URL _urlToFile;
private String _separator = "", _terminator = "";
private List<CSVRecord> _rows;
private String[] _headers;
Expand Down Expand Up @@ -175,4 +176,5 @@ public void save() {

printWriter.close();
}
}

}
64 changes: 53 additions & 11 deletions stdlib/Foundation/URL.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
/*
* This source file is part of the Swift open source project.
*
* Copyright (c) 2017 Guillermo Facundo Colunga and the Swift project authors.
* Licensed under GNU General Public License v3.0.
*
* See /LICENSE for license information.
*
*/
package Foundation;

/**
*
* A value that identifies the location of a resource, such as an item on a
* remote server or the path to a local file.
*
* @author Guillermo Facundo Colunga
* @version 201802032056
*/
public class URL {

private String _fileURLWithPath = "";


public URL(String fileURLWithPath) {
this._fileURLWithPath = fileURLWithPath;
}

public String getFileURL() {
return this._fileURLWithPath;
}

private String _fileURLWithPath = "";
private String _user, _password;
private int _port;

public URL( String fileURLWithPath ) {
this._fileURLWithPath = fileURLWithPath;
}

public String getFileURL() {
return this._fileURLWithPath;
}

public String getUser() {
return this._user;
}

public void setUser( String user ) {
_user = user;
}

public String getPassword() {
return this._password;
}

public void setPassword( String password ) {
_password = password;
}

public int getPort() {
return _port;
}

public void setPort( int port ) {
_port = port;
}
}
91 changes: 91 additions & 0 deletions stdlib/Foundation/XMLWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package Foundation;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class XMLWriter {

/**
* Write an XML based on the objects received. You have to specify the root name
* of the document and it´s name.
*
* @param objects
* the List objects received
* @param root
* the root of the document
*/
public <T> String save(List<T> objects, String root) {
String xml = getHeadborard();
xml += openElement(root);
for (Object o : objects) {
Class<?> clazz = o.getClass();
String objectName = o.getClass().getSimpleName();
xml += openElement(objectName);

for (Field field : clazz.getDeclaredFields()) {
String fieldName = field.getName();
xml += openElement(fieldName);
xml += getValue(field, clazz, o);
xml += closeElement(fieldName);
}
xml += closeElement(objectName);
}
xml += closeElement(root);
return xml;
}

/**
* Returns the value of a generic attribute. You need to specify the class where
* the attribute is located to search the correct method that returns it´s
* value. The object is needed too because you have to invoke that method with
* it.
*
* @param field
* the
* @param clazz
* @param o
* @return
*/
private String getValue(Field field, Class<?> clazz, Object o) {
String fieldName = field.getName();
for (Method method : clazz.getMethods()) {
if (method.getName().toLowerCase().endsWith("get" + fieldName.toLowerCase())) {
try {
return String.valueOf(method.invoke(o));
} catch (IllegalAccessException e) {
System.out.println("Could not determine method: " + method.getName());
} catch (InvocationTargetException e) {
System.out.println("Could not determine method: " + method.getName());
}
}
}
return "error";
}

/**
* Returns true if the Class that receives inherit from Collection
*
* @param c
* @return
*/
public boolean isClassCollection(Class<?> c) {
return Collection.class.isAssignableFrom(c) || Map.class.isAssignableFrom(c) || c.isArray();
}

private String openElement(String element) {
return "<" + element + ">";
}

private String closeElement(String element) {
return "</" + element + ">";
}

private String getHeadborard() {
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
}

}
20 changes: 20 additions & 0 deletions stdlib/TestKit/AcceptanceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This source file is part of the swift open source project.
*
* Copyright (c) 2018 willy and the swift project authors.
* Licensed under GNU General Public License v3.0.
*
* See /LICENSE for license information.
*
*/
package TestKit;

/**
* Instance of AcceptanceTest.java
*
* @author
* @version
*/
public interface AcceptanceTest {

}
20 changes: 20 additions & 0 deletions stdlib/TestKit/DBTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This source file is part of the swift open source project.
*
* Copyright (c) 2018 willy and the swift project authors.
* Licensed under GNU General Public License v3.0.
*
* See /LICENSE for license information.
*
*/
package TestKit;

/**
* Instance of DBTest.java
*
* @author
* @version
*/
public interface DBTest {

}
20 changes: 20 additions & 0 deletions stdlib/TestKit/IntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This source file is part of the swift open source project.
*
* Copyright (c) 2018 willy and the swift project authors.
* Licensed under GNU General Public License v3.0.
*
* See /LICENSE for license information.
*
*/
package TestKit;

/**
* Instance of IntegrationTest.java
*
* @author
* @version
*/
public interface IntegrationTest {

}
20 changes: 20 additions & 0 deletions stdlib/TestKit/RegressionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This source file is part of the swift open source project.
*
* Copyright (c) 2018 willy and the swift project authors.
* Licensed under GNU General Public License v3.0.
*
* See /LICENSE for license information.
*
*/
package TestKit;

/**
* Instance of RegressionTest.java
*
* @author
* @version
*/
public interface RegressionTest {

}
20 changes: 20 additions & 0 deletions stdlib/TestKit/SmokeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This source file is part of the swift open source project.
*
* Copyright (c) 2018 willy and the swift project authors.
* Licensed under GNU General Public License v3.0.
*
* See /LICENSE for license information.
*
*/
package TestKit;

/**
* Instance of SmokeTest.java
*
* @author
* @version
*/
public interface SmokeTest {

}
20 changes: 20 additions & 0 deletions stdlib/TestKit/UnitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This source file is part of the swift open source project.
*
* Copyright (c) 2018 willy and the swift project authors.
* Licensed under GNU General Public License v3.0.
*
* See /LICENSE for license information.
*
*/
package TestKit;

/**
* Instance of UnitTest.java
*
* @author
* @version
*/
public interface UnitTest {

}
1 change: 1 addition & 0 deletions test/Foundation/CSVFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.Test;

public class CSVFileTest {

private CSVFile file;

@Before public void setUp() {
Expand Down
35 changes: 35 additions & 0 deletions test/Foundation/XMLWriterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package Foundation;

import static org.junit.Assert.assertEquals;

import java.awt.Point;
import java.util.ArrayList;

import org.junit.Test;

public class XMLWriterTest {

@Test
public void test() {
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+"<Points><Point><x>2.0</x><y>3.0</y><serialVersionUID>"
+"error</serialVersionUID></Point><Point><x>4.0</x><y>3.0"
+"</y><serialVersionUID>error</serialVersionUID></Point><Point>"
+"<x>6.0</x><y>9.0</y><serialVersionUID>error</serialVersionUID>"+
"</Point><Point><x>5.0</x><y>7.0</y><serialVersionUID>error</serialVersionUID>"
+"</Point><Point><x>1.0</x><y>2.0</y><serialVersionUID>error</serialVersionUID>"
+"</Point></Points>";


ArrayList<Point> p1 = new ArrayList<Point>();
p1.add(new Point(2, 3));
p1.add(new Point(4, 3));
p1.add(new Point(6, 9));
p1.add(new Point(5, 7));
p1.add(new Point(1, 2));

assertEquals(expected, new XMLWriter().save(p1, "Points"));

}

}

0 comments on commit 5d142ea

Please sign in to comment.