Error in Jena code: Content not allowed in Prolog - java

I have the following error: Content not allowed in Prolog** I have two methos, one called from main and another from button event handler. One method read the rdf file and another create individual of Student class based on name entered by users in a text field.
What is the expected error.
My code is:
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;
import javax.swing.*;
import java.awt.*;
import static java.io.FileDescriptor.in;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NewJFrame extends javax.swing.JFrame {
OntModel model = ModelFactory.createOntologyModel();
public static void readOntology(String file,OntModel model)
InputStream in = FileManager.get().open(file);
if (in ==null) {
throw new IllegalArgumentException( "File: " + file + " not found");
}
model.read(in,"");
Another method called from Button handler method()
private static void read1(String str,OntModel model)
{
String ns="http://www.semanticweb.org/untitled-ontology-0012/";
OntClass user = model.createClass(ns + "User");
OntClass user1 = model.getOntClass(ns + "User");
Individual janeSmith = user1.createIndividual(ns + str);
JOptionPane.showMessageDialog(null, janeSmith.toString());
try {
String file="F://student.owl";
FileWriter out=new FileWriter(file);
} catch (IOException ex) { Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
OntModel model=ModelFactory.createOntologyModel();
String str=jTextField2.getText();
read1(str, model);
}
And finally main()
public static void main(String args[]) {
OntModel model = ModelFactory.createOntologyModel();
// read camera ontology
String file="F://student.owl";
readOntology(file , model );

Related

Parse .prn file to html

I am trying to convert a .prn file to html. But due to file format, I am not able to parse in a way I want.
I tried many approaches. some of are:
package main;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class PrnToHtml {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader(".\\Workbook2.prn"));
FileWriter writer = new FileWriter("output_prn.html")) {
writer.write("<html><body><h3>PRN to HTML</h3><table border>\n");
String currentLine;
while ((currentLine = reader.readLine()) != null) {
writer.write("<tr>");
for(String field: currentLine.split("\\s{2,}")) // "\\s{2,}"
writer.write("<td>" + field + "</td>");
writer.write("</tr>\n");
}
writer.write("</table></body></html>\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output of this will be html page looks like this:
prn file\data looks like this:
Other this I tried to read this is:
package main;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class PRNToHtml {
private static final String DILIM_PRN = " ";
private static final Pattern PRN_SPLITTER = Pattern.compile(DILIM_PRN);
public static void main(String[] args) throws URISyntaxException, IOException {
try (#SuppressWarnings("resource")
Stream<String> lines = new BufferedReader(new FileReader(".\\Workbook2.prn")).lines()) {
List<String[]> inputValuesInLines = lines.map(l -> PRN_SPLITTER.split(l)).collect(Collectors.toList());
for (String[] strings : inputValuesInLines) {
for (String s : strings) {
System.out.print(s.replaceAll("\\s+", "") + " ");
}
System.out.println();
}
}
}
}
output of this is the exactly same looking in prn data file. But when I am trying to embed in html, it is looking weird like this:
Help will be appreciated.
Thank you :)

how to send a stream to another object?

How do I stream each line to the Parser instance?
package net.bounceme.dur.files;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
public class StreamFile {
private final static Logger log = Logger.getLogger(StreamFile.class.getName());
private Parser p = new Parser();
public StreamFile() {
}
public void read(String filePath) {
Stream<String> stream = null;
try {
stream = Files.lines(Paths.get(filePath));
} catch (IOException ex) {
Logger.getLogger(StreamFile.class.getName()).log(Level.SEVERE, null, ex);
}
stream.forEach(System.out::println);
}
}

Error Calling my testImage() Method from inside an imported .jar file from Eclipe in Wavemaker

I am simply trying to call my testImage() method in Wavemaker. I imported the .jar file after running the application perfectly in Eclipse. However when I call the same method in the .jar file in Wavemaker it gives my this error:
Error
Compile failed with output: [{"filename" : "master/services/MyJavaService1/src/com/demo_jquery/myjavaservice1/MyJavaService1.java","type" : "ERROR","lineNumber" : 134,"columnNumber" : 22,"startPosition" : 4743,"endPosition" : 4751,"message" : "The method testImage() in the type pictures.TestUrl is not applicable for the arguments (java.lang.String)"}]
I will now show you the TestUrl class which I call to invoke my two methods testImage() and getImage():
package pictures;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.imageio.ImageIO;
/*
* By: Victor Foning
*
* This program will consist of two Methods:
*
* The First will Test the Validity and reachability of an validity
* of an image URL.
*
* The second methods will log4j
*
*
*
*
*
*/
public class TestUrl {
public Boolean testImage (String l) {
// String urlString = "http://www.eurobiopark.org/sites/default/files/EurobioparkMashups5.1.png";
System.out.println("Using " + l);
// Open connection
URL u = null;
try {
u = new URL( l);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
URLConnection connection = null;
try {
connection = u.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Check if response code is HTTP_OK (200)
HttpURLConnection httpConnection
= (HttpURLConnection) connection;
int code = 0;
try {
code = httpConnection.getResponseCode();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String message = null;
try {
message = httpConnection.getResponseMessage();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(code + " " + message);
if (code == HttpURLConnection.HTTP_OK) {
return true;
} else {
return false;
}
}
public void getImage (String u) {
BufferedImage image =null;
try{
URL url =new URL(u);
// read the url
image = ImageIO.read(url);
ImageIO.write(image, "jpg",new File("C://Users//Foning//Desktop//GeoDataLab//mash7.jpg"));
}catch(IOException e){
e.printStackTrace();
}
}
}
The Picture is downloaded locally and the Console out is this:
Using http://www.eurobiopark.org/sites/default/files/EurobioparkMashups5.1.png
200 OK
true
Here is my TestDownload main Class through which I call my two Methods:
package pictures;
import java.io.IOException;
/*
* By: Victor Foning 17/Septembre/2019
*
* From this Main Methods we will call:
*
* TestUrl.java and the GetImage.java Methods
*
*
*
*/
public class TestDownload {
public static void main(String[] args) {
String path = "http://www.eurobiopark.org/sites/default/files/EurobioparkMashups5.1.png";
// We Begin encapsulating the TestUrl Methods
TestUrl im = new TestUrl();
boolean image = im.testImage(path);
if(image){
im.getImage(path);
System.out.print("true");
}
else{
System.out.print(" victor_WakeUP_false");
}
}
}
Here I then export the .jar file (in Bold) into Wavemaker and Make the same method invocation through my JavaService Class:
package com.demo_jquery.myjavaservice1;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pictures.TestUrl;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.springframework.beans.factory.annotation.Autowired;
import com.wavemaker.runtime.security.SecurityService;
import com.wavemaker.runtime.service.annotations.ExposeToClient;
import com.wavemaker.runtime.service.annotations.HideFromClient;
Here you will find the class getImageFromWaveMaker()I create inside my javaService1 class to invoke my two methods:
package com.demo_jquery.myjavaservice1;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pictures.TestUrl;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.springframework.beans.factory.annotation.Autowired;
import com.wavemaker.runtime.security.SecurityService;
import com.wavemaker.runtime.service.annotations.ExposeToClient;
import com.wavemaker.runtime.service.annotations.HideFromClient;
import com.demo_jquery.myjavaservice1.model.*;
#ExposeToClient
public class MyJavaService1 {
private static final Logger logger = LoggerFactory.getLogger(MyJavaService1.class);
#Autowired
private SecurityService securityService;
public void getImageFromWavemaker( String p) {
String path =
"http://www.eurobiopark.org/sites/default/files/EurobioparkMashups5.1.png";
// We Begin encapsulating the TestUrl Methods
TestUrl im = new TestUrl();
boolean image = im.testImage(path);
if(image){
//im.getImage(url);
logger.info("true");
}
else{
logger.info("victor_WakeUP_false");
}
}
}
Please help me figure out why I get and Error when I run this code?
Thanks to a bit of meditation and positive re-orientation from some good friends I was able to solve this issue.
I came to realize that even though camelcase is accepted when declaring method. I re-declared both methods in my TestUrl Class by starting them with a Capital Letter.
With reference to the Error Code:
Aslo, understanding that all classes in java are derived from the object class and that the String class is one of the most important class in Java. I made that java.lang.String class the Super Class when I created all my Classes in Eclipse.
I did a maven Compile, which generated a .jar file. I proceeded to upload the .jar file in my resource lib file on Wavemaker, Binded my variables accordingly with my JavaService class and the results were good.
Please feel free to express any further perspective on this issue, I would be glad to hear it. Thank you!

How to get values from grid using regex in java

I'm working on resume parser and i can get some of data i.e company details from text but not getting if it is kept in a grid or table
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class CmpnyNameex {
public static void main(String[] args)throws IOException {
String text="";
String name="";
XWPFDocument msDocx = new XWPFDocument(new FileInputStream("A:\\Resumes\\Anwesh.docx"));
XWPFWordExtractor extractor = new XWPFWordExtractor(msDocx);
text = extractor.getText();
}
catch(FileNotFoundException ex){ex.printStackTrace();
JOptionPane.showMessageDialog(null,"The system cannot find the file specified file it may be because of old file format","Error",JOptionPane.ERROR_MESSAGE);
}
String rx13="(?<=Have been associated with).*.(.*Ltd?)";
Pattern p1 = Pattern.compile(rx13);
Matcher found1 = p1.matcher(text);
while(found1.find())
{
name= found1.group(0);
}
}
}

how can i get all the hyperlinks and its paragraphs in an website?

I want to get all hyperlinks and name it as .txt file, and i want to store all paragraphs inside those each hyperlinks and save as a text file by their article title.
i have the code here and i am fixing this for 2 months. i could not get code for this crawling/scraping logic.
Anyone please code and fix it.
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.text.BadLocationException;
import javax.swing.text.EditorKit;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class App {
public static void main(String[] args) throws URISyntaxException,
IOException, BadLocationException {
HTMLDocument doc = new HTMLDocument() {
public HTMLEditorKit.ParserCallback getReader(int pos) {
return new HTMLEditorKit.ParserCallback() {
public void handleText(char[] data, int pos) {
System.out.println(data);
}
};
}
};
URL url = new URI("http://tamilblog.ishafoundation.org/").toURL();
URLConnection conn = url.openConnection();
Reader rd = new InputStreamReader(conn.getInputStream());
OutputStreamWriter writer = new OutputStreamWriter(
new FileOutputStream("ram.txt"), "UTF-8");
EditorKit kit = new HTMLEditorKit();
kit.read(rd, doc, 0);
try {
Document docs = Jsoup.connect(
"http://tamilblog.ishafoundation.org/").get();
Elements links = docs.select("a[href]");
Elements elements = docs.select("*");
System.out.println("Total Links :" + links.size());
for (Element element : elements) {
System.out.println(element.ownText());
}
for (Element link : links) {
String hrefUrl = link.attr("href");
if (!"#".equals(hrefUrl) && !hrefUrl.isEmpty()) {
System.out.println(" * a: link :" + hrefUrl);
System.out.println(" * a: text :" + link.text());
Document document = Jsoup.connect(hrefUrl)
.timeout(0) //Infinite timeout
.get();
String html = document.toString();
writer.write(html);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
writer.close();
}
}
}
Try something like this
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class NewClass {
public static void main(String[] args) throws IOException {
Document doc = Jsoup.connect("http://tamilblog.ishafoundation.org").get();
Elements section = doc.select("section#content");
Elements article = section.select("article");
for (Element a : article) {
System.out.println("Title : \n" + a.select("a").text());
System.out.println("Article summary: \n" + a.select("div.entry-summary").text());
}
}
}

Categories