Skip to content

Commit

Permalink
#1682: Non-deterministic definition of default namespace @XmlSchema(…
Browse files Browse the repository at this point in the history
…namespace=...) in package-info.java

Signed-off-by: Lukas Jungmann <lukas.jungmann@oracle.com>
(cherry picked from commit adeec65)
  • Loading branch information
lukasj committed Feb 1, 2023
1 parent 4ad29ec commit b655147
Showing 1 changed file with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand All @@ -10,16 +10,6 @@

package com.sun.tools.xjc.generator.bean;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
import javax.xml.namespace.QName;

import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JPackage;
import com.sun.tools.xjc.generator.annotation.spec.XmlSchemaWriter;
Expand All @@ -33,8 +23,17 @@
import com.sun.tools.xjc.model.CTypeRef;
import com.sun.tools.xjc.model.CValuePropertyInfo;
import com.sun.tools.xjc.model.Model;
import com.sun.tools.xjc.outline.PackageOutline;
import com.sun.tools.xjc.outline.Aspect;
import com.sun.tools.xjc.outline.PackageOutline;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

import javax.xml.namespace.QName;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/**
* {@link PackageOutline} enhanced with schema2java specific
Expand All @@ -48,7 +47,7 @@ public final class PackageOutlineImpl implements PackageOutline {
private final JPackage _package;
private final ObjectFactoryGenerator objectFactoryGenerator;

/*package*/ final Set<ClassOutlineImpl> classes = new HashSet<ClassOutlineImpl>();
/*package*/ final Set<ClassOutlineImpl> classes = new LinkedHashSet<>();
private final Set<ClassOutlineImpl> classesView = Collections.unmodifiableSet(classes);

private String mostUsedNamespaceURI;
Expand All @@ -64,6 +63,7 @@ public final class PackageOutlineImpl implements PackageOutline {
*
* @see #calcDefaultValues()
*/
@Override
public String getMostUsedNamespaceURI() {
return mostUsedNamespaceURI;
}
Expand All @@ -73,6 +73,7 @@ public String getMostUsedNamespaceURI() {
* <p>
* The value is computed by examining what would yield the smallest generated code.
*/
@Override
public XmlNsForm getAttributeFormDefault() {
assert attributeFormDefault!=null;
return attributeFormDefault;
Expand All @@ -83,23 +84,28 @@ public XmlNsForm getAttributeFormDefault() {
* <p>
* The value is computed by examining what would yield the smallest generated code.
*/
@Override
public XmlNsForm getElementFormDefault() {
assert elementFormDefault!=null;
return elementFormDefault;
}

@Override
public JPackage _package() {
return _package;
}

@Override
public ObjectFactoryGenerator objectFactoryGenerator() {
return objectFactoryGenerator;
}

@Override
public Set<ClassOutlineImpl> getClasses() {
return classesView;
}

@Override
public JDefinedClass objectFactory() {
return objectFactoryGenerator.getObjectFactory();
}
Expand Down Expand Up @@ -144,17 +150,20 @@ public Void onElement(CElementPropertyInfo p) {
return null;
}

@Override
public Void onReference(CReferencePropertyInfo p) {
for (CElement e : p.getElements()) {
countURI(propUriCountMap, e.getElementName());
}
return null;
}

@Override
public Void onAttribute(CAttributePropertyInfo p) {
return null;
}

@Override
public Void onValue(CValuePropertyInfo p) {
return null;
}
Expand All @@ -174,8 +183,7 @@ public Void onValue(CValuePropertyInfo p) {
elementFormDefault = getFormDefault();
attributeFormDefault = XmlNsForm.UNQUALIFIED;
try {
XmlNsForm modelValue = _model.getAttributeFormDefault(mostUsedNamespaceURI);
attributeFormDefault = modelValue;
attributeFormDefault = _model.getAttributeFormDefault(mostUsedNamespaceURI);
} catch (Exception e) {
// ignore and accept default
}
Expand All @@ -195,19 +203,18 @@ public Void onValue(CValuePropertyInfo p) {

// Map to keep track of how often each type or element uri is used in this package
// mostly used to calculate mostUsedNamespaceURI
private HashMap<String, Integer> uriCountMap = new HashMap<String, Integer>();
private Map<String, Integer> uriCountMap = new LinkedHashMap<>();

// Map to keep track of how often each property uri is used in this package
// used to calculate elementFormDefault
private HashMap<String, Integer> propUriCountMap = new HashMap<String, Integer>();
private Map<String, Integer> propUriCountMap = new LinkedHashMap<>();

/**
* pull the uri out of the specified QName and keep track of it in the
* specified hash map
*
* @param qname
*/
private void countURI(HashMap<String, Integer> map, QName qname) {
private void countURI(Map<String, Integer> map, QName qname) {
if (qname == null) return;

String uri = qname.getNamespaceURI();
Expand All @@ -229,7 +236,7 @@ private void countURI(HashMap<String, Integer> map, QName qname) {
* and when that happens it unintentionally also renames (normally
* unqualified) local elements, prefer non-"" URI when there's a tie.
*/
private String getMostUsedURI(HashMap<String, Integer> map) {
private String getMostUsedURI(Map<String, Integer> map) {
String mostPopular = null;
int count = 0;

Expand Down

0 comments on commit b655147

Please sign in to comment.