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

org.openmdx.application.mof.mapping.xmi.Uml2ModelMapper Maven / Gradle / Ivy

There is a newer version: 2.18.10
Show newest version
/*
 * ====================================================================
 * Project:     openMDX/Core, http://www.openmdx.org/
 * Description: Uml2ModelMapper
 * Owner:       OMEX AG, Switzerland, http://www.omex.ch
 * ====================================================================
 *
 * This software is published under the BSD license as listed below.
 * 
 * Copyright (c) 2004-2009, OMEX AG, Switzerland
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 * 
 * * Neither the name of the openMDX team nor the names of its
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * ------------------
 * 
 * This product includes software developed by the Apache Software
 * Foundation (http://www.apache.org/).
 */

package org.openmdx.application.mof.mapping.xmi;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.openmdx.base.exception.ServiceException;
import org.openmdx.base.mof.cci.AggregationKind;
import org.openmdx.base.mof.cci.ModelElement_1_0;
import org.openmdx.base.mof.cci.Multiplicity;
import org.openmdx.base.mof.cci.Stereotypes;
import org.openmdx.base.text.conversion.HtmlEncoder;
import org.openmdx.base.text.conversion.UUIDConversion;
import org.openmdx.kernel.id.UUIDs;

@SuppressWarnings({"rawtypes","unchecked"})
public class Uml2ModelMapper {

    //---------------------------------------------------------------------------
    public Uml2ModelMapper(
        PrintWriter printWriter,
        short xmiFormat
    ) {
        this.pw = new PrintWriter(printWriter);
        this.classifierIds = new HashMap();
        this.associationEndIds = new HashMap();
        this.associationIds = new HashMap();
    }

    //---------------------------------------------------------------------------
    public void mapOperationBegin(
        ModelElement_1_0 operationDef,
        ModelElement_1_0 returnType
    ) throws ServiceException {
        final String name = operationDef.getName();
        final Boolean isQuery;
        if(operationDef.isExceptionType()) {
            isQuery = null;
        } else {
            isQuery = (Boolean)operationDef.objGetValue("isQuery");
        }
        String returnTypeQualifiedName = returnType == null
            ? null
            : (String)returnType.getQualifiedName();
        
        pw.write(indent, 0, nTabs); 
        pw.print("");
        
        // Annotation
        String annotation = (String)operationDef.objGetValue("annotation");
        if(annotation != null) {            
            pw.write(indent, 0, nTabs);
            pw.print("\t");
        }
        
        // Stereotype
        if(!operationDef.objGetList("stereotype").isEmpty()) {
            pw.print("\t");
            pw.write(indent, 0, nTabs); 
            pw.print("\t\t");        
            pw.write(indent, 0, nTabs); 
            pw.println("\t");
        }
        
        nTabs += 1;
    }

    //---------------------------------------------------------------------------
    public void mapOperationEnd(
        ModelElement_1_0 operationDef
    ) {
        nTabs -= 1;
        pw.write(indent, 0, nTabs); 
        pw.println("");
        pw.flush();      
    }

    //---------------------------------------------------------------------------
    public void mapParameter(
        ModelElement_1_0 parameterDef,
        ModelElement_1_0 parameterTypeDef
    ) throws ServiceException {
        String name = parameterDef.getName();
        String typeQualifiedName = parameterTypeDef.getQualifiedName();
        String direction = this.toXMIParameterKind((String)parameterDef.objGetValue("direction"));

        if("return".equals(direction)) {
            pw.write(indent, 0, nTabs); 
            pw.print("");            
        }
        else {
            pw.write(indent, 0, nTabs); 
            pw.print("");
        }
        
        pw.flush();      
    }

    //---------------------------------------------------------------------------
    public void mapAssociationBegin(
        ModelElement_1_0 associationDef,
        ModelElement_1_0 end1,
        ModelElement_1_0 end2
    ) throws ServiceException {
        String name = associationDef.getName();
        String qualifiedName = associationDef.getQualifiedName();
        boolean isDerived = associationDef.isDerived().booleanValue();
        String end1QualifiedName = end1.getQualifiedName();
        String end2QualifiedName = end2.getQualifiedName();
        String end1Id = this.getAssociationEndId(end1QualifiedName);
        String end2Id = this.getAssociationEndId(end2QualifiedName);
        
        pw.write(indent, 0, nTabs); 
        pw.print("");

        // Annotation
        String annotation = (String)associationDef.objGetValue("annotation");
        if(annotation != null) {            
            pw.write(indent, 0, nTabs);
            pw.print("\t");
        }
        
        nTabs += 1;
    }

    //---------------------------------------------------------------------------
    public void mapAssociationEnd(
        ModelElement_1_0 associationDef
    ) {
        nTabs -= 1;
        pw.write(indent, 0, nTabs); 
        pw.println("");
        pw.flush();      
    }

    //---------------------------------------------------------------------------
    public void mapAssociationEnd(
        ModelElement_1_0 associationDef,        
        ModelElement_1_0 associationEndDef,
        ModelElement_1_0 associationEndTypeDef,
        List qualifierTypes,
        boolean mapAsAttribute
    ) throws ServiceException {
        String name = associationEndDef.getName();
        String qualifiedName = associationEndDef.getQualifiedName();
        boolean isChangeable = associationEndDef.isChangeable().booleanValue();
        boolean isDerived = associationDef.isDerived().booleanValue();
        String multiplicity = associationEndDef.getMultiplicity();        
        String qualifiedTypeName = associationEndTypeDef.getQualifiedName();
        String aggregation = associationEndDef.getAggregation();
        String associationQualifiedName = associationDef.getQualifiedName();        
        
        if(mapAsAttribute) {
            pw.write(indent, 0, nTabs); 
            pw.print("");
            
            // Multiplicity
            this.mapMultiplicity(multiplicity);
            
            // Qualifier
            List qualifierNames = associationEndDef.objGetList("qualifierName");
            int i = 0;
            for(Object rawQualifierName : qualifierNames) {
                String qualifierName = (String) rawQualifierName;
                ModelElement_1_0 qualifierType = (ModelElement_1_0)qualifierTypes.get(i++);
                String qualifierTypeName = qualifierType.getQualifiedName();
                
                pw.write(indent, 0, nTabs);
                pw.print("\t");                                
            }
            pw.write(indent, 0, nTabs); 
            pw.println("");            
        }
        else {
            pw.write(indent, 0, nTabs); 
            pw.print("");
            
            // Multiplicity
            this.mapMultiplicity(multiplicity);
            
            pw.write(indent, 0, nTabs); 
            pw.println("");
        }
        
        pw.flush();
    }

    //---------------------------------------------------------------------------
    public void mapPrimitiveType(
        ModelElement_1_0 primitiveTypeDef
    ) throws ServiceException {
        String name = primitiveTypeDef.getName();
        String qualifiedName = primitiveTypeDef.getQualifiedName();

        pw.write(indent, 0, nTabs);
        pw.print("");
        pw.flush();      
    }

    //---------------------------------------------------------------------------
    private void mapMultiplicity(
        String multiplicity
    ) {
        // Multiplicity
        if(Multiplicity.SINGLE_VALUE.toString().equals(multiplicity)) {
            // Upper value
            pw.write(indent, 0, nTabs);
            pw.print("\t");
            
            // Lower value
            pw.write(indent, 0, nTabs);
            pw.print("\t");                        
        }
        else if(Multiplicity.OPTIONAL.code().equals(multiplicity)) {
            // Upper value
            pw.write(indent, 0, nTabs);
            pw.print("\t");
            
            // Lower value
            pw.write(indent, 0, nTabs);
            pw.print("\t");                        
        }
        else if(Multiplicity.UNBOUNDED.equals(multiplicity)) {
            // Upper value
            pw.write(indent, 0, nTabs);
            pw.print("\t");
            
            // Lower value
            pw.write(indent, 0, nTabs);
            pw.print("\t");                        
        }
        else {
            pw.write(indent, 0, nTabs); 
            pw.print("\t");
            pw.write(indent, 0, nTabs); 
            pw.print("\t\t");        
            pw.write(indent, 0, nTabs); 
            pw.println("\t");
        }                
    }
    
    //---------------------------------------------------------------------------
    public void mapAttribute(
        ModelElement_1_0 attributeDef,
        boolean isDerived,
        boolean isChangeable,
        ModelElement_1_0 typeDef,
        boolean referencedTypeIsPrimitive
    ) throws ServiceException {
        String name = attributeDef.getName();
        String typeQualifiedName = typeDef.getQualifiedName();
        String multiplicity = attributeDef.getMultiplicity();

        pw.write(indent, 0, nTabs); 
        pw.print("");

        // Annotation
        String annotation = (String)attributeDef.objGetValue("annotation");
        if(annotation != null) {            
            pw.write(indent, 0, nTabs);
            pw.print("\t");
        }
                
        // Multiplicity
        this.mapMultiplicity(multiplicity);
        
        pw.write(indent, 0, nTabs); 
        pw.println("");
        pw.flush();      
    }

    //---------------------------------------------------------------------------
    public void mapAliasType(
        ModelElement_1_0 aliasTypeDef,
        ModelElement_1_0 typeDef,
        boolean referencedTypeIsPrimitive
    ) throws ServiceException {
        String name = aliasTypeDef.getName();
        String qualifiedName = aliasTypeDef.getQualifiedName();
        String typeQualifiedName = typeDef.getQualifiedName();

        pw.write(indent, 0, nTabs); 
        pw.print("");
        
        pw.write(indent, 0, nTabs); 
        pw.print("\t");
        
        pw.write(indent, 0, nTabs); 
        pw.print("\t\t");
        
        pw.write(indent, 0, nTabs); 
        pw.println("\t");
        
        pw.write(indent, 0, nTabs); 
        pw.print("\t");
        
        pw.write(indent, 0, nTabs); 
        pw.println("");
        
        pw.flush();      
    }

    //---------------------------------------------------------------------------
    public void mapPackageBegin(
        ModelElement_1_0 packageDef
    ) throws ServiceException {
        pw.write(indent, 0, nTabs);
        pw.write("");
        nTabs += 1;
    }

    //---------------------------------------------------------------------------
    public void mapPackageEnd(
        ModelElement_1_0 packageDef
    ) {
        nTabs -= 1;
        pw.write(indent, 0, nTabs); 
        pw.println("");
        pw.flush();      
    }

    //---------------------------------------------------------------------------
    private void printAttribute(
        String attributeName,
        String attributeValue
    ) {
       pw.print(" " + attributeName + "=\"" + attributeValue + "\""); 
    }
    
    //---------------------------------------------------------------------------
    public void mapClassBegin(
        ModelElement_1_0 classDef,
        List supertypeDefs,
        boolean hasFeatures,
        boolean asStructureType
    ) throws ServiceException {
        String name = classDef.getName();
        String classId = this.getClassifierId(classDef.getQualifiedName());
        boolean isAbstract = classDef.isAbstract().booleanValue();

        pw.write(indent, 0, nTabs); 
        pw.print("");

        // Annotation
        String annotation = (String)classDef.objGetValue("annotation");
        if(annotation != null) {
            pw.write(indent, 0, nTabs);
            pw.print("\t");
        }
        // Stereotypes
        if(asStructureType || !classDef.objGetList("stereotype").isEmpty()) {

            pw.write(indent, 0, nTabs);
            pw.print("\t");
            pw.write(indent, 0, nTabs);
            pw.print("\t\t stereotypes = classDef.objGetSet("stereotype");
                key = stereotypes.isEmpty() ? null : (String)stereotypes.iterator().next();
            }
            this.printAttribute("key", key);
            pw.println("/>");

            pw.write(indent, 0, nTabs);
            pw.println("\t");
        }
        // Generalization        
        for(
            Iterator it = supertypeDefs.iterator();
            it.hasNext();
        ) {
            pw.write(indent, 0, nTabs);
            pw.print("\t");            
        }
        nTabs += 1;
    }

    //---------------------------------------------------------------------------
    public void mapClassEnd(
        ModelElement_1_0 classDef,
        boolean hasStructuralFeatures
    ) {
        nTabs -= 1;
        pw.write(indent, 0, nTabs); 
        pw.println("");
        pw.flush();      
    }

    //---------------------------------------------------------------------------
    public void mapModelBegin(
    ) {
        pw.println("");                
        pw.println("");
        pw.println("  ");
        pw.println("  ");
        pw.println("    ");
        pw.println("      
"); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println("
"); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println("
"); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println("
"); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println("
"); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); pw.println("
"); pw.println(" "); pw.println(" "); pw.println(" "); pw.println(" "); } //--------------------------------------------------------------------------- public void mapModelEnd( ) { pw.println(""); pw.flush(); } //--------------------------------------------------------------------------- private String toMOFSyntax( String qualifiedName ) { if(qualifiedName.length() == 0) { return new String(); } else { StringBuffer sb = new StringBuffer(); for(int i = 0; i < qualifiedName.length(); i++) { char ch = qualifiedName.charAt(i); if(':' == ch) { sb.append("::"); } else { sb.append(ch); } } return sb.toString(); } } //--------------------------------------------------------------------------- private String toHTMLString( String input ) { return HtmlEncoder.encode(input, false); } //--------------------------------------------------------------------------- private String toXMIParameterKind( String direction ) { if ("return_dir".equals(direction)) { return "return"; } else if ("out_dir".equals(direction)) { return "out"; } else if ("inout_dir".equals(direction)) { return "inout"; } else { return "in"; } } //--------------------------------------------------------------------------- private String createId( ) { return "_" + UUIDConversion.toUID(UUIDs.newUUID()); } //--------------------------------------------------------------------------- private String getClassifierId( String qualifiedName ) { if (!this.classifierIds.containsKey(qualifiedName)) { this.classifierIds.put( qualifiedName, this.createId() ); } return (String)this.classifierIds.get(qualifiedName); } //--------------------------------------------------------------------------- private String getAssociationEndId( String associationEnd ) { if (!this.associationEndIds.containsKey(associationEnd)) { this.associationEndIds.put( associationEnd, this.createId() ); } return (String)this.associationEndIds.get(associationEnd); } //--------------------------------------------------------------------------- private String getAssociationId( String association ) { if (!this.associationIds.containsKey(association)) { this.associationIds.put( association, this.createId() ); } return (String)this.associationIds.get(association); } //--------------------------------------------------------------------------- // Variables //--------------------------------------------------------------------------- private final PrintWriter pw; private final Map classifierIds; private final Map associationIds; private final Map associationEndIds; private static int nTabs = 0; private static char[] indent = {'\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t','\t'}; public static final short XMI_FORMAT_RSM6 = 1; }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy