Commit faa601ea authored by Markus Opolka's avatar Markus Opolka
Browse files

Remove legacy code

parent 14c36fe9
const express = require('express')
const path = require('path')
const lib = require('./lib.legacy.js')
// Setting
const dir = 'letters'
const app = express()
const PORT = 3000
// CORS Settings
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
app.get('/api/search', function(req, res) {
// Search Endpoint with query parameters
// <element attribute="value">content</element>
var query = {
element: req.query.element,
content: req.query.content,
attribute: req.query.attribute,
value: req.query.value
}
var data = lib.forAllFiles(lib.searchLetters, dir, {query:query})
res.send(data)
})
app.get('/api/features/', function(req, res) {
// Feature Search Endpoint with query parameters
// <feature category="diakritika" type="akut" subtype="akutstattgravis" ref="26">
var query = {
category: req.query.category,
type: req.query.type,
subtype: req.query.subtype
}
var data = lib.forAllFiles(lib.searchFeatures, dir, {query:query})
res.send(data)
})
app.get('/api/letters/:filename?', function (req, res) {
// Document Endpoint with optional Document name
// Lists either all or one given document
if (req.params.filename){
// Load specified document
var filename = req.params.filename + '.xml'
res.send(lib.getXMLasJSON(path.join(dir, filename)))
} else {
// Load all documents
var data = lib.forAllFiles(lib.listLetters, dir)
res.send(data)
}
})
// Where the magic happens
app.listen(PORT, function () {
console.log('> Listening at http://localhost:' + PORT)
})
const fs = require('fs')
const path = require('path')
const convert = require('xml-js')
const flat = require('flat')
const flatten = require('flatten')
const dir = 'letters'
function findKeyRecursive(object, keytofind, ret = []) {
/*
* Finds all keys in a nested object and returns them as an array
*/
var value
Object.keys(object).forEach(function(key) {
if (key === keytofind) {
ret.push(object[key])
return true
}
if (object[key] && typeof object[key] === 'object') {
value = findKeyRecursive(object[key], keytofind, ret)
return value !== undefined
}
else {
return true
}
})
return flatten(ret)
}
function searchInFeatures(features, query) {
/*
* Searches for a query in an array of features
* Attributes must be in _attributes
*/
var results = []
var reCat = new RegExp(query.category)
var reType = new RegExp(query.type)
var reSub = new RegExp(query.subtype)
for(var idx = 0; idx < features.length; idx++){
var match_category = reCat.test(features[idx]._attributes.category)
var match_type = reType.test(features[idx]._attributes.type)
var match_subtype = reSub.test(features[idx]._attributes.subtype)
if (match_category && match_type && match_subtype) {
results.push(features[idx])
}
}
return results
}
function transformFilename (filename, url = false) {
/*
* Transforms a full filepath:
* from example_1.xml to example 1
* URL: from example_1.xml to example_1
*/
filename = filename.slice(0, -4)
if (!url) {
filename = filename.replace(/_/g, ' ')
}
return filename
}
function searchInObject (obj, query) {
/*
* Search for query in a fiven flatobject
*/
var results = []
Object.keys(obj).forEach(function (key) {
// Attributes
if (typeof query.attribute !== "undefined"){
var re_attr = new RegExp('_attributes.' + query.attribute)
if (!re_attr.test(key)){
return
}
}
// Values
if (typeof query.value !== "undefined"){
var re_valu = new RegExp(query.value)
if (!re_valu.test(obj[key])){
return
}
}
// Elements
if (typeof query.element !== "undefined"){
var re_elem = new RegExp('[^_]' + query.element, 'g')
if (!re_elem.test(key)){
return
}
}
// Content
if (typeof query.content !== "undefined"){
var re_cont = new RegExp(query.content)
if (!re_cont.test(obj[key])){
return
}
}
// Add hit to results as object
res = {}
res[key] = obj[key]
results.push(res)
})
return results
}
function getXMLasJSON (filepath) {
/*
* TODO: This is duplicate Code. Needs to go
* TODO: Maybe check if XML is valid?
* Reads XML file and transforms it to JSON
*/
var data = {}
try {
var xml = fs.readFileSync(filepath, 'utf8')
data = convert.xml2js(xml, {compact: true})
} catch (err) {
console.log('Error while loading ' + filepath)
console.log(err)
return data
}
return data
}
function countReferences (features) {
/*
* Count the references in a feature
*/
var count = 0
for (idx = 0; idx < features.length; idx++) {
count = count + features[idx]._attributes.ref.trim().split(" ").length
}
return count
}
module.exports = {
getXMLasJSON: function (filepath) {
/*
* Reads XML file and transforms it to JSON
*/
var data = {}
try {
var xml = fs.readFileSync(filepath, 'utf8')
data = convert.xml2js(xml, {compact: true})
} catch (err) {
console.log('Error while loading ' + filepath)
console.log(err)
return data
}
return data
},
listLetters:function (file, params) {
/*
* Callback function
* Transforms a given parsed XML file into the list view
*/
return {
link: '/letters/' + file.slice(0, -4),
name: transformFilename(file),
filepath: path.join(dir, file),
url: '/api/letters/' + transformFilename(file, true)
}
},
searchFeatures: function(file, params) {
/*
* Callback function
* Search for features in file
*/
var query = params.query
var result = undefined
var letterJS = getXMLasJSON(path.join(dir, file))
var features = findKeyRecursive(letterJS, 'feature')
if (typeof query.category == "undefined"){ query.category = '.*' }
if (typeof query.type == "undefined"){ query.type = '.*' }
if (typeof query.subtype == "undefined"){ query.subtype = '.*' }
var res = searchInFeatures(features, query)
var count = countReferences(res)
if(res.length > 0) {
result = {
collection: letterJS.TEI.teiHeader.fileDesc.titleStmt.collection._text,
count: count,
name: transformFilename(file),
link: '/letters/' + file.slice(0, -4),
url: '/api/letters/' + transformFilename(file, true),
results: res
}
}
return result
},
searchLetters: function(file, params){
/*
* Callback function
* Search for query in file
*/
var query = params.query
var result = undefined
var letterJS = getXMLasJSON(path.join(dir, file))
var flatobject = flat(letterJS.TEI)
var res = searchInObject(flatobject, query)
if(res.length > 0) {
result = {
collection: letterJS.TEI.teiHeader.fileDesc.titleStmt.collection._text,
name: transformFilename(file),
link: '/letters/' + file.slice(0, -4),
url: '/api/letters/' + transformFilename(file, true),
results: res
}
}
return result
},
forAllFiles: function (callback, filepath, params={}) {
/*
* Load files from filepath and perform callback on each with given parameters
*/
var ret = []
try {
var files = fs.readdirSync(filepath)
} catch (err) {
console.log('> Error while loading files from ' + filepath)
console.log(err)
return []
}
for (var i in files) {
// We just want the XML documents
if (!files[i].endsWith('.xml')) {
continue
}
// Call callback function on each file
var val = callback(files[i], params)
if (typeof val !== "undefined"){
ret.push(val)
}
}
return ret
}
}
Supports Markdown
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