Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export * from './lifecycle'
export * from './metadata'
export * from './organizationalContact'
export * from './organizationalEntity'
export * from './postalAddress'
export * from './property'
export * from './service'
export * from './swid'
Expand Down
7 changes: 7 additions & 0 deletions src/models/organizationalEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,34 @@ Copyright (c) OWASP Foundation. All Rights Reserved.
import type { Comparable } from '../_helpers/sortable'
import { SortableComparables, SortableStringables } from '../_helpers/sortable'
import { OrganizationalContactRepository } from './organizationalContact'
import type { PostalAddress } from './postalAddress'

export interface OptionalOrganizationalEntityProperties {
name?: OrganizationalEntity['name']
url?: OrganizationalEntity['url']
contact?: OrganizationalEntity['contact']
address?: OrganizationalEntity['address']
}

export class OrganizationalEntity implements Comparable<OrganizationalEntity> {
name?: string
url: Set<URL | string>
contact: OrganizationalContactRepository
address?: PostalAddress

constructor (op: OptionalOrganizationalEntityProperties = {}) {
this.name = op.name
this.url = op.url ?? new Set()
this.contact = op.contact ?? new OrganizationalContactRepository()
this.address = op.address
}

compare (other: OrganizationalEntity): number {
/* eslint-disable @typescript-eslint/strict-boolean-expressions -- run compares in weighted order */
return (this.name ?? '').localeCompare(other.name ?? '') ||
(this.address === undefined
? other.address === undefined ? 0 : -1
: other.address === undefined ? 1 : this.address.compare(other.address)) ||
this.contact.compare(other.contact) ||
(new SortableStringables(this.url)).compare(new SortableStringables(other.url))
/* eslint-enable @typescript-eslint/strict-boolean-expressions */
Expand Down
70 changes: 70 additions & 0 deletions src/models/postalAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*!
This file is part of CycloneDX JavaScript Library.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

SPDX-License-Identifier: Apache-2.0
Copyright (c) OWASP Foundation. All Rights Reserved.
*/

import type { Comparable } from '../_helpers/sortable'

export interface OptionalPostalAddressProperties {
country?: PostalAddress['country']
region?: PostalAddress['region']
locality?: PostalAddress['locality']
postOfficeBoxNumber?: PostalAddress['postOfficeBoxNumber']
postalCode?: PostalAddress['postalCode']
streetAddress?: PostalAddress['streetAddress']
}

/**
* A postal address used to identify a contactable location.
*
* @see https://cyclonedx.org/docs/1.7/xml/#type_postalAddressType
*/
export class PostalAddress implements Comparable<PostalAddress> {
country?: string
region?: string
locality?: string
postOfficeBoxNumber?: string
postalCode?: string
streetAddress?: string

constructor (op: OptionalPostalAddressProperties = {}) {
this.country = op.country
this.region = op.region
this.locality = op.locality
this.postOfficeBoxNumber = op.postOfficeBoxNumber
this.postalCode = op.postalCode
this.streetAddress = op.streetAddress
}

compare (other: PostalAddress): number {
const comparables: Array<[string | undefined, string | undefined]> = [
[this.country, other.country],
[this.region, other.region],
[this.locality, other.locality],
[this.postOfficeBoxNumber, other.postOfficeBoxNumber],
[this.postalCode, other.postalCode],
[this.streetAddress, other.streetAddress]
]
for (const [selfValue, otherValue] of comparables) {
const compared = (selfValue ?? '').localeCompare(otherValue ?? '')
if (compared !== 0) {
return compared
}
}
return 0
}
}
21 changes: 21 additions & 0 deletions src/serialize/json/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export class Factory {
return new OrganizationalContactNormalizer(this)
}

makeForPostalAddress (): PostalAddressNormalizer {
return new PostalAddressNormalizer(this)
}

makeForOrganizationalEntity (): OrganizationalEntityNormalizer {
return new OrganizationalEntityNormalizer(this)
}
Expand Down Expand Up @@ -360,14 +364,31 @@ export class OrganizationalContactNormalizer extends BaseJsonNormalizer<Models.O
}
}

export class PostalAddressNormalizer extends BaseJsonNormalizer<Models.PostalAddress> {
normalize (data: Models.PostalAddress, options: NormalizerOptions): Normalized.PostalAddress {
return {
country: data.country || undefined,
region: data.region || undefined,
locality: data.locality || undefined,
postOfficeBoxNumber: data.postOfficeBoxNumber || undefined,
postalCode: data.postalCode || undefined,
streetAddress: data.streetAddress || undefined
}
}
}

export class OrganizationalEntityNormalizer extends BaseJsonNormalizer<Models.OrganizationalEntity> {
normalize (data: Models.OrganizationalEntity, options: NormalizerOptions): Normalized.OrganizationalEntity {
const urls = normalizeStringableIter(
Array.from(data.url, (s) => escapeUri(s.toString())),
options
).filter(JsonSchema.isIriReference)
const address = data.address === undefined || this._factory.spec.version < SpecVersion.v1dot6
? undefined
: this._factory.makeForPostalAddress().normalize(data.address, options)
return {
name: data.name || undefined,
address,
url: urls.length > 0
? urls
: undefined,
Expand Down
10 changes: 10 additions & 0 deletions src/serialize/json/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,18 @@ export namespace Normalized {
phone?: string
}

export interface PostalAddress {
country?: string
region?: string
locality?: string
postOfficeBoxNumber?: string
postalCode?: string
streetAddress?: string
}

export interface OrganizationalEntity {
name?: string
address?: PostalAddress
url?: JsonSchema.IriReference[]
contact?: OrganizationalContact[]
}
Expand Down
29 changes: 28 additions & 1 deletion src/serialize/xml/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export class Factory {
return new OrganizationalContactNormalizer(this)
}

makeForPostalAddress (): PostalAddressNormalizer {
return new PostalAddressNormalizer(this)
}

makeForOrganizationalEntity (): OrganizationalEntityNormalizer {
return new OrganizationalEntityNormalizer(this)
}
Expand Down Expand Up @@ -457,18 +461,41 @@ export class OrganizationalContactNormalizer extends BaseXmlNormalizer<Models.Or
}
}

export class PostalAddressNormalizer extends BaseXmlNormalizer<Models.PostalAddress> {
normalize (data: Models.PostalAddress, options: NormalizerOptions, elementName: string): SimpleXml.Element {
return {
type: 'element',
name: elementName,
children: [
makeOptionalTextElement(data.country, 'country'),
makeOptionalTextElement(data.region, 'region'),
makeOptionalTextElement(data.locality, 'locality'),
makeOptionalTextElement(data.postOfficeBoxNumber, 'postOfficeBoxNumber'),
makeOptionalTextElement(data.postalCode, 'postalCode'),
makeOptionalTextElement(data.streetAddress, 'streetAddress')
].filter(isNotUndefined)
}
}
}

export class OrganizationalEntityNormalizer extends BaseXmlNormalizer<Models.OrganizationalEntity> {
normalize (data: Models.OrganizationalEntity, options: NormalizerOptions, elementName: string): SimpleXml.Element {
const address = data.address === undefined || this._factory.spec.version < SpecVersion.v1dot6
? undefined
: this._factory.makeForPostalAddress().normalize(data.address, options, 'address')
return {
type: 'element',
name: elementName,
children: [
makeOptionalTextElement(data.name, 'name', normalizedString),
address,
...makeTextElementIter(Array.from(
data.url, (s): string => escapeUri(s.toString())
), options, 'url'
).filter(({ children: u }) => XmlSchema.isAnyURI(u)),
...this._factory.makeForOrganizationalContact().normalizeIterable(data.contact, options, 'contact')
...(data.contact.size > 0
? this._factory.makeForOrganizationalContact().normalizeIterable(data.contact, options, 'contact')
: [])
].filter(isNotUndefined)
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/_data/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ function createComplexStructure () {
}),
manufacture: new Models.OrganizationalEntity({
name: 'meta manufacture',
address: new Models.PostalAddress({
country: 'United States',
region: 'Texas',
locality: 'Austin',
postOfficeBoxNumber: '901',
postalCode: '78758',
streetAddress: '100 Main Street'
}),
url: new Set([new URL('https://meta-manufacture.xmpl')])
}),
supplier: new Models.OrganizationalEntity({
Expand Down
8 changes: 8 additions & 0 deletions tests/_data/normalizeResults/json_sortedLists_spec1.6.json

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

8 changes: 8 additions & 0 deletions tests/_data/normalizeResults/json_sortedLists_spec1.7.json

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

36 changes: 36 additions & 0 deletions tests/_data/normalizeResults/xml_sortedLists_spec1.6.json

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

36 changes: 36 additions & 0 deletions tests/_data/normalizeResults/xml_sortedLists_spec1.7.json

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

8 changes: 8 additions & 0 deletions tests/_data/serializeResults/json_complex_spec1.6.json.bin

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

8 changes: 8 additions & 0 deletions tests/_data/serializeResults/json_complex_spec1.7.json.bin

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

Loading