Skip to content

Commit

Permalink
re-enable carriers plugin (works!) w/ NEW xml parsing library
Browse files Browse the repository at this point in the history
  • Loading branch information
billyc committed Dec 1, 2021
1 parent b8347d0 commit 9caaa1a
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 276 deletions.
38 changes: 1 addition & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
"version": "1.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"serve": "vite",
"build": "vite build",
"release": "standard-version",
"test:unit": "vue-cli-service test:unit",
"lint": "vue-cli-service lint",
"test:unit": "broken//vue-cli-service test:unit",
"lint": "broken//vue-cli-service lint",
"bumpversion": "standard-version",
"deploy": "bash scripts/deploy-ghpages.sh matsim-vsp/simwrapper"
},
"dependencies": {
"@aftersim/pako": "^1.0.11-3",
"@babel/runtime": "^7.16.3",
"@flowmap.gl/core": "^7.3.3",
"@luma.gl/core": "^8.5.10",
"@math.gl/polygon": "^3.5.6",
Expand Down Expand Up @@ -77,7 +76,6 @@
"vue-video-player": "^5.0.2",
"vuera": "^0.2.7",
"vuex": "^3.1.3",
"xml2js": "^0.4.23",
"yaml": "^1.7.2"
},
"devDependencies": {
Expand All @@ -89,7 +87,7 @@
"@types/debounce": "^1.2.0",
"@types/geojson": "^7946.0.7",
"@types/marked": "^0.7.2",
"@types/micromatch": "^4.0.1",
"@types/micromatch": "^4.0.2",
"@types/nprogress": "^0.2.0",
"@types/pako": "^1.0.1",
"@types/papaparse": "^5.0.3",
Expand Down
54 changes: 22 additions & 32 deletions src/js/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,44 +46,34 @@ export function findMatchingGlobInFiles(filenames: string[], glob: string): stri
return []
}

export async function parseXML(xml: string, settings: any) {
// The '$' object contains a leaf's attributes
// The '$$' object contains an explicit array of the children
export async function parseXML(xml: string, settings: any = {}) {
// This uses the fast-xml-parser library, which is the least-quirky
// of all the terrible XML libraries.
//
// Sometimes you can also refer to a child node by name, such as
// carrier.shipments

// Some examples:

// PLANS
// plan is at carriers.carrier[x].plan[0] -- are there ever multiple plans?
// tour is at plan.tour[x]
// -- $ has vehicleId
// -- $$ has array of:
// #name --> act/leg
// $ --> other params
// route --> string of links "12345 6789 123"
// - Element attributes are stored directly in the element as "$attributeName"
//
// - Items with just one element are stored as is; but you can
// force a path to be always-array with "alwaysArray: ['my.path.to.element]"
//
// - Order is not preserved; like items are stored as arrays. For matsim, this
// is only a problem for plans (I think?) but you can recreate the plan order
// since act and leg elements always alternate. (Or use "preserveOrder: true"
// but that creates LOTS of one-item arrays everywhere. Sad.)

// SHIPMENTS
// to get the array of shipment objects, use
// carriers.carrier[x].shipments.$$ -> returns array of shipment objects
// -- each shipment object: has .$ attributes
const defaultConfig = {
ignoreAttributes: false,
preserveOrder: false,
attributeNamePrefix: '$',
isArray: undefined as any,
}

// these options are all mandatory for reading the complex carriers
// file. The main weirdness is that matsim puts children of different
// types in an order that matters (act,leg,act,leg,act... etc)
const defaultConfig = () => {
return {
strict: true,
trim: true,
preserveOrder: true,
explicitChildren: true,
explicitArray: true,
// Allow user to pass in an array of "always as array" XML paths:
if (settings.alwaysArray)
defaultConfig.isArray = (name: string, jpath: string) => {
if (settings.alwaysArray.indexOf(jpath) !== -1) return true
}
}

const options = Object.assign(defaultConfig, settings)

const parser = new XMLParser(options)

return new Promise((resolve, reject) => {
Expand Down
Loading

0 comments on commit 9caaa1a

Please sign in to comment.