Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy-1 committed Nov 12, 2017
1 parent 0a4113b commit 2857897
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 53 deletions.
Binary file modified bin/org/emed/classes/Section.class
Binary file not shown.
Binary file modified bin/org/emed/main/Body.class
Binary file not shown.
Binary file modified bin/org/emed/main/Main.class
Binary file not shown.
Binary file modified bin/org/emed/main/TableFunctions.class
Binary file not shown.
Binary file modified latex.jar
Binary file not shown.
14 changes: 12 additions & 2 deletions src/org/emed/classes/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import java.util.ArrayList;
import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;

import org.w3c.dom.Node;



public class Section {
Expand All @@ -15,7 +21,9 @@ public String getTitle() {
return title;
}

public void setTitle(String title) {
public void setTitle(XPath xPath, Node nodeSection) throws XPathExpressionException {
Node nodeSectionTitle = (Node) xPath.compile("title").evaluate(nodeSection, XPathConstants.NODE);
String title = nodeSectionTitle.getTextContent();
this.title = title;
}

Expand All @@ -35,7 +43,9 @@ public List<Section> getSecContent() {
}

public void setSecContent(ArrayList<Section> seccontent) {
this.seccontent = seccontent;
if (this.seccontent == null) {
this.seccontent = seccontent;
}
}

}
32 changes: 31 additions & 1 deletion src/org/emed/latex/standard/BodyStandard.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,15 @@ private static String downloadFigures(Figure figure)
}

private static void writingParContent(BufferedWriter wrlatex, List<ParContent> parContents) throws IOException {
int ifList = 0;

for (ParContent parContentPar : parContents) {

if (parContentPar.getClass().getName() == "org.emed.classes.Par") {
Par par = (Par) parContentPar;
if (par.getType() != null && par.getType().contains("list-par")) {
wrlatex.write("\\item ");
}
wrlatex.write(par.getContent());
} else if (parContentPar.getClass().getName() == "org.emed.classes.Italic") {
Italic italic = (Italic) parContentPar;
Expand All @@ -275,7 +280,32 @@ private static void writingParContent(BufferedWriter wrlatex, List<ParContent> p
}


}
} else if (parContentPar.getClass().getName() == "org.emed.classes.ParContent") {

// For nested list
ifList += 1;

if (ifList == 1 && parContentPar.getType().contains("ordered")) {
wrlatex.newLine();
wrlatex.write("\\begin{enumerate}");
wrlatex.newLine();
} else if (ifList == 1 && parContentPar.getType().contains("unordered")) {
wrlatex.newLine();
wrlatex.write("\\begin{itemize}");
wrlatex.newLine();
}

List<ParContent> listParContents = parContentPar.getParContentList();
listParContents.forEach(listPar -> listPar.setType("list-par"));
writingParContent(wrlatex, listParContents);

if (ifList + 2 == parContents.size() && parContentPar.getType().contains("ordered")) {
wrlatex.write("\\end{enumerate}");
} else if (ifList + 2 == parContents.size() && parContentPar.getType().contains("unordered")) {
wrlatex.write("\\end{itemize}");
}

}

} // end of Paragraph content
}
Expand Down
86 changes: 53 additions & 33 deletions src/org/emed/main/Body.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,52 @@ public class Body {

protected static ArrayList<Section> body(Document document, XPath xPath) throws XPathExpressionException, DOMException {
NodeList nodeSections = (NodeList) xPath.compile("/article/body/sec").evaluate(document, XPathConstants.NODESET);
// set sections
ArrayList<Section> listSections = new ArrayList<Section>();
// set subsections
ArrayList<Section> sublistSections = new ArrayList<Section>();
//set subsubsections
ArrayList<Section> subsublistSections = new ArrayList<Section>();
for (int i = 0; i < nodeSections.getLength(); i++) {
Section section = new Section();
section.setType("");
Node nodeSection = nodeSections.item(i);
sectionParsing(xPath, listSections, sublistSections, subsublistSections, nodeSection);
sectionParsing(xPath, section, nodeSection);
listSections.add(section);

NodeList nodeSubSections = (NodeList) xPath.compile("sec").evaluate(nodeSection, XPathConstants.NODESET);
if (nodeSubSections != null) {
// set subsections
ArrayList<Section> subListSections = new ArrayList<Section>();
section.setSecContent(subListSections);
for (int ii = 0; ii < nodeSubSections.getLength(); ii++) {
Section subSection = new Section();
subSection.setType("sub");
Node nodeSubSection = nodeSubSections.item(ii);
sectionParsing(xPath, subSection, nodeSubSection);
subListSections.add(subSection);

NodeList nodeSubSubSections = (NodeList) xPath.compile("sec").evaluate(nodeSubSection, XPathConstants.NODESET);
if (nodeSubSubSections != null) {
//set subsubsections
ArrayList<Section> subSubListSections = new ArrayList<Section>();
subSection.setSecContent(subSubListSections);
for (int iii = 0; iii < nodeSubSubSections.getLength(); iii++) {
Section subSubSection = new Section();
subSubSection.setType("subsub");
Node nodeSubSubSection = nodeSubSubSections.item(iii);
sectionParsing(xPath, subSubSection, nodeSubSubSection);
subSubListSections.add(subSubSection);
}
}
}

}

}
return listSections;
}


private static void sectionParsing(XPath xPath, ArrayList<Section> listSections, ArrayList<Section> sublistSections, ArrayList<Section> subsublistSections, Node nodeSection)
private static void sectionParsing(XPath xPath, Section section, Node nodeSection)
throws XPathExpressionException, DOMException, NumberFormatException {
Node nodeSectionTitle = (Node) xPath.compile("title").evaluate(nodeSection, XPathConstants.NODE);
Section section = new Section();
Node ifSubSec = (Node) xPath.compile("parent::sec").evaluate(nodeSection, XPathConstants.NODE);
Node ifSubSubSec = (Node) xPath.compile("parent::sec/parent::sec").evaluate(nodeSection, XPathConstants.NODE);
if (ifSubSec == null) {
section.setType("");
listSections.add(section);
} else if (ifSubSec != null && ifSubSubSec == null) {
section.setType("sub");
sublistSections.add(section);
} else if (ifSubSec != null && ifSubSubSec != null) {
section.setType("subsub");
subsublistSections.add(section);
}
section.setTitle(nodeSectionTitle.getTextContent());

section.setTitle(xPath, nodeSection);
NodeList nodeSecElements = (NodeList) xPath.compile("p|fig|sec|table-wrap|list").evaluate(nodeSection, XPathConstants.NODESET);
for (int i1 = 0; i1 < nodeSecElements.getLength(); i1++) {

Expand All @@ -84,8 +99,7 @@ private static void sectionParsing(XPath xPath, ArrayList<Section> listSections,
ParContent itemList = new ParContent();
itemList.setType("unordered");
section.getSecContent().add(itemList);
parsingParContent(nodeSubSecListItem, itemList);

parsingParContent(nodeSubSecListItem, itemList);
}
Markup markup2 = new Markup();
markup2.setContent("\\end{itemize}");
Expand Down Expand Up @@ -205,18 +219,10 @@ private static void sectionParsing(XPath xPath, ArrayList<Section> listSections,

section.getSecContent().add(table);

} else if ((nodeSecElement.getNodeName() == "sec") && (nodeSecElement.getTextContent() != null)) {
if (section.getType() == "sub") {
section.setSecContent(subsublistSections);
} else if (section.getType() == "") {
section.setSecContent(sublistSections);
}
sectionParsing(xPath, listSections, sublistSections, subsublistSections, nodeSecElement);
}
}
}
}


protected static void parsingParContent(Node nodeSecElement, ParContent parContent) throws DOMException {
NodeList insidePars = nodeSecElement.getChildNodes();
for (int j = 0; j < insidePars.getLength(); j++) {
Expand Down Expand Up @@ -258,6 +264,20 @@ protected static void parsingParContent(Node nodeSecElement, ParContent parConte
xref.setTableContent(xrefTable);
parContent.getParContentList().add(xref);
}
} else if (insidePar.getNodeName() == "list") {
NodeList listItems = insidePar.getChildNodes();

for (int i = 0; i < listItems.getLength(); i++) {
NodeList listItem = listItems.item(i).getChildNodes();
for (int y = 0; y < listItem.getLength(); y++) {
ParContent itemList = new ParContent();
itemList.setType(insidePar.getAttributes().getNamedItem("list-type").getTextContent());
parContent.getParContentList().add(itemList);
Node listItemP = listItem.item(y);
parsingParContent(listItemP, itemList);
}
}

}
}
}
Expand Down
63 changes: 51 additions & 12 deletions src/org/emed/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,22 @@
public class Main {

public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException, XPathExpressionException, IllegalArgumentException, IllegalAccessException {
String inputFile = args[0];

// path to LaTeX in Gost format
String outputFile = args[1];

// path to LaTeX in Stadard format
String outputLatexStandard = args[2];

// path to bibtex
String outputBib = args[3];

writerToFile(inputFile, outputFile, outputLatexStandard, outputBib);

if (args.length == 4) {
String inputFile = args[0];
// path to LaTeX in Gost format
String outputFile = args[1];
// path to LaTeX in Standard format
String outputLatexStandard = args[2];
// path to bibtex
String outputBib = args[3];
writerToFile(inputFile, outputFile, outputLatexStandard, outputBib);
} else if (args.length == 3) {
String inputFile = args[0];
String outputLatexStandard = args[1];
String outputBib = args[2];
writerToFile(inputFile, outputLatexStandard, outputBib);
}
}

private static void writerToFile(String inputFile, String outputFile, String outputLatexStandard, String outputBib) throws IOException,
Expand Down Expand Up @@ -93,6 +96,42 @@ private static void writerToFile(String inputFile, String outputFile, String out
latexStandardWriter(wrlatex, latex, referenceLink);
bibWriter(bib, latex);
}

private static void writerToFile(String inputFile, String outputLatexStandard, String outputBib) throws IOException,
ParserConfigurationException, SAXException, XPathExpressionException, DOMException, NumberFormatException {


// writing LaTeX in World Standard
Path latexStandard = Paths.get(outputLatexStandard);
BufferedWriter wrlatex = Files.newBufferedWriter(latexStandard, StandardCharsets.UTF_8);

// writing bibtex
Path bibtexStandard = Paths.get(outputBib);
BufferedWriter bib = Files.newBufferedWriter(bibtexStandard, StandardCharsets.UTF_8);

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

Document document = builder.parse(inputFile);
XPath xPath = XPathFactory.newInstance().newXPath();

/* parsing JATS XML */
LaTeX latex = jatsParser(document, xPath);

/* creating reference to a bib with regex */
String referenceLink = outputBib.trim().replaceAll(".bib$", "");
if (referenceLink.contains("\\") || referenceLink.contains("/")) {
Pattern p = Pattern.compile("(\\w+)$");
Matcher m = p.matcher(referenceLink);
if (m.find()) {
referenceLink = m.group();
}
}

/* writing to LaTeX (standard, bib) */
latexStandardWriter(wrlatex, latex, referenceLink);
bibWriter(bib, latex);
}

private static void latexGostWriter(BufferedWriter wrobj, LaTeX latex) throws IOException {

Expand Down
21 changes: 16 additions & 5 deletions src/org/emed/main/TableFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ protected static void countColumns(XPath xPath, Table table, Node tableHead)
for (int w = 0; w < childFirstRows.getLength(); w++) {
Node childFirstRow = childFirstRows.item(w);
if (childFirstRow.getNodeValue() == null && (childFirstRow.getNodeName() == "th" || childFirstRow.getNodeName() == "td")) {
int number = Integer.parseInt(childFirstRow.getAttributes().getNamedItem("colspan").getNodeValue());
int number = 1;
if (childFirstRow.getAttributes().getNamedItem("colspan") != null) {
number = Integer.parseInt(childFirstRow.getAttributes().getNamedItem("colspan").getNodeValue());
}
columnNumber = columnNumber + number;
}
}
Expand All @@ -49,14 +52,22 @@ protected static void cellParsing(XPath xPath, Table table, Node tableHead, Stri
Cell cell = new Cell();
row.getCell().add(cell);
Node cellNode = cellNodes.item(w);
if (cellNode != null && cellNode.getAttributes() != null && cellNode.getAttributes().getNamedItem("colspan") != null) {
cell.setColspan(Integer.parseInt(cellNode.getAttributes().getNamedItem("colspan").getNodeValue()));
cell.setRowspan(Integer.parseInt(cellNode.getAttributes().getNamedItem("rowspan").getNodeValue()));
if (cellNode != null && cellNode.getAttributes() != null) {
if (cellNode.getAttributes().getNamedItem("colspan") != null) {
cell.setColspan(Integer.parseInt(cellNode.getAttributes().getNamedItem("colspan").getNodeValue()));
} else {
cell.setColspan(1);
}
if (cellNode.getAttributes().getNamedItem("rowspan") != null) {
cell.setRowspan(Integer.parseInt(cellNode.getAttributes().getNamedItem("rowspan").getNodeValue()));
} else {
cell.setRowspan(1);
}
ParContent parContent = new ParContent();
parContent.setType("tableCell");
cell.getParContent().add(parContent);
Body.parsingParContent(cellNode, parContent);
}
}

}

Expand Down

1 comment on commit 2857897

@Vitaliy-1
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Master branch now accepts 3 arguments: #1
Tables now can be without explicitly pointed rows and columns numbers: #3

Please sign in to comment.