|
What this is
Other links
The source code
// $Id: GeneratorPHP4.java,v 1.13 2004/10/03 17:59:19 mvw Exp $
// Copyright (c) 2004 The Regents of the University of California. All
// Rights Reserved. Permission to use, copy, modify, and distribute this
// software and its documentation without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph appear in all copies. This software program and
// documentation are copyrighted by The Regents of the University of
// California. The software program and documentation are supplied "AS
// IS", without any accompanying services from The Regents. The Regents
// does not warrant that the operation of the program will be
// uninterrupted or error-free. The end-user understands that the program
// was developed for research purposes and is advised not to rely
// exclusively on the program for any reason. IN NO EVENT SHALL THE
// UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
// SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
// ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
// THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
// PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
// CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
// UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
package org.argouml.language.php.generator;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.rmi.server.UID;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.swing.Icon;
import org.apache.log4j.Logger;
import org.argouml.application.ArgoVersion;
import org.argouml.application.api.Argo;
import org.argouml.application.api.Notation;
import org.argouml.application.api.PluggableNotation;
import org.argouml.language.php.PHPDocumentor;
import org.argouml.model.ModelFacade;
import org.argouml.uml.generator.FileGenerator;
import org.argouml.uml.generator.Generator2;
/**
* Generator class for PHP 4.x source code
*
* @author Kai Schröder
* @since ArgoUML 0.15.5
*/
public class GeneratorPHP4
extends Generator2
implements PluggableNotation, FileGenerator {
/**
* Sets the indentation level to four spaces
*/
protected static final String INDENT = " ";
/**
* The name of the language this module generates source code for
*/
protected static final String LANGUAGE_NAME = "PHP";
/**
* Icon to represent this notation in the GUI
*/
protected static final Icon ICON = Argo.lookupIconResource("PHPNotation");
/**
* Info block already written to log file?
*/
protected static final TreeMap TM_INFO_BLOCK_LOGGED = new TreeMap();
/**
* The major version of the language this module generates source code for
*/
private int iLanguageMajorVersion = LANGUAGE_MAJOR_VERSION;
/**
* source section handler
*/
private static Section objSection = null;
/**
* The log4j logger to log messages to
*/
private static final Logger LOG = Logger.getLogger(GeneratorPHP4.class);
/**
* The major version of the language this module generates source code for
*/
private static final int LANGUAGE_MAJOR_VERSION = 4;
// ----- class constructors ------------------------------------------------
/**
* Zero-argument class constructor
*/
private GeneratorPHP4() {
super(Notation.makeNotation(LANGUAGE_NAME, LANGUAGE_MAJOR_VERSION
+ ".x", ICON));
logModuleInfo();
}
/**
* Class constructor
*
* @param iLangMajorVersion The major version of the language this module
* generates source code for.
*/
protected GeneratorPHP4(int iLangMajorVersion) {
super(Notation.makeNotation(LANGUAGE_NAME, iLangMajorVersion
+ ".x", ICON));
iLanguageMajorVersion = iLangMajorVersion;
logModuleInfo();
}
// ----- org.argouml.application.api.NotationProvider ----------------------
/**
* Generates extension point
*
* @param modelElement Model element to generate notation for.
*
* @return Generated notation for model element.
*/
public String generateExtensionPoint(Object modelElement) {
// TODO: Auto-generated method stub
LOG.debug("generateExtensionPoint(MExtensionPoint modelElement)");
if (!ModelFacade.isAExtensionPoint(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, ExtensionPoint required");
}
return "generateExtensionPoint(MExtensionPoint modelElement)";
}
/**
* @see org.argouml.application.api.NotationProvider2#generateObjectFlowState(java.lang.Object)
*/
public String generateObjectFlowState(Object m) {
Object c = ModelFacade.getType(m);
if (c == null) return "";
return ModelFacade.getName(c);
}
/**
* Generates operation
*
* @param modelElement Model element to generate notation for.
* @param bAddDocs Add documentation in front of notation?
*
* @return Generated notation for model element.
*/
public String generateOperation(Object modelElement, boolean bAddDocs) {
if (!ModelFacade.isAOperation(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, Operation required");
}
String sOperation = "";
if (bAddDocs) {
PHPDocumentor objPHPDoc = null;
try {
objPHPDoc = new PHPDocumentor(modelElement);
} catch (Exception exp) {
LOG.error("Generating operation DocBlock FAILED: "
+ exp.getMessage());
} finally {
if (objPHPDoc != null) {
sOperation += objPHPDoc.toString();
}
}
}
String sVisibility = generate(ModelFacade.getVisibility(modelElement));
if (sVisibility != null && sVisibility != "") {
sOperation += sVisibility + " ";
}
if (iLanguageMajorVersion > 4) {
/* scope */
Object ownerScope = ModelFacade.getOwnerScope(modelElement);
if (ModelFacade.CLASSIFIER_SCOPEKIND.equals(ownerScope)) {
sOperation += "static ";
}
/* changeability */
if (ModelFacade.isLeaf(modelElement)) {
sOperation += "final ";
}
/* abstractness */
if (ModelFacade.isAbstract(modelElement)) {
sOperation += "abstract ";
}
}
boolean bReturnByReference = false;
Iterator itTaggedValues = ModelFacade.getTaggedValues(modelElement);
if (itTaggedValues != null && itTaggedValues instanceof Iterator) {
while (itTaggedValues.hasNext()) {
Object objTaggedValue = itTaggedValues.next();
if (ModelFacade.getTagOfTag(objTaggedValue).equals("&")) {
if (ModelFacade.getValueOfTag(objTaggedValue)
.equals("true")) {
bReturnByReference = true;
break;
}
}
}
}
String sOperationName = NameGenerator.generate(modelElement,
iLanguageMajorVersion);
if (bReturnByReference) {
sOperationName = "&" + sOperationName;
}
sOperation += "function " + sOperationName + "(";
Collection colParameters = ModelFacade.getParameters(modelElement);
if (colParameters != null) {
Iterator itParameters = colParameters.iterator();
boolean bFirst = true;
while (itParameters.hasNext()) {
Object objParameter = itParameters.next();
if (!ModelFacade.isReturn(objParameter)) {
if (!bFirst) {
sOperation += ", ";
} else {
bFirst = false;
}
sOperation += generate(objParameter);
}
}
}
sOperation += ")";
return sOperation;
}
/**
* Generates attribute
*
* @param modelElement Model element to generate notation for.
* @param bAddDocs Add documentation in front of notation?
*
* @return Generated notation for model element.
*/
public String generateAttribute(Object modelElement, boolean bAddDocs) {
if (!ModelFacade.isAAttribute(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, Attribute required");
}
String sAttribute = "";
if (bAddDocs) {
PHPDocumentor objPHPDoc = null;
try {
objPHPDoc = new PHPDocumentor(modelElement);
} catch (Exception exp) {
LOG.error("Generating attribute DocBlock FAILED: "
+ exp.getMessage());
} finally {
if (objPHPDoc != null) {
sAttribute += objPHPDoc.toString(INDENT);
}
}
}
String sVisibility = generate(ModelFacade.getVisibility(modelElement));
if (sVisibility != null && sVisibility != "") {
sAttribute += sVisibility + " ";
}
if (iLanguageMajorVersion > 4) {
/* scope */
Object ownerScope = ModelFacade.getOwnerScope(modelElement);
if (ModelFacade.CLASSIFIER_SCOPEKIND.equals(ownerScope)) {
sAttribute += "static ";
}
} else {
sAttribute += "var ";
}
sAttribute += "$" + NameGenerator.generate(modelElement,
iLanguageMajorVersion);
String sInitialValue = null;
Object exprInitialValue = ModelFacade.getInitialValue(modelElement);
if (exprInitialValue != null) {
sInitialValue = generateDefaultValue(
ModelFacade.getType(modelElement),
generate(exprInitialValue).trim(), false);
} else {
sInitialValue = generateDefaultValue(
ModelFacade.getType(modelElement), null, false);
}
if (sInitialValue != null && sInitialValue.length() > 0) {
sAttribute += " = " + sInitialValue;
} else {
sAttribute += "[ ";
sAttribute += (exprInitialValue != null) ? "!= null" : "null";
sAttribute += " | ";
sAttribute += generateDefaultValue(
ModelFacade.getType(modelElement),
generate(exprInitialValue).trim(), false);
sAttribute += " | ";
sAttribute += generateDefaultValue(
ModelFacade.getType(modelElement),
generate(exprInitialValue).trim(), true);
sAttribute += " ]";
}
sAttribute += ";";
return sAttribute;
}
/**
* Generates parameter
*
* @param modelElement Model element to generate notation for.
*
* @return Generated notation for model element.
*/
public String generateParameter(Object modelElement) {
if (!ModelFacade.isAParameter(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, Parameter required");
}
String sParameter = "";
if (ModelFacade.isReturn(modelElement)) {
Object objType = ModelFacade.getType(modelElement);
if (ModelFacade.getName(objType).equals("void")) {
return "";
} else {
String sType = convertType(objType);
if (sType != null && sType.trim() != "") {
return "return (" + sType + ") $returnValue;";
} else {
return "return $returnValue;";
}
}
} else {
if (iLanguageMajorVersion < 5) {
// TODO: Do we really need this for PHP5?
// TODO: Implent this in ModelFacade
/* if OUT or INOUT, then pass by reference */
if (ModelFacade.getKind(modelElement).equals(
ModelFacade.INOUT_PARAMETERDIRECTIONKIND)
|| ModelFacade.getKind(modelElement).equals(
ModelFacade.OUT_PARAMETERDIRECTIONKIND)) {
sParameter += "&";
}
}
sParameter += "$" + ModelFacade.getName(modelElement);
String sDefaultValue =
generate(ModelFacade.getDefaultValue(modelElement));
if (sDefaultValue != null && sDefaultValue.length() > 0) {
sParameter += " = " + sDefaultValue;
} else {
boolean bAddDefaultValue = false;
Collection colParameters =
ModelFacade.getParameters(ModelFacade
.getBehavioralFeature(modelElement));
if (colParameters != null) {
Iterator itParameters = colParameters.iterator();
while (itParameters.hasNext()) {
Object objParameter = itParameters.next();
if (!ModelFacade.isReturn(objParameter)) {
if (!modelElement.equals(objParameter)) {
if (ModelFacade.getDefaultValue(objParameter)
!= null) {
bAddDefaultValue = true;
}
} else {
break;
}
}
}
}
if (bAddDefaultValue) {
sParameter += " = " + generateDefaultValue(
ModelFacade.getType(modelElement), null, false);
}
}
}
return sParameter;
}
/**
* Generates package
*
* @param modelElement Model element to generate notation for.
*
* @return Generated notation for model element.
*
* TODO: fix org.argouml.model.ModelFacade#getType
*/
public String generatePackage(Object modelElement) {
String sPackage = "";
if (!ModelFacade.isAPackage(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, Package required");
}
String sPackageName =
NameGenerator.generate(modelElement, iLanguageMajorVersion);
PHPDocumentor objPHPDoc = null;
try {
objPHPDoc = new PHPDocumentor(modelElement);
} catch (Exception exp) {
LOG.error("Generating package DocBlock FAILED: "
+ exp.getMessage());
} finally {
if (objPHPDoc != null) {
sPackage += objPHPDoc.toString() + "\n";
}
}
Collection colElements = ModelFacade.getOwnedElements(modelElement);
if (colElements != null) {
Iterator itElements = colElements.iterator();
if (itElements != null) {
while (itElements.hasNext()) {
Object objElement = itElements.next();
if (ModelFacade.isAPackage(objElement)) {
sPackage += generate(objElement) + "\n";
} else if (ModelFacade.isAClassifier(objElement)) {
sPackage += generate(objElement) + "\n";
} else {
sPackage += "/*\n";
sPackage += "feature not supported by PHP:\n";
sPackage += "-----------------------------\n";
sPackage += generate(objElement) + "\n";
sPackage += "*/\n";
}
if (itElements.hasNext()) {
sPackage += "\n";
}
}
}
} else {
sPackage += "// this package contains no elements\n";
}
sPackage += "\n/* end of package " + sPackageName + " */";
return sPackage;
}
/**
* Generates class or interface
*
* @param modelElement Model element to generate notation for.
*
* @return Generated notation for model element.
*/
public String generateClassifier(Object modelElement) {
if (!ModelFacade.isAClassifier(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, Classifier required");
}
String sClassifier = "";
String sClassType = "";
if (iLanguageMajorVersion > 4) {
if (ModelFacade.isAClass(modelElement)) {
if (ModelFacade.isLeaf(modelElement)) {
sClassType = "final class";
} else {
sClassType = "class";
}
if (ModelFacade.isAbstract(modelElement)) {
sClassType = "abstract " + sClassType;
}
} else if (ModelFacade.isAInterface(modelElement)) {
sClassType = "interface";
if (ModelFacade.isLeaf(modelElement)) {
sClassType = "final " + sClassType;
}
} else {
return null;
}
} else {
if (ModelFacade.isAClass(modelElement)
|| ModelFacade.isAInterface(modelElement)) {
sClassType = "class";
} else {
return null;
}
}
PHPDocumentor objPHPDoc = null;
try {
objPHPDoc = new PHPDocumentor(modelElement);
} catch (Exception exp) {
LOG.error("Generating classifier DocBlock FAILED: "
+ exp.getMessage());
} finally {
if (objPHPDoc != null) {
sClassifier += objPHPDoc.toString();
}
}
String sClassName =
NameGenerator.generate(modelElement, iLanguageMajorVersion);
sClassifier += sClassType + " " + sClassName + "\n";
sClassifier += generateClassifierGeneralisations(modelElement);
sClassifier += generateClassifierSpecifications(modelElement);
sClassifier += "{\n";
sClassifier += generateClassifierAttributes(modelElement);
sClassifier += generateClassifierOperations(modelElement);
sClassifier += "\n} /* end of " + sClassType + " " + sClassName + " */";
return sClassifier;
}
/**
* Generates tagged value
*
* @param modelElement Model element to generate notation for.
*
* @return Generated notation for model element.
*/
public String generateTaggedValue(Object modelElement) {
// TODO: Auto-generated method stub
LOG.debug("generateTaggedValue(MTaggedValue modelElement)");
if (!ModelFacade.isATaggedValue(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, TaggedValue required");
}
return "generateTaggedValue(MTaggedValue modelElement)";
}
/**
* Generates association
*
* @param modelElement Model element to generate notation for.
*
* @return Generated notation for model element.
*/
public String generateAssociation(Object modelElement) {
// TODO: Auto-generated method stub
LOG.debug("generateExtensionPoint(MExtensionPoint modelElement)");
if (!ModelFacade.isAAssociation(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, Association required");
}
return "generateAssociation(MAssociation modelElement)";
}
/**
* Generates association end
*
* @param modelElement Model element to generate notation for.
*
* @return Generated notation for model element.
*/
public String generateAssociationEnd(Object modelElement) {
// TODO: Auto-generated method stub
LOG.debug("generateAssociationEnd(MAssociationEnd modelElement)");
if (!ModelFacade.isAAssociationEnd(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, AssociationEnd required");
}
return "generateAssociationEnd(MAssociationEnd modelElement)";
}
/**
* Genarates multiplicity
*
* @param modelElement Model element to generate notation for.
*
* @return Generated notation for model element.
*/
public String generateMultiplicity(Object modelElement) {
// TODO: Auto-generated method stub
LOG.debug("generateMultiplicity(MMultiplicity modelElement)");
if (!ModelFacade.isAMultiplicity(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, Multiplicity required");
}
return "generateMultiplicity(MMultiplicity modelElement)";
}
/**
* Generates state
*
* @param modelElement Model element to generate notation for.
*
* @return Generated notation for model element.
*/
public String generateState(Object modelElement) {
// TODO: Auto-generated method stub
LOG.debug("generateState(MState modelElement)");
if (!ModelFacade.isAState(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, State required");
}
return "generateState(MState modelElement)";
}
/**
* Generates transition
*
* @param modelElement Model element to generate notation for.
*
* @return Generated notation for model element.
*/
public String generateTransition(Object modelElement) {
// TODO: Auto-generated method stub
LOG.debug("generateTransition(MTransition modelElement)");
if (!ModelFacade.isATransition(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, Transition required");
}
return "generateTransition(MTransition modelElement)";
}
/**
* Generates action
*
* @param modelElement Model element to generate notation for.
*
* @return Generated notation for model element.
*/
public String generateAction(Object modelElement) {
// TODO: Auto-generated method stub
LOG.debug("generateAction(Object modelElement)");
if (!ModelFacade.isAAction(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, Action required");
}
return "generateAction(Object modelElement)";
}
/**
* Generates guard
*
* @param modelElement Model element to generate notation for.
*
* @return Generated notation for model element.
*/
public String generateGuard(Object modelElement) {
// TODO: Auto-generated method stub
LOG.debug("generateGuard(MGuard modelElement)");
if (!ModelFacade.isAGuard(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, Guard required");
}
return "generateGuard(MGuard modelElement)";
}
/**
* Generates message
*
* @param modelElement Model element to generate notation for.
*
* @return Generated notation for model element.
*/
public String generateMessage(Object modelElement) {
// TODO: Auto-generated method stub
LOG.debug("generateMessage(MMessage modelElement)");
if (!ModelFacade.isAMessage(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, Message required");
}
return "generateMessage(MMessage modelElement)";
}
/**
* Generates visibility
*
* @param modelElement Model element to generate notation for.
*
* @return Generated notation for model element.
*/
public String generateVisibility(Object modelElement) {
if (!ModelFacade.isAVisibilityKind(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, VisibilityKind required");
}
if (iLanguageMajorVersion > 4) {
if (ModelFacade.PUBLIC_VISIBILITYKIND.equals(modelElement)) {
return "public";
} else if (ModelFacade.PROTECTED_VISIBILITYKIND
.equals(modelElement)) {
return "protected";
} else if (ModelFacade.PRIVATE_VISIBILITYKIND
.equals(modelElement)) {
return "private";
}
}
return "";
}
/**
* Generates the String representation for an Event.
*
* @param modelElement Model element to generate notation for.
*
* @return Generated notation for model element.
*/
public String generateEvent(Object modelElement) {
if (!ModelFacade.isAEvent(modelElement)) {
throw new ClassCastException(modelElement.getClass()
+ " has wrong object type, Event required");
}
return "";
}
// --- org.argouml.uml.generator.FileGenerator -----------------------------
/**
* Generates file
*
* @param modelElement Model element to generate notation for.
* @param sPath output base directory
*
* @deprecated style break, see issue 2570
*
* @return name of generated file on success;
*
|
Copyright 1998-2008 Alvin Alexander
All Rights Reserved.
devdaily.com is based in louisville, kentucky, and this web site is hosted by godaddy.com