@@ -145,13 +145,7 @@ public PackageURL(final String purl) throws MalformedPackageURLException {
145145 } else {
146146 this .subpath = null ;
147147 }
148- // qualifiers are optional - check for existence
149- final String rawQuery = uri .getRawQuery ();
150- if (rawQuery != null && !rawQuery .isEmpty ()) {
151- this .qualifiers = parseQualifiers (rawQuery );
152- } else {
153- this .qualifiers = null ;
154- }
148+
155149 // this is the rest of the purl that needs to be parsed
156150 String remainder = uri .getRawPath ();
157151 // trim trailing '/'
@@ -169,6 +163,14 @@ public PackageURL(final String purl) throws MalformedPackageURLException {
169163 }
170164 this .type = StringUtil .toLowerCase (validateType (remainder .substring (start , index )));
171165
166+ // qualifiers are optional - check for existence
167+ final String rawQuery = uri .getRawQuery ();
168+ if (rawQuery != null && !rawQuery .isEmpty ()) {
169+ this .qualifiers = parseQualifiers (this .type , rawQuery );
170+ } else {
171+ this .qualifiers = null ;
172+ }
173+
172174 start = index + 1 ;
173175
174176 // version is optional - check for existence
@@ -231,7 +233,7 @@ public PackageURL(
231233 throws MalformedPackageURLException {
232234 this .type = StringUtil .toLowerCase (validateType (requireNonNull (type , "type" )));
233235 this .namespace = validateNamespace (this .type , namespace );
234- this .qualifiers = parseQualifiers (qualifiers );
236+ this .qualifiers = parseQualifiers (this . type , qualifiers );
235237 this .name = validateName (this .type , requireNonNull (name , "name" ), this .qualifiers );
236238 this .version = validateVersion (this .type , version );
237239 this .subpath = validateSubpath (subpath );
@@ -456,7 +458,7 @@ private static String validateMlflowName(final String name, final Map<String,Str
456458 }
457459 }
458460
459- private static @ Nullable Map <String , String > validateQualifiers (final @ Nullable Map <String , String > values )
461+ private static @ Nullable Map <String , String > validateQualifiers (final String type , final @ Nullable Map <String , String > values )
460462 throws MalformedPackageURLException {
461463 if (values == null || values .isEmpty ()) {
462464 return null ;
@@ -467,6 +469,22 @@ private static String validateMlflowName(final String name, final Map<String,Str
467469 validateKey (key );
468470 validateValue (key , entry .getValue ());
469471 }
472+
473+ switch (type ) {
474+ case StandardTypes .BAZEL :
475+ String defaultRegistry = "https://bcr.bazel.build" ;
476+ String repoURL = values .get ("repository_url" );
477+ String normalized = repoURL .toLowerCase ();
478+ if (normalized .endsWith ("/" )) {
479+ normalized = normalized .substring (0 , normalized .length () - 1 );
480+ }
481+
482+ if (normalized .equals (defaultRegistry )){
483+ values .remove ("repository_url" );
484+ }
485+ break ;
486+ }
487+
470488 return values ;
471489 }
472490
@@ -593,7 +611,7 @@ private static void verifyTypeConstraints(String type, @Nullable String namespac
593611 }
594612 }
595613
596- private static @ Nullable Map <String , String > parseQualifiers (final @ Nullable Map <String , String > qualifiers )
614+ private static @ Nullable Map <String , String > parseQualifiers (final String type , final @ Nullable Map <String , String > qualifiers )
597615 throws MalformedPackageURLException {
598616 if (qualifiers == null || qualifiers .isEmpty ()) {
599617 return null ;
@@ -606,14 +624,14 @@ private static void verifyTypeConstraints(String type, @Nullable String namespac
606624 TreeMap ::new ,
607625 (map , value ) -> map .put (StringUtil .toLowerCase (value .getKey ()), value .getValue ()),
608626 TreeMap ::putAll );
609- return validateQualifiers (results );
627+ return validateQualifiers (type , results );
610628 } catch (ValidationException ex ) {
611629 throw new MalformedPackageURLException (ex .getMessage ());
612630 }
613631 }
614632
615633 @ SuppressWarnings ("StringSplitter" ) // reason: surprising behavior is okay in this case
616- private static @ Nullable Map <String , String > parseQualifiers (final String encodedString )
634+ private static @ Nullable Map <String , String > parseQualifiers (final String type , final String encodedString )
617635 throws MalformedPackageURLException {
618636 try {
619637 final TreeMap <String , String > results = Arrays .stream (encodedString .split ("&" ))
@@ -631,7 +649,7 @@ private static void verifyTypeConstraints(String type, @Nullable String namespac
631649 }
632650 },
633651 TreeMap ::putAll );
634- return validateQualifiers (results );
652+ return validateQualifiers (type , results );
635653 } catch (ValidationException e ) {
636654 throw new MalformedPackageURLException (e );
637655 }
@@ -746,6 +764,12 @@ public static final class StandardTypes {
746764 * @since 2.0.0
747765 */
748766 public static final String APK = "apk" ;
767+ /**
768+ * Bazel-based packages.
769+ *
770+ * @since 2.0.0
771+ */
772+ public static final String BAZEL = "bazel" ;
749773 /**
750774 * Bitbucket-based packages.
751775 */
0 commit comments