Sure, I can help you with that. Just let me know which part you’d like me to translate.

huangapple 未分类评论78阅读模式
英文:

I have a feature model (in xml format). Is there a way to generate java classes for any given feature model when a required feature is traversed?

问题

以下是您提供的内容的翻译部分:

我尝试使用JAXB读取XML文件,但它只读取了第一层元素。我也不知道如何生成XML文件中每个所需特性的Java类。这是我正在处理的样本特性模型以及我从读取文件中获得的输出。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<featureModel>
    <struct>
        <and abstract="true" mandatory="true" name="SvgMapApp">
            <feature name="L2Build"/>
            <and abstract="true" mandatory="true" name="Layers">
                <feature name="ColorRegion"/>
                <feature name="Relief"/>
                <feature name="Rivers"/>
                <feature name="Lakes"/>
                <feature name="PopCircle"/>
            </and>
            <and abstract="true" mandatory="true" name="Legends">
                <and abstract="true" name="Controls">
                    <feature name="Navigator"/>
                    <feature name="ReliefControls"/>
                    <feature name="RiverControls"/>
                    <feature name="LakeControls"/>
                    <feature name="PopCircleControls"/>
                    <feature name="CoordinateDisplay"/>
                </and>
                <and abstract="true" name="Stats1">
                    <feature name="AgeChart"/>
                    <feature name="StatsMedianAge"/>
                    <feature name="EthnicBarChart"/>
                    <feature name="EthnicPieChart"/>
                </and>
                <and abstract="true" name="Stats2">
                    <feature name="StatsSex"/>
                    <feature name="StatsHouseholds"/>
                    <feature name="StatsPopulation"/>
                </and>
                <feature hidden="true" name="Legend"/>
            </and>
            <feature hidden="true" mandatory="true" name="USStates"/>
            <feature mandatory="true" name="Base"/>
        </and>
    </struct>
    <!-- 约束规则部分已省略 -->
    <!-- 注释部分已省略 -->
</featureModel>

我想要自动生成模型类,但以下是我使用的类和获得的输出:

FeatureModel.java

public class FeatureModel {
    @XmlElement(name="struct")
    private Struct struct;

    public Struct getStruct() {
        return struct;
    }

    public void setStruct(Struct struct) {
        this.struct = struct;
    }
    // ...
}

Struct.java

public class Struct {
    @XmlElement(name="and")
    private And and[];

    public And[] getAnd() {
        return and;
    }

    public void setAnd(And[] and) {
        this.and = and;
    }
    // ...
}

Alt.java

public class Alt {
    @XmlElement(name="feature")
    private Feature[] feature;

    @XmlAttribute
    private String name;

    @XmlAttribute
    private String mandatory;
    // ...
}

And.java

public class And {
    @XmlAttribute
    private String name;

    @XmlAttribute
    private String mandatory;

    @XmlElement(name="feature")
    private Feature[] feature;

    @XmlElement(name="or")
    private Or or;

    @XmlElement(name="alt")
    private Alt alt;
    // ...
}

Or.java

public class Or {
    @XmlElement(name="feature")
    private Feature[] feature;

    @XmlAttribute
    private String name;
    // ...
}

Feature.java

public class Feature {
    @XmlAttribute
    private String name;
    // ...
}

这是我得到的输出:

FeatureModel [struct = Struct [and = [And Root = SvgMapApp, mandatory = true, feature = [Feature [name = L2Build], Feature [name = USStates], Feature [name = Base]] [alt = null, or = null]]]]
英文:

I have tried reading the XML file using JAXB but it only reads the first-level elements. I also do not know how to go about generating the java classes for each of the required features in the XML file. Here is a sample feature model that I am working with and the output I have obtained from reading the file.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;
    	&lt;featureModel&gt;
    		&lt;struct&gt;
    			&lt;and abstract=&quot;true&quot; mandatory=&quot;true&quot; name=&quot;SvgMapApp&quot;&gt;
    				&lt;feature name=&quot;L2Build&quot;/&gt;
    				&lt;and abstract=&quot;true&quot; mandatory=&quot;true&quot; name=&quot;Layers&quot;&gt;
    					&lt;feature name=&quot;ColorRegion&quot;/&gt;
    					&lt;feature name=&quot;Relief&quot;/&gt;
    					&lt;feature name=&quot;Rivers&quot;/&gt;
    					&lt;feature name=&quot;Lakes&quot;/&gt;
    					&lt;feature name=&quot;PopCircle&quot;/&gt;
    				&lt;/and&gt;
    				&lt;and abstract=&quot;true&quot; mandatory=&quot;true&quot; name=&quot;Legends&quot;&gt;
    					&lt;and abstract=&quot;true&quot; name=&quot;Controls&quot;&gt;
    						&lt;feature name=&quot;Navigator&quot;/&gt;
    						&lt;feature name=&quot;ReliefControls&quot;/&gt;
    						&lt;feature name=&quot;RiverControls&quot;/&gt;
    						&lt;feature name=&quot;LakeControls&quot;/&gt;
    						&lt;feature name=&quot;PopCircleControls&quot;/&gt;
    						&lt;feature name=&quot;CoordinateDisplay&quot;/&gt;
    					&lt;/and&gt;
    					&lt;and abstract=&quot;true&quot; name=&quot;Stats1&quot;&gt;
    						&lt;feature name=&quot;AgeChart&quot;/&gt;
    						&lt;feature name=&quot;StatsMedianAge&quot;/&gt;
    						&lt;feature name=&quot;EthnicBarChart&quot;/&gt;
    						&lt;feature name=&quot;EthnicPieChart&quot;/&gt;
    					&lt;/and&gt;
    					&lt;and abstract=&quot;true&quot; name=&quot;Stats2&quot;&gt;
    						&lt;feature name=&quot;StatsSex&quot;/&gt;
    						&lt;feature name=&quot;StatsHouseholds&quot;/&gt;
    						&lt;feature name=&quot;StatsPopulation&quot;/&gt;
    					&lt;/and&gt;
    					&lt;feature hidden=&quot;true&quot; name=&quot;Legend&quot;/&gt;
    				&lt;/and&gt;
    				&lt;feature hidden=&quot;true&quot; mandatory=&quot;true&quot; name=&quot;USStates&quot;/&gt;
    				&lt;feature mandatory=&quot;true&quot; name=&quot;Base&quot;/&gt;
    			&lt;/and&gt;
    		&lt;/struct&gt;
    		&lt;constraints&gt;
    			&lt;rule&gt;
    				&lt;imp&gt;
    					&lt;var&gt;PopCircleControls&lt;/var&gt;
    					&lt;var&gt;PopCircle&lt;/var&gt;
    				&lt;/imp&gt;
    			&lt;/rule&gt;
    			&lt;rule&gt;
    				&lt;imp&gt;
    					&lt;var&gt;ReliefControls&lt;/var&gt;
    					&lt;var&gt;Relief&lt;/var&gt;
    				&lt;/imp&gt;
    			&lt;/rule&gt;
    			&lt;rule&gt;
    				&lt;imp&gt;
    					&lt;var&gt;RiverControls&lt;/var&gt;
    					&lt;var&gt;Rivers&lt;/var&gt;
    				&lt;/imp&gt;
    			&lt;/rule&gt;
    			&lt;rule&gt;
    				&lt;imp&gt;
    					&lt;var&gt;LakeControls&lt;/var&gt;
    					&lt;var&gt;Lakes&lt;/var&gt;
    				&lt;/imp&gt;
    			&lt;/rule&gt;
    			&lt;rule&gt;
    				&lt;imp&gt;
    					&lt;var&gt;Controls&lt;/var&gt;
    					&lt;var&gt;Legend&lt;/var&gt;
    				&lt;/imp&gt;
    			&lt;/rule&gt;
    			&lt;rule&gt;
    				&lt;imp&gt;
    					&lt;var&gt;Stats1&lt;/var&gt;
    					&lt;var&gt;Legend&lt;/var&gt;
    				&lt;/imp&gt;
    			&lt;/rule&gt;
    			&lt;rule&gt;
    				&lt;imp&gt;
    					&lt;var&gt;Stats2&lt;/var&gt;
    					&lt;var&gt;Legend&lt;/var&gt;
    				&lt;/imp&gt;
    			&lt;/rule&gt;
    			&lt;rule&gt;
    				&lt;imp&gt;
    					&lt;var&gt;L2Build&lt;/var&gt;
    					&lt;not&gt;
    						&lt;var&gt;EthnicBarChart&lt;/var&gt;
    					&lt;/not&gt;
    				&lt;/imp&gt;
    			&lt;/rule&gt;
    		&lt;/constraints&gt;
    		&lt;comments&gt;
    			&lt;c&gt; this turns panel a off&lt;/c&gt;
    			&lt;c&gt; this turns panel a off&lt;/c&gt;
    			&lt;c&gt;Layers { tab }&lt;/c&gt;
    			&lt;c&gt;Legends { out=&quot;&quot; }&lt;/c&gt;
    			&lt;c&gt;Controls { tab }&lt;/c&gt;
    			&lt;c&gt;Stats1 { tab }&lt;/c&gt;
    			&lt;c&gt;Stats2 { tab }&lt;/c&gt;
    		&lt;/comments&gt;
    	&lt;/featureModel&gt;

I would like to autogenerate the model classes, but here are the classes I have worked with and the output I obtained:

FeatureModel.java

public class FeatureModel {
	
	 @XmlElement(name=&quot;struct&quot;)
	 private Struct struct;

	   

	    public Struct getStruct ()
	    {
	        return struct;
	    }

	    public void setStruct (Struct struct)
	    {
	        this.struct = struct;
	    }

	   .....
	    
}

Struct.java

public class Struct {
	
    @XmlElement(name=&quot;and&quot;)

	private And and[];
	
    public And[] getAnd ()
    {
        return and;
    }

    public void setAnd (And[] and)
    {
        this.and = and;
    }

    .........
    
	
}

Alt.java

public class Alt {
	@XmlElement(name=&quot;feature&quot;)
	private Feature[] feature;
	
	@XmlAttribute
    private String name;

   
	@XmlAttribute
    private String mandatory;

    public Feature[] getFeature ()
    {
        return feature;
    }

    public void setFeature (Feature[] feature)
    {
        this.feature = feature;
    }

    public String getName ()
    {
        return name;
    }

    public void setName (String name)
    {
        this.name = name;
    }

    
    public String getMandatory ()
    {
        return mandatory;
    }

    public void setMandatory (String mandatory)
    {
        this.mandatory = mandatory;
    }

    ...........
}

And.java

public class And {
	
	@XmlAttribute
    private String name;
	
	@XmlAttribute
    private String mandatory;
	

	@XmlElement(name=&quot;feature&quot;)
	private Feature[] feature;
	 
	@XmlElement(name=&quot;or&quot;)
	private Or or;

	@XmlElement(name=&quot;alt&quot;)
    private Alt alt;
	
	public String getName ()
	    {
	        return name;
	    }

	public void setName (String name)
	    {
	        this.name = name;
	    }
	public Feature[] getFeature ()
	    {
	        return feature;
	    }

	public void setFeature (Feature[] feature)
	    {
	        this.feature = feature;
	    }

    public Or getOr ()
    {
        return or;
    }

    public void setOr (Or or)
    {
        this.or = or;
    }

    public Alt getAlt ()
    {
        return alt;
    }

    public void setAlt (Alt alt)
    {
        this.alt = alt;
    }

  
    public String getMandatory ()
    {
        return mandatory;
    }

    public void setMandatory (String mandatory)
    {
        this.mandatory = mandatory;
    }

    .......
    	
}

Or.java

public class Or {
	
	@XmlElement(name=&quot;feature&quot;)
	private Feature[] feature;
	
	@XmlAttribute
    private String name;

    public Feature[] getFeature ()
    {
        return feature;
    }

    public void setFeature (Feature[] feature)
    {
        this.feature = feature;
    }

    public String getName ()
    {
        return name;
    }

    public void setName (String name)
    {
        this.name = name;
    }

    ......
}

Feature.java

public class Feature {
	
	@XmlAttribute
	 private String name;

	    public String getName ()
	    {
	        return name;
	    }

	    public void setName (String name)
	    {
	        this.name = name;
	    }

	    ...............
}

Here is my output:
FeatureModel [struct = Struct [and = [And Root = SvgMapApp, mandatory = true, feature = [Feature [name = L2Build], Feature [name = USStates], Feature [name = Base]] [alt = null, or = null]]]]

答案1

得分: 0

Sure, here's the translated Java code part you provided:

public class And {
    
    @XmlAttribute
    private String name;
    
    @XmlAttribute
    private String mandatory;
    
    @XmlElement(name="feature")
    private Feature[] feature;
     
    @XmlElement(name="or")
    private Or or;

    @XmlElement(name="alt")
    private Alt alt;

    // added
    @XmlElement(name="and")
    private And[] and;
}

Let me know if you need anything else!

英文:

As for adjusting the generated Java code, look at what exactly each element can contain and what is missing. For instance, the and elements in the xml contain either features or ands (or both); in the And class, only a member for a list of Features is present, so add the ands manually.

public class And {
    
    @XmlAttribute
    private String name;
    
    @XmlAttribute
    private String mandatory;
    

    @XmlElement(name=&quot;feature&quot;)
    private Feature[] feature;
     
    @XmlElement(name=&quot;or&quot;)
    private Or or;

    @XmlElement(name=&quot;alt&quot;)
    private Alt alt;

    // added
    @XmlElement(name=&quot;and&quot;)
    private And[] and;
}

This should also cover the ands in the deeper nested levels. Looking at your output, the ands are exactly what are missing. I would also assume several ors and alts are possible (so those should be array fields), but those aren't present in the xml at all, so I guess they come from another file - you'll have to see for yourself how they fit in.

The feature seems to always have a name, but also a hidden and mandatory attribute sometimes, so add those to Feature.

huangapple
  • 本文由 发表于 2020年8月14日 20:09:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/63412501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定