Skip to content
Snippets Groups Projects
Commit 9fc541f3 authored by Nikos Marinos's avatar Nikos Marinos
Browse files

xml-validation 1st version

parent 9ce0242a
No related branches found
No related tags found
1 merge request!21Replace master with dev
......@@ -147,6 +147,13 @@ const allFileTypes = XMLFileTypes.concat(
const transformXML = async (xmlUri, xslUri, param) => {
const xml = await loadXML(xmlUri)
const xsl = await loadXML(xslUri)
if (!xmlUri.startsWith('blob')) {
const validationErrors = await validateXML(xmlUri)
if (validationErrors !== 'success') {
const errMsg = JSON.stringify(validationErrors)
throw new Error(errMsg)
}
}
if (typeof XSLTProcessor !== 'undefined') {
const xsltProcessor = new XSLTProcessor()
xsltProcessor.importStylesheet(xsl)
......@@ -164,6 +171,27 @@ const transformXML = async (xmlUri, xslUri, param) => {
}
}
const validateXML = async xml =>
new Promise((resolve, reject) => {
fetch('/xml/validate/api/post', {
method: 'POST',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${window.localStorage.getItem('token')}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
xml,
dtd: 'app/assets/xsl/dtd/archivearticle3.dtd',
}),
})
.then(response => response.json())
.then(data => resolve(data))
.catch(err => {
reject(err)
})
})
const loadXML = url =>
new Promise((resolve, reject) => {
let request = ''
......@@ -224,9 +252,9 @@ class EPMCTagged extends React.Component {
const { files } = currentVersion
const xml = files.find(file => file.type === 'PMC')
if (xml) {
const nxml = await transformXML(xml.url, nxmlXSL).catch(err => {
const nxml = await transformXML(xml.url, nxmlXSL, null).catch(err => {
if (err) {
this.setError('XML is invalid/malformed.')
this.setError(err.message)
}
})
if (nxml) {
......@@ -257,7 +285,7 @@ class EPMCTagged extends React.Component {
filelist,
).catch(err => {
if (err) {
this.setError('XML is invalid/malformed.')
this.setError(err)
}
})
const htmlBlob = new Blob([html.documentElement.outerHTML], {
......
......@@ -14,5 +14,6 @@
"./server/bulk",
"./server/eutils",
"./server/email",
"./server/xmlValidation",
"./server/db"
]
......@@ -35,6 +35,7 @@
"minio": "^6.0.0",
"moment": "^2.18.1",
"node-fetch": "^2.2.0",
"node-libxml": "^3.2.3",
"pdfjs-dist": "^2.0.489",
"prop-types": "^15.5.10",
"properties-reader": "0.0.16",
......
const passport = require('passport')
const Libxml = require('node-libxml')
const config = require('config')
const fetch = require('node-fetch')
const pubsweetServer = config.get('pubsweet-server.baseUrl')
const authBearer = passport.authenticate('bearer', { session: false })
const fetchFile = (fileUri, token) => {
const url = `${pubsweetServer}${fileUri}`
return new Promise((resolve, reject) => {
fetch(url, {
method: 'GET',
headers: {
Authorization: `${token}`,
Accept: 'application/xml',
},
})
.then(data => {
resolve(data.text())
})
.catch(err => {
reject(err)
})
})
}
module.exports = app => {
app.post('/xml/validate/api/post', authBearer, (req, res) => {
res.set({ 'Content-Type': 'application/json' })
const libxml = new Libxml()
fetchFile(req.body.xml, req.headers.authorization)
.then(xml => {
const xmlIsWellformed = libxml.loadXmlFromString(xml)
libxml.loadDtds([req.body.dtd])
const testDefaultV = libxml.validateAgainstDtds()
if (xmlIsWellformed && testDefaultV) {
res.json('success')
} else {
res.send(
JSON.stringify(libxml.validationDtdErrors[req.body.dtd], [
'column',
'level',
'line',
'message',
]),
)
}
})
.catch(err => {
res.send(JSON.stringify(err))
})
})
}
module.exports = {
backend: () => require('./api'),
}
{
"name": "xpub-epmc-xmlValidation",
"version": "1.0.0",
"lockfileVersion": 1
}
{
"name": "xpub-epmc-xmlValidation",
"version": "1.0.0",
"description": "API for validating against DTD",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "nm",
"license": "ISC"
}
This diff is collapsed.
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment