diff --git a/app/package.json b/app/package.json index f96bedf..ce158ac 100644 --- a/app/package.json +++ b/app/package.json @@ -1,6 +1,6 @@ { "name": "addict", - "version": "0.2.0", + "version": "0.3.0", "license": "MIT", "type": "module", "scripts": { diff --git a/app/src/model/ddi.ts b/app/src/model/ddi.ts index 823f6a6..50df1cc 100644 --- a/app/src/model/ddi.ts +++ b/app/src/model/ddi.ts @@ -1,7 +1,18 @@ -import { CATEGORY_SCHEME_ID, CATEGORY_ID, CATEGORY_LABEL, CATEGORY_SCHEME_LABEL } from "@utils/contants"; +import { + CATEGORY_SCHEME_ID, + CATEGORY_ID, + CATEGORY_LABEL, + CATEGORY_SCHEME_LABEL, + VARIABLE_ID, + VARIABLE_LABEL +} from "@utils/contants"; -export type DDIObjectIDs = typeof CATEGORY_SCHEME_ID | typeof CATEGORY_ID; -export type DDIObjectLabels = typeof CATEGORY_SCHEME_LABEL | typeof CATEGORY_LABEL; +export type DDIObjectIDs = typeof CATEGORY_SCHEME_ID | typeof CATEGORY_ID | typeof VARIABLE_ID; + +export type DDIObjectLabels = + | typeof CATEGORY_SCHEME_LABEL + | typeof CATEGORY_LABEL + | typeof VARIABLE_LABEL; export type DDIBaseObject = { URN: string; diff --git a/app/src/utils/badges.ts b/app/src/utils/badges.ts index 6615a76..1ae04a0 100644 --- a/app/src/utils/badges.ts +++ b/app/src/utils/badges.ts @@ -2,7 +2,14 @@ import { lighten } from "@mui/material/styles"; import { DDIObjectIDs, DDIObjectLabels } from "@model/ddi"; -import { CATEGORY_SCHEME_ID, CATEGORY_ID, CATEGORY_SCHEME_LABEL, CATEGORY_LABEL } from "./contants"; +import { + CATEGORY_SCHEME_ID, + CATEGORY_ID, + CATEGORY_SCHEME_LABEL, + CATEGORY_LABEL, + VARIABLE_ID, + VARIABLE_LABEL +} from "./contants"; export const getLabelFromId = (id: DDIObjectIDs): DDIObjectLabels => { if (id === CATEGORY_SCHEME_ID) { @@ -11,6 +18,9 @@ export const getLabelFromId = (id: DDIObjectIDs): DDIObjectLabels => { if (id === CATEGORY_ID) { return CATEGORY_LABEL; } + if (id === VARIABLE_ID) { + return VARIABLE_LABEL; + } throw new Error(`Unknow DDI object id: ${id}`); }; @@ -18,6 +28,7 @@ export const getBadgeColor = (baseColor: string) => (id: DDIObjectIDs): string => { if (id === CATEGORY_SCHEME_ID) return lighten(baseColor, 0); - if (id === CATEGORY_ID) return lighten(baseColor, 0.15); + if (id === CATEGORY_ID) return lighten(baseColor, 0.1); + if (id === VARIABLE_ID) return lighten(baseColor, 0.2); throw new Error(`Unknow id: ${id}`); }; diff --git a/app/src/utils/contants.ts b/app/src/utils/contants.ts index e564b62..22c5650 100644 --- a/app/src/utils/contants.ts +++ b/app/src/utils/contants.ts @@ -3,14 +3,17 @@ import { DDIObjectIDs } from "@model/ddi"; /* Route path */ export const CATEGORY_SCHEME_ID = "category-scheme"; export const CATEGORY_ID = "category"; +export const VARIABLE_ID = "variable"; -export const DDI_OBJECTS = [CATEGORY_SCHEME_ID, CATEGORY_ID] as Array; +export const DDI_OBJECTS = [CATEGORY_SCHEME_ID, CATEGORY_ID, VARIABLE_ID] as Array; /* Supported DDI objects */ export const CATEGORY_SCHEME_LABEL = "Category Scheme"; export const CATEGORY_LABEL = "Category"; +export const VARIABLE_LABEL = "Variable"; /* XML */ export const CATEGORY_SCHEME_XML_PATH = "l:CategoryScheme"; export const CATEGORY_XML_PATH = "l:Category"; +export const VARIABLE_XML_PATH = "l:Variable"; diff --git a/app/src/utils/xml/Category.ts b/app/src/utils/xml/Category.ts index ec4407f..528ae44 100644 --- a/app/src/utils/xml/Category.ts +++ b/app/src/utils/xml/Category.ts @@ -2,7 +2,7 @@ import { CATEGORY_ID, CATEGORY_XML_PATH, CATEGORY_SCHEME_ID } from "@utils/conta import { DDIBaseObject, DDIDetailledObject } from "@model/ddi"; -import { getCode, getElementLabel, getElementURN, getLabelsByLang, getPreferedLabel } from "./common"; +import { getCode, getElementContent, getElementURN, getLabelsByLang, getPreferedLabel } from "./common"; export const getCategories = (xmlDoc: Document | Element): DDIBaseObject[] => { const categories = xmlDoc.getElementsByTagName(CATEGORY_XML_PATH); @@ -25,11 +25,10 @@ export const getCategory = (xmlDoc: Document, id: string): DDIDetailledObject => if (!category) throw new Error(`Unknow Category Scheme: ${id}`); const labels = category.getElementsByTagName("r:Content"); - // Find parent const categoryScheme = category.closest("CategoryScheme") as Element; const parentURN = getElementURN(categoryScheme); - const parentLabel = getElementLabel(categoryScheme); - // TODO find parent + const parentLabel = getElementContent(categoryScheme); + return { URN: getElementURN(category), labels: getLabelsByLang(labels), diff --git a/app/src/utils/xml/CategoryScheme.ts b/app/src/utils/xml/CategoryScheme.ts index b6c9fa8..b89557a 100644 --- a/app/src/utils/xml/CategoryScheme.ts +++ b/app/src/utils/xml/CategoryScheme.ts @@ -26,7 +26,7 @@ export const getCategoryScheme = (xmlDoc: Document, id: string): DDIDetailledObj if (!categoryScheme) throw new Error(`Unknow Category Scheme: ${id}`); const labels = categoryScheme.getElementsByTagName("r:Content"); const children = getCategories(categoryScheme); - // TODO getCategories as child + // TODO find parent return { URN: getElementURN(categoryScheme), diff --git a/app/src/utils/xml/Variable.ts b/app/src/utils/xml/Variable.ts new file mode 100644 index 0000000..04e039f --- /dev/null +++ b/app/src/utils/xml/Variable.ts @@ -0,0 +1,33 @@ +import { VARIABLE_ID, VARIABLE_XML_PATH } from "@utils/contants"; + +import { DDIBaseObject, DDIDetailledObject } from "@model/ddi"; + +import { getCode, getElementURN, getLabelsByLang, getPreferedLabel } from "./common"; + +export const getVariables = (xmlDoc: Document | Element): DDIBaseObject[] => { + const variables = xmlDoc.getElementsByTagName(VARIABLE_XML_PATH); + return Array.from(variables).map(v => { + const labels = v.getElementsByTagName("r:Content"); + return { + URN: getElementURN(v), + label: getPreferedLabel(getLabelsByLang(labels)), + type: VARIABLE_ID + }; + }); +}; + +export const getVariable = (xmlDoc: Document, id: string): DDIDetailledObject => { + const variables = xmlDoc.getElementsByTagName(VARIABLE_XML_PATH); + const variable = Array.from(variables).find(v => { + const foundId = v.querySelector("ID")?.textContent; + return id === foundId; + }); + if (!variable) throw new Error(`Unknow Variable: ${id}`); + const labels = variable.getElementsByTagName("r:Content"); + + return { + URN: getElementURN(variable), + labels: getLabelsByLang(labels), + code: getCode(variable) + }; +}; diff --git a/app/src/utils/xml/common.ts b/app/src/utils/xml/common.ts index b7391dd..8fed715 100644 --- a/app/src/utils/xml/common.ts +++ b/app/src/utils/xml/common.ts @@ -19,7 +19,7 @@ export const getElementURN = (e: Element): string => { return `${agency}:${id}:${version}`; }; -export const getElementLabel = (e: Element): string => +export const getElementContent = (e: Element): string => getLabelsByLang(e.getElementsByTagName("r:Content"))[PREFERED_LANGUAGE]; export const getCode = (e: Element): string => new XMLSerializer().serializeToString(e); diff --git a/app/src/utils/xml/index.ts b/app/src/utils/xml/index.ts index a6986a5..b968146 100644 --- a/app/src/utils/xml/index.ts +++ b/app/src/utils/xml/index.ts @@ -1,23 +1,34 @@ -import { CATEGORY_SCHEME_ID, CATEGORY_ID, CATEGORY_SCHEME_LABEL, CATEGORY_LABEL } from "@utils/contants"; +import { + CATEGORY_SCHEME_ID, + CATEGORY_ID, + CATEGORY_SCHEME_LABEL, + CATEGORY_LABEL, + VARIABLE_ID, + VARIABLE_LABEL +} from "@utils/contants"; import { DDIBaseObject, DDIDetailledObject, DDIObjectIDs, DDIObjectLabels } from "@model/ddi"; import { getCategories, getCategory } from "./Category"; import { getCategoryScheme, getCategorySchemes } from "./CategoryScheme"; +import { getVariable, getVariables } from "./Variable"; export const getDDIObjects = (xmlDoc: Document): DDIBaseObject[] => [ ...getCategorySchemes(xmlDoc), - ...getCategories(xmlDoc) + ...getCategories(xmlDoc), + ...getVariables(xmlDoc) ]; export const getDDIObject = (content: Document, type: DDIObjectIDs, id: string): DDIDetailledObject => { if (type === CATEGORY_SCHEME_ID) return getCategoryScheme(content, id); if (type === CATEGORY_ID) return getCategory(content, id); + if (type === VARIABLE_ID) return getVariable(content, id); throw new Error(`Unknow DDI object type: ${type}`); }; export const getTitle = (type: DDIObjectIDs): DDIObjectLabels => { - if (type === "category-scheme") return CATEGORY_SCHEME_LABEL; - if (type === "category") return CATEGORY_LABEL; + if (type === CATEGORY_SCHEME_ID) return CATEGORY_SCHEME_LABEL; + if (type === CATEGORY_ID) return CATEGORY_LABEL; + if (type === VARIABLE_ID) return VARIABLE_LABEL; throw new Error(`Unknow type: ${type}`); }; diff --git a/app/yarn.lock b/app/yarn.lock index abf3a9c..ef8f3e1 100644 --- a/app/yarn.lock +++ b/app/yarn.lock @@ -1950,7 +1950,7 @@ react-router@6.28.0: dependencies: "@remix-run/router" "1.21.0" -react-syntax-highlighter@^15.5.13: +react-syntax-highlighter@^15.6.1: version "15.6.1" resolved "https://registry.yarnpkg.com/react-syntax-highlighter/-/react-syntax-highlighter-15.6.1.tgz#fa567cb0a9f96be7bbccf2c13a3c4b5657d9543e" integrity sha512-OqJ2/vL7lEeV5zTJyG7kmARppUjiB9h9udl4qHQjjgEos66z00Ia0OckwYfRxCSFrW8RJIBnsBwQsHZbVPspqg==