Skip to content

Commit

Permalink
Merge pull request #77 from metanorma/issue/75
Browse files Browse the repository at this point in the history
Issue/75
  • Loading branch information
Intelligent2013 authored Oct 12, 2023
2 parents 51f7177 + f8c0bdf commit 7db7f72
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/metanorma/PublicationIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public List<String> getDocumentsPaths(String documentType) {
documentsPaths.add(inputXmlFilePath.toString());
}
} catch (Exception ex) {
System.out.println("Can't process the publication index '" + filename + "':" + ex);
System.err.println("Can't process the publication index '" + filename + "':" + ex);
}
return documentsPaths;
}
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/org/metanorma/RepositoryIndex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.metanorma;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class RepositoryIndex {

private String filename;

private final String REPOSITORY_INDEX_FILENAME = "repository_index.xml";

public RepositoryIndex(String startFolder) {
init(startFolder);
}

private void init(String startFolder) {
// find repository_index.xml in the current folder and parents folders

File f = new File(startFolder);
if (!f.isDirectory()) {
startFolder = f.getParent();
}

boolean endFoldersTree = false;

Path repositoryIndexPath = Paths.get(startFolder, REPOSITORY_INDEX_FILENAME);

while (!Files.exists(repositoryIndexPath) && !endFoldersTree) {
try {
repositoryIndexPath = Paths.get(repositoryIndexPath.getParent().getParent().toString(), REPOSITORY_INDEX_FILENAME);

} catch (Exception ex) {
System.err.println("Can't find the repository index '" + REPOSITORY_INDEX_FILENAME + "'.");
endFoldersTree = true;
}
}
if (endFoldersTree) {
filename = "";
} else {
filename = repositoryIndexPath.toString();
}
}

public String getPath() {
return filename;
}
}
25 changes: 21 additions & 4 deletions src/main/java/org/metanorma/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static String getFileContentFromResources(String fileName) throws Excepti
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
try (InputStream is = classLoader.getResourceAsStream(fileName)) {
if (is == null) {
System.out.println("Cannot get resource \"" + fileName + "\" from Jar file.");
System.err.println("Cannot get resource \"" + fileName + "\" from Jar file.");
return null;
}
try (InputStreamReader isr = new InputStreamReader(is);
Expand Down Expand Up @@ -86,9 +86,9 @@ public static void createSymbolicLink(String targetFilename, String symbolicLink
}
} else {

System.out.println("Cannot create the symbolic link \"" + symbolicLink + "\" for the file " + targetFilename + ".");
System.err.println("Cannot create the symbolic link \"" + symbolicLink + "\" for the file " + targetFilename + ".");
if (ex instanceof FileSystemException) {
System.out.println(((FileSystemException) ex).getReason());
System.err.println(((FileSystemException) ex).getReason());
}
}
}
Expand All @@ -100,7 +100,7 @@ public static void copyFile(String sourceFilename, String targetFilename) {
Path target = Paths.get(targetFilename);
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ex) {
System.out.println("Cannot copy the file \"" + sourceFilename + "\" to the file " + targetFilename + ": " + ex);
System.err.println("Cannot copy the file \"" + sourceFilename + "\" to the file " + targetFilename + ": " + ex);
}
}

Expand Down Expand Up @@ -253,4 +253,21 @@ public static String getRelativePath(String filePath, String outputPath) {
String strRelativePath = relativePath.toString().replace("\\","/");
return strRelativePath;
}

public static boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
try {
int d = Integer.parseInt(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}

public static boolean fileExists(String filename) {
Path path = Paths.get(filename);
return Files.exists(path);
}
}
28 changes: 28 additions & 0 deletions src/main/java/org/metanorma/XMLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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;
import org.xml.sax.XMLReader;
Expand All @@ -16,6 +18,9 @@
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.*;
import java.net.URL;
import java.nio.file.Path;
Expand Down Expand Up @@ -131,4 +136,27 @@ public static String getRootElement (String xml) {
}
return "";
}

public static String getTextByXPath(String xml, String expression) {
try {
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(is);
XPath xPath = XPathFactory.newInstance().newXPath();

NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
sb.append(node.getNodeValue());
}
return sb.toString();
} catch (Exception ex) {
System.err.println("Can't retrieve the text by XPath '" + expression + "':" + ex);
}
return "";
}
}
89 changes: 66 additions & 23 deletions src/main/java/org/metanorma/stepmod2mn.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.metanorma;

import java.io.*;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -10,21 +9,15 @@
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.URIResolver;
import javax.xml.transform.sax.SAXSource;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
Expand All @@ -33,15 +26,9 @@
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
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;

import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;


/**
* This class for the conversion of an NISO/ISO XML file to Metanorma XML or AsciiDoc
Expand All @@ -58,6 +45,8 @@ public class stepmod2mn {
static final String INPUT_LOG = "Input: %s (%s)";
static final String OUTPUT_LOG = "Output: %s (%s)";

static final String ERRORS_FATAL_LOG = "errors.fatal.log.txt";

static final String SVG_EXTENSION = ".svg";

static boolean DEBUG = false;
Expand Down Expand Up @@ -162,6 +151,8 @@ public class stepmod2mn {

String boilerplatePath = "";

String repositoryIndexPath = "";

String outputPathSchemas = "";

/**
Expand Down Expand Up @@ -288,11 +279,12 @@ public static void main(String[] args) throws ParseException {
List<Map.Entry<String,String>> inputOutputFiles = new ArrayList<>();

boolean isStandaloneXML = false;
String repositoryIndexPath = "";
// if remote file (http or https)
if (Util.isUrl(argXMLin)) {

if (!Util.isUrlExists(argXMLin)) {
System.out.println(String.format(INPUT_NOT_FOUND, XML_INPUT, argXMLin));
System.err.println(String.format(INPUT_NOT_FOUND, XML_INPUT, argXMLin));
System.exit(Constants.ERROR_EXIT_CODE);
}

Expand Down Expand Up @@ -327,7 +319,7 @@ public static void main(String[] args) throws ParseException {
fXMLin = fXMLin.getAbsoluteFile();
//System.out.println("fXMLin=" + fXMLin.toString());
if (!fXMLin.exists()) {
System.out.println(String.format(INPUT_NOT_FOUND, XML_INPUT, fXMLin));
System.err.println(String.format(INPUT_NOT_FOUND, XML_INPUT, fXMLin));
System.exit(Constants.ERROR_EXIT_CODE);
}

Expand All @@ -341,6 +333,9 @@ public static void main(String[] args) throws ParseException {
if (fXMLin.isDirectory()) {
inputFolder = fXMLin.getAbsolutePath();

RepositoryIndex repositoryIndex = new RepositoryIndex(inputFolder);
repositoryIndexPath = repositoryIndex.getPath();

try (Stream<Path> walk = Files.walk(Paths.get(fXMLin.getAbsolutePath()))) {
List<String> inputXMLfiles = walk.map(x -> x.toString())
.filter(f -> f.endsWith("resource.xml") || f.endsWith("module.xml")).collect(Collectors.toList());
Expand All @@ -361,6 +356,9 @@ public static void main(String[] args) throws ParseException {
// inputFolder is root of repository
inputFolder = rootFolder.getAbsolutePath();

RepositoryIndex repositoryIndex = new RepositoryIndex(argXMLin_normalized);
repositoryIndexPath = repositoryIndex.getPath();

PublicationIndex publicationIndex = new PublicationIndex(argXMLin_normalized);
String documentType = "";
if (cmd.hasOption("type")) {
Expand Down Expand Up @@ -389,11 +387,17 @@ public static void main(String[] args) throws ParseException {
outAdocFile = XMLUtils.getOutputAdocPath(argOutputPath, argXMLin.toString());
}

RepositoryIndex repositoryIndex = new RepositoryIndex(argXMLin);
repositoryIndexPath = repositoryIndex.getPath();

inputOutputFiles.add(new AbstractMap.SimpleEntry<>(argXMLin, outAdocFile));
}
}

try {

List<Map.Entry<String,String>> badInputOutputFiles = new ArrayList<>();

for (Map.Entry<String,String> entry: inputOutputFiles) {
String filenameIn = entry.getKey();
String filenameOut = entry.getValue();
Expand All @@ -411,10 +415,16 @@ public static void main(String[] args) throws ParseException {
resourcePath = fileIn.getParent() + File.separator;
}
app.setResourcePath(resourcePath);
app.setRepositoryIndexPath(repositoryIndexPath);
app.setOutputPathSchemas(outputPathSchemas);
app.convertstepmod2mn(filenameIn, fileOut);
boolean res = app.convertstepmod2mn(filenameIn, fileOut);
if (!res) {
badInputOutputFiles.add(entry);
}
}

inputOutputFiles.removeAll(badInputOutputFiles);

//if (isInputFolder) {
// Generate metanorma.yml in the root of path
//new MetanormaCollection(inputOutputFiles).generate(inputFolder);
Expand Down Expand Up @@ -452,7 +462,7 @@ public static void main(String[] args) throws ParseException {
}

//private void convertstepmod2mn(File fXMLin, File fileOut) throws IOException, TransformerException, SAXParseException {
private void convertstepmod2mn(String xmlFilePath, File fileOut) throws IOException, TransformerException, SAXParseException {
private boolean convertstepmod2mn(String xmlFilePath, File fileOut) throws IOException, TransformerException, SAXParseException {
System.out.println("Transforming...");
try {
Source srcXSL = null;
Expand All @@ -461,7 +471,15 @@ private void convertstepmod2mn(String xmlFilePath, File fileOut) throws IOExcept

// get linearized XML with default attributes substitution from DTD
String linearizedXML = XMLUtils.processLinearizedXML(xmlFilePath);


// Issue: https://github.com/metanorma/stepmod2mn/issues/75
// check @part
String part = XMLUtils.getTextByXPath(linearizedXML, "*/@part");
if (part.isEmpty() || !Util.isNumeric(part)) {
System.err.println("Ignore document processing due the wrong attribute 'part' value: '" + part + "'");
return false;
}

// load linearized xml
Source src = new StreamSource(new StringReader(linearizedXML));
ClassLoader cl = this.getClass().getClassLoader();
Expand Down Expand Up @@ -499,9 +517,18 @@ private void convertstepmod2mn(String xmlFilePath, File fileOut) throws IOExcept
if (outputPath == null) {
outputPath = System.getProperty("user.dir");
}

Path pathFileErrorsFatalLog = Paths.get(outputPath, ERRORS_FATAL_LOG);
File fileErrorsFatalLog = new File(pathFileErrorsFatalLog.toString());
if (fileErrorsFatalLog.exists()) {
Files.delete(pathFileErrorsFatalLog);
}

transformer.setParameter("outpath", outputPath);
transformer.setParameter("outpath_schemas", outputPathSchemas);
transformer.setParameter("boilerplate_path", boilerplatePath);
transformer.setParameter("repositoryIndex_path", repositoryIndexPath);
transformer.setParameter("errors_fatal_log", ERRORS_FATAL_LOG);

transformer.setParameter("debug", DEBUG);

Expand All @@ -513,12 +540,24 @@ private void convertstepmod2mn(String xmlFilePath, File fileOut) throws IOExcept

Util.writeStringToFile(adoc, fileOut);

if (fileErrorsFatalLog.length() != 0) {
// if current document doesn't exist in the repository index, then
// then delete it from list
return false;
} else {
// delete empty
if (Files.exists(fileErrorsFatalLog.toPath())) {
Files.delete(pathFileErrorsFatalLog);
}
}

} catch (SAXParseException e) {
throw (e);
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(Constants.ERROR_EXIT_CODE);
}
return true;
}


Expand Down Expand Up @@ -553,6 +592,10 @@ public void setBoilerplatePath(String boilerplatePath) {
this.boilerplatePath = boilerplatePath;
}

public void setRepositoryIndexPath(String repositoryIndexPath) {
this.repositoryIndexPath = repositoryIndexPath;
}

public void generateSVG(String xmlFilePath, String image, String outPath, boolean isSVGmap) throws IOException, TransformerException, SAXParseException {
List<String> xmlFiles = new ArrayList<>();
try (Stream<Path> walk = Files.walk(Paths.get(xmlFilePath))) {
Expand All @@ -562,7 +605,7 @@ public void generateSVG(String xmlFilePath, String image, String outPath, boolea
.filter(f -> f.toLowerCase().endsWith(Constants.XML_EXTENSION))
.collect(Collectors.toList());
} catch (Exception e) {
System.out.println("Can't generate SVG file(s) for " + xmlFilePath + "...");
System.err.println("Can't generate SVG file(s) for " + xmlFilePath + "...");
e.printStackTrace();
}
for(String xmlFile: xmlFiles) {
Expand Down
Loading

0 comments on commit 7db7f72

Please sign in to comment.