Skip to content

Commit

Permalink
feat(xjc): call postProcessModel with DTD models
Browse files Browse the repository at this point in the history
When code is generated from DTD schema, the hook function
`postProcessModel` was not called. The code to do so was moved
from global utility class to `BGMBuilder` class in the old code more
than five years ago with the effect, that the hook was not called
anymore for any other input than XSD.

A Junit 4 test is included.

Signed-off-by: Nikolaus Rosenmayr <nr@firecoder.com>
  • Loading branch information
nros authored and lukasj committed Sep 5, 2023
1 parent 7188568 commit 1116d50
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.sun.tools.xjc.AbortException;
import com.sun.tools.xjc.ErrorReceiver;
import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.Plugin;
import com.sun.tools.xjc.model.CAttributePropertyInfo;
import com.sun.tools.xjc.model.CBuiltinLeafInfo;
import com.sun.tools.xjc.model.CClassInfo;
Expand Down Expand Up @@ -107,6 +108,11 @@ public static Model parse(

Ring.get(ModelChecker.class).check();

if( opts.activePlugins!=null ) {
for (Plugin ma : opts.activePlugins)
ma.postProcessModel(model, Ring.get(ErrorReceiver.class));
}

if(ef.hadError()) return null;
else return model;
} finally {
Expand Down
84 changes: 84 additions & 0 deletions jaxb-ri/xjc/src/test/java/com/sun/tools/xjc/XjcDtdPluginTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 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
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

package com.sun.tools.xjc;

import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.Driver.OptionsEx;
import com.sun.tools.xjc.model.Model;
import com.sun.tools.xjc.outline.Outline;
import com.sun.tools.xjc.util.ErrorReceiverFilter;
import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

public class XjcDtdPluginTest {
@Test
public void testCallPostProcessModelOnPlugin() {
final String dtdSchema = "<!ELEMENT hello (#PCDATA)>";

final OptionsEx opt = new OptionsEx();
opt.setSchemaLanguage(Language.DTD);
opt.compatibilityMode = Options.EXTENSION;

opt.addGrammar(
new InputSource(
new ByteArrayInputStream(
dtdSchema.getBytes(StandardCharsets.UTF_8)
)
)
);

final Boolean[] wasPostProcessModelHookCalled = new Boolean[1];
wasPostProcessModelHookCalled[0] = Boolean.FALSE;

opt.activePlugins.add(new Plugin() {
@Override
public String getOptionName() {
return "XdebugPostProcessModel";
}

@Override
public String getUsage() {
return null;
}

@Override
public boolean run(
Outline outline,
Options opt,
ErrorHandler errorHandler) throws SAXException {
return true;
}

@Override
public void postProcessModel(Model model, ErrorHandler errorHandler) {
wasPostProcessModelHookCalled[0] = Boolean.TRUE;
super.postProcessModel(model, errorHandler);
}
});

ModelLoader.load(
opt,
new JCodeModel(),
new ErrorReceiverFilter()
);

Assert.assertTrue(
"DTD model did not call postProcessModel hook of the plugin",
wasPostProcessModelHookCalled[0]
);
}
}

0 comments on commit 1116d50

Please sign in to comment.