Skip to content

Commit

Permalink
Cleanup and fix #658
Browse files Browse the repository at this point in the history
  • Loading branch information
dgarijo committed Dec 31, 2023
1 parent 95387b0 commit 0e46a31
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 133 deletions.
3 changes: 3 additions & 0 deletions src/main/java/widoco/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ private void completeOntologyMetadata(OWLAnnotation a, OWLOntology o) {
if (this.currentLanguage.equals(valueLanguage)
|| (abstractSection == null || "".equals(abstractSection))) {
abstractSection = value;
this.setIncludeAbstract(true); // in case users set no place holder text but added their own
}
} catch (Exception e) {
logger.error("Error while getting ontology abstract. No literal provided");
Expand All @@ -596,6 +597,7 @@ private void completeOntologyMetadata(OWLAnnotation a, OWLOntology o) {
|| (mainOntologyMetadata.getDescription() == null
|| mainOntologyMetadata.getDescription().isEmpty())) {
mainOntologyMetadata.setDescription(value);
this.setIncludeDescription(true);
}
} catch (Exception e) {
logger.error("Error while getting ontology description. No literal provided");
Expand Down Expand Up @@ -821,6 +823,7 @@ private void completeOntologyMetadata(OWLAnnotation a, OWLOntology o) {
if (this.currentLanguage.equals(valueLanguage)
|| (introText == null || "".equals(introText))) {
introText = value;
this.setIncludeIntroduction(true);
}
} catch (Exception e) {
logger.error("Error while introduction annotation. No literal provided");
Expand Down
158 changes: 25 additions & 133 deletions src/main/java/widoco/WidocoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,55 +155,10 @@ public static void writeModel(OWLOntologyManager m, OWLOntology o, OWLDocumentFo
}
}

// /**
// * Method that reads a local file and loads it into the configuration.
// * @param model
// * @param ontoPath
// * @param ontoURL
// */
// private static void readOntModel(OntModel model,Configuration c){
// String[] serializations = {"RDF/XML", "TTL", "N3"};
// String ontoPath = c.getOntologyPath();
// String ext = "";
// for(String s:serializations){
// InputStream in;
// try{
// in = FileManager.get().open(ontoPath);
// if (in == null) {
// System.err.println("Error: Ontology file not found");
// return;
// }
// model.read(in, null, s);
// System.out.println("Vocab loaded in "+s);
// if(s.equals("RDF/XML")){
// ext="xml";
// }else if(s.equals("TTL")){
// ext="ttl";
// }else if(s.equals("N3")){
// ext="n3";
// }
// c.getMainOntology().addSerialization(s, "ontology."+ext);
// //c.setVocabSerialization(s);
// break;
// }catch(Exception e){
// System.err.println("Could not open the ontology in "+s);
// }
// }
//
// }

public static void copyResourceFolder(String[] resources, String savePath) throws IOException {
for (String resource : resources) {
String aux = resource.substring(resource.lastIndexOf("/") + 1, resource.length());
File b = new File(savePath + File.separator + aux);
b.createNewFile();
copyLocalResource(resource, b);
}
}

public static void copyResourceDir(String resourceFolder, File destinationFolder) throws IOException {
// Determine if running from JAR or as source
logger.info("copyResourceFolder from "+resourceFolder+" to "+ destinationFolder);
logger.info("Copying resource folder from "+resourceFolder+" to "+ destinationFolder);
URL resourceUrl = WidocoUtils.class.getClassLoader().getResource(resourceFolder);
if (!destinationFolder.exists())
destinationFolder.mkdirs();
Expand Down Expand Up @@ -300,102 +255,39 @@ public static void copyLocalResource(String resourceName, File dest) {
}
}


/**
* Copy a file from outside the project into the desired file.
*
* Auxiliary method for reading local resources and returning their content
* @param path
* @param dest
* path of the file
* @return
* content of the file
*/
public static void copyExternalResource(String path, File dest) {
try {
InputStream is = new FileInputStream(path);
copy(is, dest);
} catch (Exception e) {
public static String readExternalResource(String path) {
String content = "";
try{
content = new String ( Files.readAllBytes( Paths.get(path) ) );
}catch (IOException e){
logger.error("Exception while copying " + path + e.getMessage());
}
return content;
}

public static String readExternalResource(String path) {
String content = "";
try{
content = new String ( Files.readAllBytes( Paths.get(path) ) );
}catch (IOException e){
logger.error("Exception while copying " + path + e.getMessage());
}
return content;
}

/**
* Code to unzip a file. Inspired from
* http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/ Taken
* from
*
* @param resourceName
* @param outputFolder
*/
public static void unZipIt(String resourceName, String outputFolder) {

byte[] buffer = new byte[1024];

try {
ZipInputStream zis = new ZipInputStream(CreateResources.class.getResourceAsStream(resourceName));
ZipEntry ze = zis.getNextEntry();

while (ze != null) {
String fileName = ze.getName();
File newFile = new File(outputFolder, fileName);
if (!newFile.toPath().normalize().startsWith(outputFolder)) {
throw new RuntimeException("Bad zip entry");
}
// System.out.println("file unzip : "+ newFile.getAbsoluteFile());
if (ze.isDirectory()) {
String temp = newFile.getAbsolutePath();
new File(temp).mkdirs();
} else {
String directory = newFile.getParent();
if (directory != null) {
File d = new File(directory);
if (!d.exists()) {
d.mkdirs();
}
}
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
ze = zis.getNextEntry();
}

zis.closeEntry();
zis.close();

} catch (IOException ex) {
logger.error("Error while extracting the reosurces: " + ex.getMessage());
}

}

public static void copy(InputStream is, File dest) throws Exception {
OutputStream os = null;
try {
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} catch (Exception e) {
logger.error("Exception while copying resource. " + e.getMessage());
throw e;
} finally {
if (is != null)
is.close();
if (os != null)
os.close();
}
try (OutputStream os = new FileOutputStream(dest)) {
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} catch (Exception e) {
logger.error("Exception while copying resource. " + e.getMessage());
throw e;
} finally {
if (is != null)
is.close();
}
}

public static String getValueAsLiteralOrURI(OWLAnnotationValue v) {
Expand Down
Binary file removed src/main/resources/lode.zip
Binary file not shown.
Binary file removed src/main/resources/oops.zip
Binary file not shown.
Binary file removed src/main/resources/webvowl_1.1.7.zip
Binary file not shown.
28 changes: 28 additions & 0 deletions src/test/java/widoco/CreateDocInThreadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,34 @@ public void testIssue530() {
fail("Error while running the test: " +e.getMessage());
}
}

/**
* Test to check if after the -noPlaceHolder flag is used, but the ontology has a custom intro
* the custom introduction annotation works correctly.
* Reference: https://github.com/dgarijo/Widoco/issues/658
*/
@org.junit.Test
public void testIssue658() {
try {
String pathToOnto = "test" + File.separator + "medatatestont.ttl";
c.setFromFile(true);
//emulating the flag "-noPlaceHolder"
c.setIncludeIntroduction(false);
c.setIncludeAbstract(false);
c.setIncludeDescription(false);
this.c.setOntologyPath(pathToOnto);
//read the model from file
WidocoUtils.loadModelToDocument(c);
c.loadPropertiesFromOntology(c.getMainOntology().getOWLAPIModel());
//not needed, but added for consistency with the other tests.
CreateResources.generateDocumentation(c.getDocumentationURI(), c, c.getTmpFile());
//read from myDoc/doc/sections/introduction.html
String result = WidocoUtils.readExternalResource("myDoc/doc/sections/introduction-en.html");
assertTrue(result.contains("This is an intro"));
}catch(Exception e){
fail("Error while running the test: " +e.getMessage());
}
}
//
/**
* This is a test to see if a big ontology works (several MB)
Expand Down

0 comments on commit 0e46a31

Please sign in to comment.