All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xuml.tools.diagram.ClassDiagramGenerator Maven / Gradle / Ivy

package xuml.tools.diagram;

import static com.google.common.base.Optional.absent;
import static com.google.common.base.Optional.of;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBElement;

import org.apache.commons.io.IOUtils;

import xuml.tools.miuml.metamodel.jaxb.AssociativeReference;
import xuml.tools.miuml.metamodel.jaxb.Attribute;
import xuml.tools.miuml.metamodel.jaxb.BinaryAssociation;
import xuml.tools.miuml.metamodel.jaxb.Class;
import xuml.tools.miuml.metamodel.jaxb.DerivedAttribute;
import xuml.tools.miuml.metamodel.jaxb.Domain;
import xuml.tools.miuml.metamodel.jaxb.Domains;
import xuml.tools.miuml.metamodel.jaxb.Event;
import xuml.tools.miuml.metamodel.jaxb.Generalization;
import xuml.tools.miuml.metamodel.jaxb.IdentifierAttribute;
import xuml.tools.miuml.metamodel.jaxb.IndependentAttribute;
import xuml.tools.miuml.metamodel.jaxb.ModeledDomain;
import xuml.tools.miuml.metamodel.jaxb.Named;
import xuml.tools.miuml.metamodel.jaxb.Perspective;
import xuml.tools.miuml.metamodel.jaxb.Reference;
import xuml.tools.miuml.metamodel.jaxb.ReferentialAttribute;
import xuml.tools.miuml.metamodel.jaxb.Relationship;
import xuml.tools.miuml.metamodel.jaxb.Subsystem;
import xuml.tools.miuml.metamodel.jaxb.SubsystemElement;
import xuml.tools.miuml.metamodel.jaxb.UnaryAssociation;

import com.google.common.base.Optional;

public class ClassDiagramGenerator {

    private String placeInTemplate(String divs, Optional viewJson) {
        try {
            String template = IOUtils.toString(ClassDiagramGenerator.class
                    .getResourceAsStream("/class-diagram-template.html"));
            return template.replace("${xuml.divs}", divs).replace("${view.json}",
                    viewJson.or("{}"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private Optional getSubsystem(Domains domains, String domainName,
            String subsystemName) {
        for (JAXBElement domain : domains.getDomain()) {
            if (domain.getValue() instanceof ModeledDomain
                    && domain.getValue().getName().equals(domainName)) {
                ModeledDomain md = (ModeledDomain) domain.getValue();
                for (Subsystem ss : md.getSubsystem())
                    if (ss.getName().equals(subsystemName))
                        return of(ss);
            }
        }
        return absent();
    }

    public String generate(Domains domains, String domainName, String subsystemName,
            Optional viewJson) {
        return placeInTemplate(generateDivs(getSubsystem(domains, domainName, subsystemName)),
                viewJson);
    }

    public String generate(Domains domains, int domainIndex, int ssIndex,
            Optional viewJson) {
        if (domains.getDomain().isEmpty())
            return placeInTemplate("", Optional. absent());
        ModeledDomain md = ((ModeledDomain) domains.getDomain().get(domainIndex).getValue());
        if (md.getSubsystem().isEmpty())
            return placeInTemplate("", Optional. absent());
        Subsystem ss = md.getSubsystem().get(ssIndex);
        return generate(domains, md.getName(), ss.getName(), viewJson);
    }

    private String generateDivs(Optional subsystem) {
        if (!subsystem.isPresent())
            return "";

        StringBuilder s = new StringBuilder();
        for (JAXBElement element : subsystem.get()
                .getSubsystemElement())
            if (element.getValue() instanceof xuml.tools.miuml.metamodel.jaxb.Class)
                generateClass(s, (xuml.tools.miuml.metamodel.jaxb.Class) element.getValue());
            else if (element.getValue() instanceof Relationship) {
                Relationship r = (Relationship) element.getValue();
                if (r instanceof BinaryAssociation)
                    generateAssociation(s, (BinaryAssociation) r, subsystem.get());
                else if (r instanceof UnaryAssociation)
                    generateAssociation(s, (UnaryAssociation) r, subsystem.get());
                else if (r instanceof Generalization)
                    generateGeneralization(s, (Generalization) r);
            }
        return s.toString();
    }

    private void generateGeneralization(StringBuilder s, Generalization g) {
        for (Named sp : g.getSpecializedClass())
            s.append("
\n"); } private String replaceSpaces(String s) { return s.replaceAll(" ", "_"); } private void generateAssociation(StringBuilder s, BinaryAssociation r, Subsystem ss) { if (r.getActivePerspective().getViewedClass() .equals(r.getPassivePerspective().getViewedClass())) { System.out.println("binary association involving one class only not yet supported"); return; } s.append( "
\n"); } private void generateAssociation(StringBuilder s, UnaryAssociation r, Subsystem subsystem) { // TODO implement unary associations } private String getMultiplicityAbbreviation(Perspective p) { if (p.isConditional() && p.isOnePerspective()) return "0..1"; else if (!p.isConditional() && p.isOnePerspective()) return "1"; else if (p.isConditional() && !p.isOnePerspective()) return "*"; else return "1..*"; } private static String getIdentifierName(BigInteger i) { if (i.intValue() == 1) return "I"; else return "I" + i; } private static String getRelationshipName(BigInteger n) { return "R" + n; } private void generateClass(StringBuilder s, xuml.tools.miuml.metamodel.jaxb.Class cls) { // System.out.println("class=" + cls.getName()); s.append("
\n"); s.append("
\n"); for (JAXBElement attr : cls.getAttribute()) { // System.out.println("attribute=" + attr.getValue().getName()); List items = new ArrayList(); for (IdentifierAttribute id : attr.getValue().getIdentifier()) items.add(getIdentifierName(id.getNumber())); if (attr.getValue() instanceof IndependentAttribute) { IndependentAttribute a = (IndependentAttribute) attr.getValue(); s.append("
" + a.getName() + ": " + a.getType() + " "); // if (!a.isMandatory()) // items.add("O"); } else if (attr.getValue() instanceof ReferentialAttribute) { ReferentialAttribute r = (ReferentialAttribute) attr.getValue(); Reference ref = r.getReference().getValue(); items.add(getRelationshipName(ref.getRelationship())); s.append("
" + r.getName() + ": "); } else if (attr.getValue() instanceof DerivedAttribute) { DerivedAttribute d = (DerivedAttribute) attr.getValue(); s.append("
" + "/ " + d.getName() + ": " + d.getType()); } StringBuilder b = new StringBuilder(); for (String item : items) { if (b.length() > 0) b.append(","); b.append(item); } if (b.length() > 0) s.append("{" + b + "}"); s.append("
\n"); } s.append("
\n"); // if (!cls.getOperation().isEmpty()) { // s.append("
"); // for (Operation op : cls.getOperation()) { // s.append("
"); // s.append(op.getName() + "("); // boolean first = true; // for (OperationParameter p : op.getParameter()) { // if (!first) // s.append(","); // s.append(p.getName()); // first = false; // } // s.append(")"); // s.append("
"); // } // s.append("
"); // } if (cls.getLifecycle() != null && !cls.getLifecycle().getEvent().isEmpty()) { s.append("
"); for (JAXBElement event : cls.getLifecycle().getEvent()) { s.append("
"); s.append(event.getValue().getName()); s.append("
"); } s.append("
"); } s.append("
\n"); } private BigInteger getAssociationClassRelationship(Class cls) { for (JAXBElement element : cls.getAttribute()) if (element.getValue() instanceof ReferentialAttribute) { ReferentialAttribute ref = (ReferentialAttribute) element.getValue(); if (ref.getReference().getValue() instanceof AssociativeReference) { return ref.getReference().getValue().getRelationship(); } } return null; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy