Skip to content

Commit

Permalink
Added new identifier type "peppol-lax"; see
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Apr 24, 2024
1 parent 43c31db commit fcf6e87
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 30 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ They depend on several other libraries so I suggest you are going for the Maven

# News and noteworthy

* v9.3.6 - 2024-04-24
* Added new class `PeppolLaxIdentifierFactory`
* Added new enum entry `ESMPIdentifierType.PEPPOL_LAX` for SMP handling. See [smp#275](https://github.com/phax/phoss-smp/issues/275).
* v9.3.5 - 2024-04-23
* Updated to OpenPeppol eDEC Code Lists v8.8
* v9.3.4 - 2024-04-04
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public enum ESMPIdentifierType implements IHasID <String>, IHasDisplayName
{
SIMPLE ("simple", "Simple", SimpleIdentifierFactory.INSTANCE),
PEPPOL ("peppol", "Peppol", PeppolIdentifierFactory.INSTANCE),
PEPPOL_LAX ("peppol-lax", "Peppol (lax)", PeppolLaxIdentifierFactory.INSTANCE),
BDXR1 ("bdxr1", "OASIS BDXR v1", BDXR1IdentifierFactory.INSTANCE),
BDXR2 ("bdxr2", "OASIS BDXR v2", BDXR2IdentifierFactory.INSTANCE);

Expand Down Expand Up @@ -78,7 +79,8 @@ public static ESMPIdentifierType getFromIDOrNull (@Nullable final String sID)
}

@Nullable
public static ESMPIdentifierType getFromIDOrDefault (@Nullable final String sID, @Nullable final ESMPIdentifierType eDefault)
public static ESMPIdentifierType getFromIDOrDefault (@Nullable final String sID,
@Nullable final ESMPIdentifierType eDefault)
{
// Legacy ID
if ("bdxr".equals (sID))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,18 @@ public class PeppolIdentifierFactory implements IIdentifierFactory
/** Global instance to be used. */
public static final PeppolIdentifierFactory INSTANCE = new PeppolIdentifierFactory ();

private final boolean m_bStrict;

public PeppolIdentifierFactory ()
{}
{
// Always strict
this (true);
}

protected PeppolIdentifierFactory (final boolean bStrict)
{
m_bStrict = bStrict;
}

@Override
public boolean isDocumentTypeIdentifierSchemeMandatory ()
Expand Down Expand Up @@ -114,41 +124,44 @@ public boolean isDocumentTypeIdentifierValueValid (@Nullable final String sSchem
// at least 1 char
if (nLength == 0)
return false;

// <= 500 chars
if (nLength > PeppolIdentifierHelper.MAX_DOCUMENT_TYPE_VALUE_LENGTH)
return false;

// Check if the value is ISO-8859-1 encoded
if (!PeppolIdentifierHelper.areCharsetChecksDisabled ())
if (!StandardCharsets.ISO_8859_1.newEncoder ().canEncode (sValue))
return false;

try
{
final IPeppolDocumentTypeIdentifierParts aParts = PeppolDocumentTypeIdentifierParts.extractFromString (sValue);

if (sScheme != null)
switch (sScheme)
{
case PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_BUSDOX_DOCID_QNS:
// POLICY 17
if (!isValidCustomizationIDBusdoxDocidQns (aParts.getCustomizationID ()))
return false;
break;
case PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_PEPPOL_DOCTYPE_WILDCARD:
// Chapter 5.1.2
if (!isValidCustomizationIDPeppolDoctypeWildcard (aParts.getCustomizationID ()))
return false;
break;
default:
// Ignore - no special Peppol rules
}

}
catch (final IllegalArgumentException ex)
{
// Syntax error - not valid
return false;
}
if (m_bStrict)
try
{
final IPeppolDocumentTypeIdentifierParts aParts = PeppolDocumentTypeIdentifierParts.extractFromString (sValue);

if (sScheme != null)
switch (sScheme)
{
case PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_BUSDOX_DOCID_QNS:
// POLICY 17
if (!isValidCustomizationIDBusdoxDocidQns (aParts.getCustomizationID ()))
return false;
break;
case PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_PEPPOL_DOCTYPE_WILDCARD:
// Chapter 5.1.2
if (!isValidCustomizationIDPeppolDoctypeWildcard (aParts.getCustomizationID ()))
return false;
break;
default:
// Ignore - no special Peppol rules
}

}
catch (final IllegalArgumentException ex)
{
// Syntax error - not valid
return false;
}

return true;
}
Expand Down Expand Up @@ -320,6 +333,6 @@ public PeppolProcessIdentifier createProcessIdentifier (@Nullable final String s
@Override
public String toString ()
{
return new ToStringGenerator (this).getToString ();
return new ToStringGenerator (this).append ("Strict", m_bStrict).getToString ();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2015-2024 Philip Helger
* philip[at]helger[dot]com
*
* 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.
*/
package com.helger.peppolid.factory;

/**
* Specific implementation of {@link IIdentifierFactory} for lax handling of
* Peppol identifiers. See https://github.com/phax/phoss-smp/issues/275 for
* background information.
*
* @author Philip Helger
* @since 9.3.6
*/
public class PeppolLaxIdentifierFactory extends PeppolIdentifierFactory
{
/** Global instance to be used. */
@SuppressWarnings ("hiding")
public static final PeppolLaxIdentifierFactory INSTANCE = new PeppolLaxIdentifierFactory ();

public PeppolLaxIdentifierFactory ()
{
super (false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,44 @@ public void testIsValidDocumentTypeIdentifierValue ()
assertTrue (PeppolIdentifierFactory.INSTANCE.isDocumentTypeIdentifierValueValid (PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_PEPPOL_DOCTYPE_WILDCARD,
"urn:rootnamespace::localelement##customizationid*::version"));
}

@Test
public void testIsValidDocumentTypeIdentifierValueLax ()
{
assertFalse (PeppolLaxIdentifierFactory.INSTANCE.isDocumentTypeIdentifierValueValid (PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_BUSDOX_DOCID_QNS,
null));
assertFalse (PeppolLaxIdentifierFactory.INSTANCE.isDocumentTypeIdentifierValueValid (PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_BUSDOX_DOCID_QNS,
""));

// Difference to the regular one
assertTrue (PeppolLaxIdentifierFactory.INSTANCE.isDocumentTypeIdentifierValueValid (PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_BUSDOX_DOCID_QNS,
"invoice"));
// Difference to the regular one
assertTrue (PeppolLaxIdentifierFactory.INSTANCE.isDocumentTypeIdentifierValueValid (PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_PEPPOL_DOCTYPE_WILDCARD,
"invoice"));
// Difference to the regular one
assertTrue (PeppolLaxIdentifierFactory.INSTANCE.isDocumentTypeIdentifierValueValid ("bla", "invoice"));
// Difference to the regular one
assertTrue (PeppolLaxIdentifierFactory.INSTANCE.isDocumentTypeIdentifierValueValid (null, "invoice"));
assertTrue (PeppolLaxIdentifierFactory.INSTANCE.isDocumentTypeIdentifierValueValid (PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_BUSDOX_DOCID_QNS,
"order "));

// Difference to the regular one
assertTrue (PeppolLaxIdentifierFactory.INSTANCE.isDocumentTypeIdentifierValueValid (PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_BUSDOX_DOCID_QNS,
StringHelper.getRepeated ('a',
PeppolIdentifierHelper.MAX_DOCUMENT_TYPE_VALUE_LENGTH)));
assertFalse (PeppolLaxIdentifierFactory.INSTANCE.isDocumentTypeIdentifierValueValid (PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_BUSDOX_DOCID_QNS,
StringHelper.getRepeated ('a',
PeppolIdentifierHelper.MAX_DOCUMENT_TYPE_VALUE_LENGTH +
1)));
assertTrue (PeppolLaxIdentifierFactory.INSTANCE.isDocumentTypeIdentifierValueValid (PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_BUSDOX_DOCID_QNS,
"urn:rootnamespace::localelement##customizationid::version"));

// * only valid for peppol-doctype-wildcard
// Difference to the regular one
assertTrue (PeppolLaxIdentifierFactory.INSTANCE.isDocumentTypeIdentifierValueValid (PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_BUSDOX_DOCID_QNS,
"urn:rootnamespace::localelement##customizationid*::version"));
assertTrue (PeppolLaxIdentifierFactory.INSTANCE.isDocumentTypeIdentifierValueValid (PeppolIdentifierHelper.DOCUMENT_TYPE_SCHEME_PEPPOL_DOCTYPE_WILDCARD,
"urn:rootnamespace::localelement##customizationid*::version"));
}
}

0 comments on commit fcf6e87

Please sign in to comment.