With the help of the api https://github.com/decorators-squad/eo-yaml I create a yaml file however I have the problem that the file format does not fit as I would like to have them
How my config should look like:
name: ali
age: unknown
gender: male
How my Config look:
yaml name: aliage: unknowngender: male
My create methode
for ( Field field : clazz.getDeclaredFields() ) {
if (Modifier.isPrivate(field.getModifiers())) {
field.setAccessible(true);
}
Object value = null;
try {
value = field.get(this);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
YamlMapping yaml = Yaml.createYamlMappingBuilder()
.add(field.getName(), value.toString()).build();
try {
fileWriter.write(yaml.toString());
fileWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}```
**My ConfigTest Class**
```java
#Configuration(name = "config.yml")
public class ConfigTest extends YamlConfig {
private final String name = "ali";
private final String age = "unknown";
private final String gender = "male";
public ConfigTest() {
this.create();
}
}
You are creating a new YamlMapping for each key/value pair and immediately render it. So you render three separate mappings:
name: ali
age: unknown
gender: male
Then, you concatenate them into a file. Since you do not add line breaks, they are all written to the same line.
What you actually want to do is to create one mapping that contains all three key/value pairs:
YamlMappingBuilder builder = Yaml.createYamlMappingBuilder();
for ( Field field : clazz.getDeclaredFields() ) {
if (Modifier.isPrivate(field.getModifiers())) {
field.setAccessible(true);
}
Object value = null;
try {
value = field.get(this);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
builder.add(field.getName(), value.toString());
}
try {
fileWriter.write(builder.build().toString());
fileWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
I have a class and inside it there is a baseDir variable which has been defined as follows:
public class experiment {
for (int exp = 0; exp < experimentCnt; exp++) {
String dirString = config.getClass().getSimpleName() + "_" + df.format(new Date());
String baseDir = new File(homeDir + "/" + dirString).getAbsolutePath();
System.out.println("Running simulation: " + dirString);
setCurrentDirectory(baseDir);
PrintWriter paramsLog = null;
try {
paramsLog = new PrintWriter(
new File("experimentParams.log").getAbsoluteFile(), "UTF-8");
paramsLog.println(params);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Now, I want to use that baseDir variable in another class. How can I make it accessible?
Instead of making a new variable inside your function, you should just make a public variable outside of your function.
public class experiment {
public String baseDir;
for (int exp = 0; exp < experimentCnt; exp++) {
String dirString = config.getClass().getSimpleName() + "_" + df.format(new Date());
baseDir = new File(homeDir + "/" + dirString).getAbsolutePath();
System.out.println("Running simulation: " + dirString);
setCurrentDirectory(baseDir);
PrintWriter paramsLog = null;
try {
paramsLog = new PrintWriter(
new File("experimentParams.log").getAbsoluteFile(), "UTF-8");
paramsLog.println(params);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
I'm having a problem with the textbox. I'm trying to copy a file inside the arraylist and transfer it to another folder.
When I'm using this: String strSource = "D:\\New folder\\";
String strDestination = "D:\\New folder\\Another Folder\\";
It copies the file inside but when i change it to String strSource = txtSource.getText(); and same with the destination there is no error but only catch but the destination folder is empty. If you guys confuse on my question i'll try to explain to you.
Copy Method
public void copyFiles(String source, String destination){
try {
File fileFrom = new File(source);
File fileTo = new File(destination);
Files.copy( fileFrom.toPath(), fileTo.toPath());
} catch (IOException e) {
// e.printStackTrace();
MessageBox msgBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
msgBox.setText("Error");
msgBox.setMessage("File is AlreadyExist."); //where this is the one catching..
msgBox.open();
}
}
Copy Button
String strSource = new String(txtSource.getText());
String strDestination = new String (txtDestination.getText());
try {
ArrayList<String> list = readConfigFileList(ConstantVariables.SPECIFIC_FILE_LIST);
for (String strList : list) {
copyFiles(strSource + strList, strDestination + strList);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This one is the solution to me i just add \\.
String strSource = txtSource.getText() + "\\;
GitHub issues download using eclipse egit doesn't return anything.
Recently, i've been attempting to create a java desktop application (for windows), that will download GitHub issues from a specific GitHub issue repository, and save them in a .csv file.
I've created a simple GUI using Swing to enable the input of repository names. I'm also using eclipse's egit library to establish a connection to GitHub in order to download issues. I use authentication, entered using a .properties file in order to authenticate egit's connection with GitHub.
Here is the main code my application uses to download the issues and write them to a .csv file:
package io.github.psgs.issuesdownload;
import io.github.psgs.issuesdownload.gui.GUI;
import org.eclipse.egit.github.core.Issue;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.IssueService;
import java.io.FileWriter;
import java.io.IOException;
public class IssuesDownload {
public static void main(String[] args) {
try {
Config.loadConfiguration();
} catch(IOException ex) {
}
GUI.main(args);
}
public static String saveIssues(String repoDetails) {
String[] repoInfo = repoDetails.split("/");
String repoOwner = repoInfo[0];
String repoName = repoInfo[1];
GitHubClient client = new GitHubClient();
client.setCredentials(Config.githubuser, Config.githubpass);
IssueService issueService = new IssueService(client);
try {
FileWriter writer = new FileWriter("issues.csv");
//String[] header = {"Id", "Title", "Creator", "Assignee", "Milestone", "State", "Body Text"};
writer.append("Id, Title, Creator, Assignee, Milestone, State, Body Text");
writer.append("\n");
for (Issue issue : issueService.getIssues(repoOwner, repoName, null)) {
//String[] data = {String.valueOf(issue.getId()), issue.getTitle(), issue.getUser().getName(), issue.getAssignee().getName(), issue.getMilestone().getTitle(), issue.getState(), issue.getBodyText()};
writer.append(String.valueOf(issue.getId()) + ",");
writer.append(issue.getTitle() + ",");
writer.append(issue.getUser().getName() + ",");
writer.append(issue.getAssignee().getName() + ",");
writer.append(issue.getMilestone().getTitle() + ",");
writer.append(issue.getState() + ",");
writer.append(issue.getBodyText());
writer.append("\n");
}
writer.flush();
writer.close();
return "Download Complete!";
} catch (IOException ex) {
System.out.println("An IOException has occured!");
ex.printStackTrace();
if (ex.getMessage().equalsIgnoreCase("api.github.com")) {
return "An error has occured, reaching " + ex.getMessage() + "! Please check your network connection.";
}
}
return "An error has occured!";
}
}
This code is also available at: https://gist.github.com/psgs/9048602
The whole repository can be found at: https://github.com/psgs/IssuesDownload
When I run this code, with the .properties file in the same directory as the compile .jar file, the GitHub issues don't appear in the .csv file. I've tested the .csv file output, and the headers write correctly when I remove the download code.
Would anybody know why this is happening? Perhaps it's an authentication problem that i've missed?
After trying a few new API wrappers, i've found an API library that works. I'm now using Kohsuke Kawaguchi's GitHub API for Java to connect to GitHub.
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>github-api</artifactId>
<version>1.49</version>
</dependency>
My code now reads as follows:
package io.github.psgs.issuesdownload;
import io.github.psgs.issuesdownload.gui.GUI;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import java.io.FileWriter;
import java.io.IOException;
public class IssuesDownload {
public static void main(String[] args) {
try {
Config.loadConfiguration();
} catch (IOException ex) {
System.out.println("An IOException had occured while loading the configuration!");
ex.printStackTrace();
}
GUI.main(args);
}
public static String saveIssues(String repoDetails, GHIssueState issueState) {
String[] repoInfo = repoDetails.split("/");
try {
GitHub github = GitHub.connectUsingOAuth(Config.githubtoken);
GHRepository repository = github.getUser(repoInfo[0]).getRepository(repoInfo[1]);
FileWriter writer = new FileWriter("issues.csv");
writer.append("Id, Title, Creator, Assignee, Milestone, State, Body Text");
writer.append("\n");
for (GHIssue issue : repository.getIssues(issueState)) {
writer.append(String.valueOf(issue.getNumber()) + ",");
writer.append(issue.getTitle() + ",");
writer.append(issue.getUser().getLogin() + ",");
if (issue.getAssignee() != null) {
writer.append(issue.getAssignee().getName() + ",");
} else {
writer.append(" ,");
}
if (issue.getMilestone() != null) {
writer.append(issue.getMilestone().getTitle() + ",");
} else {
writer.append(" ,");
}
writer.append(issue.getState() + ",");
writer.append(issue.getBody() + ",");
writer.append("\n");
}
writer.flush();
writer.close();
return "Download Complete!";
} catch (IOException ex) {
System.out.println("An IOException has occured!");
ex.printStackTrace();
if (ex.getMessage().equalsIgnoreCase("api.github.com")) {
return "An error has occurred reaching " + ex.getMessage() + "! Please check your network connection.";
}
}
return "An error has occured!";
}
}
It looks like the method you're using is meant to be used to retrieve the issues for the currently authenticated user.
Perhaps this test (in the project's repository) would be helpful. The only difference I see is that you're not passing a singletonMap and I'm not sure you exactly have to.
public static <T> void outputCsv(String fileName, String[] headerName,
List<T> listResult, HttpServletRequest request) {
HttpServletResponse response = ResponseUtil.getResponse();
OutputStream out;
try {
// Encode file name
fileName = Util.encodeUnicodeFile(fileName, request);
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/octet-stream; charset="
+ Constant.CONST_SJIS_ENCODING);
response.setHeader("Content-disposition", "attachment; filename=\""
+ fileName + "\"");
response.setCharacterEncoding(Constant.CONST_SJIS_ENCODING);
out = response.getOutputStream();
CSVWriter writer = new CSVWriter(new OutputStreamWriter(out,
Constant.CONST_WINDOW_JAPAN_ENCODING),
Constant.CONST_CSV_SEPARATOR_COMMA);
// Convert list result data to List <String[]>
List<String[]> list = convertListDataToCsvArray(listResult,
headerName);
// Write list data
writer.writeAll(list);
writer.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static <T> List<String[]> convertListDataToCsvArray(
List<T> listResult, String[] headerName) {
List<String[]> listCsv = new ArrayList<String[]>();
// First row is header
listCsv.add(headerName);
for (T object : listResult) {
// Get all fields of object
Field[] fields = object.getClass().getFields();
// Init row
String[] row = new String[headerName.length];
int index = 0;
for (Field field : fields) {
try {
if (field.getType().equals(Long.class)) {
// Read field value and set to string array
Long value = (Long) field.get(object);
row[index] = value != null ? String.valueOf(value) : "";
} else if (field.getType().equals(String.class)) {
// Read field value and set to string array
String value = (String) field.get(object);
row[index] = value != null ? value : "";
} else if (field.getType().equals(BigDecimal.class)) {
// Read field value and set to string array
BigDecimal value = (BigDecimal) field.get(object);
row[index] = value != null ? String.valueOf(value) : "";
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
index++;
}
// Add row
listCsv.add(row);
}
return listCsv;
}
This is the below code in my MyCrawler.java and it is crawling all those links that I have provided in href.startsWith but suppose If I do not want to crawl this particular page http://inv.somehost.com/people/index.html then how can I do this in my code..
public MyCrawler() {
}
public boolean shouldVisit(WebURL url) {
String href = url.getURL().toLowerCase();
if (href.startsWith("http://www.somehost.com/") || href.startsWith("http://inv.somehost.com/") || href.startsWith("http://jo.somehost.com/")) {
//And If I do not want to crawl this page http://inv.somehost.com/data/index.html then how it can be done..
return true;
}
return false;
}
public void visit(Page page) {
int docid = page.getWebURL().getDocid();
String url = page.getWebURL().getURL();
String text = page.getText();
List<WebURL> links = page.getURLs();
int parentDocid = page.getWebURL().getParentDocid();
try {
URL url1 = new URL(url);
System.out.println("URL:- " +url1);
URLConnection connection = url1.openConnection();
Map responseMap = connection.getHeaderFields();
Iterator iterator = responseMap.entrySet().iterator();
while (iterator.hasNext())
{
String key = iterator.next().toString();
if (key.contains("text/html") || key.contains("text/xhtml"))
{
System.out.println(key);
// Content-Type=[text/html; charset=ISO-8859-1]
if (filters.matcher(key) != null){
System.out.println(url1);
try {
final File parentDir = new File("crawl_html");
parentDir.mkdir();
final String hash = MD5Util.md5Hex(url1.toString());
final String fileName = hash + ".txt";
final File file = new File(parentDir, fileName);
boolean success =file.createNewFile(); // Creates file crawl_html/abc.txt
System.out.println("hash:-" + hash);
System.out.println(file);
// Create file if it does not exist
// File did not exist and was created
FileOutputStream fos = new FileOutputStream(file, true);
PrintWriter out = new PrintWriter(fos);
// Also could be written as follows on one line
// Printwriter out = new PrintWriter(new FileWriter(args[0]));
// Write text to file
Tika t = new Tika();
String content= t.parseToString(new URL(url1.toString()));
out.println("===============================================================");
out.println(url1);
out.println(key);
//out.println(success);
out.println(content);
out.println("===============================================================");
out.close();
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TikaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// http://google.com
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("=============");
}
And this is my Controller.java code from where MyCrawler is getting called..
public class Controller {
public static void main(String[] args) throws Exception {
CrawlController controller = new CrawlController("/data/crawl/root");
controller.addSeed("http://www.somehost.com/");
controller.addSeed("http://inv.somehost.com/");
controller.addSeed("http://jo.somehost.com/");
controller.start(MyCrawler.class, 20);
controller.setPolitenessDelay(200);
controller.setMaximumCrawlDepth(2);
}
}
Any suggestions will be appreciated..
How about adding a property to tell which urls you want to exclude.
Add to your exclusions list all the pages that you don't want them to get crawled.
Here is an example:
public class MyCrawler extends WebCrawler {
List<Pattern> exclusionsPatterns;
public MyCrawler() {
exclusionsPatterns = new ArrayList<Pattern>();
//Add here all your exclusions using Regular Expresssions
exclusionsPatterns.add(Pattern.compile("http://investor\\.somehost\\.com.*"));
}
/*
* You should implement this function to specify
* whether the given URL should be visited or not.
*/
public boolean shouldVisit(WebURL url) {
String href = url.getURL().toLowerCase();
//Iterate the patterns to find if the url is excluded.
for (Pattern exclusionPattern : exclusionsPatterns) {
Matcher matcher = exclusionPattern.matcher(href);
if (matcher.matches()) {
return false;
}
}
if (href.startsWith("http://www.ics.uci.edu/")) {
return true;
}
return false;
}
}
In this example we are telling that all urls that start with http://investor.somehost.com should not be crawled.
So these wont be crawled:
http://investor.somehost.com/index.html
http://investor.somehost.com/something/else
I recommend you reading about regular expresions.