1 /* "CodeWorker": a scripting language for parsing and generating text.
2
3 Copyright (C) 1996-1997, 1999-2010 C�dric Lemaire
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 To contact the author: codeworker@free.fr
20 */
21
22 #ifdef WIN32
23 #pragma warning (disable : 4786)
24 #endif
25
26 #include "ScpStream.h"
27 #include "CppCompilerEnvironment.h"
28 #include "CGRuntime.h"
29 #include "ExprScriptExpression.h"
30 #include <string>
31
32 //##protect##"INCLUDE FILES"
33 #include "UtlXMLStream.h"
34 #include "DtaAttributeType.h"
35 //##protect##"INCLUDE FILES"
36
37 #include "GrfSaveProjectTypes.h"
38
39 namespace CodeWorker {
~GrfSaveProjectTypes()40 GrfSaveProjectTypes::~GrfSaveProjectTypes() {
41 delete _pXMLFileName;
42 }
43
executeInternal(DtaScriptVariable & visibility)44 SEQUENCE_INTERRUPTION_LIST GrfSaveProjectTypes::executeInternal(DtaScriptVariable& visibility) {
45 std::string sXMLFileName = _pXMLFileName->getValue(visibility);
46 //##protect##"execute"
47 //##protect##"execute"
48 return CGRuntime::saveProjectTypes(sXMLFileName);
49 }
50
51 //##protect##"implementation"
generateXMLFile(UtlXMLStream & myXMLFile,DtaAttributeType & myType,std::set<std::string> & listForAvoidingCycles)52 void GrfSaveProjectTypes::generateXMLFile(UtlXMLStream& myXMLFile, DtaAttributeType& myType, std::set<std::string>& listForAvoidingCycles) {
53 if (listForAvoidingCycles.find(myType.getName()) != listForAvoidingCycles.end()) return;
54 listForAvoidingCycles.insert(myType.getName());
55 myXMLFile.writeBeginningOfObject(myType.getName());
56 const std::map<std::string, DtaFollowingAttributeInfo*>& listOfAttributes = myType.getAttributes();
57 std::map<std::string, DtaFollowingAttributeInfo*>::const_iterator i;
58 for (i = listOfAttributes.begin(); i != listOfAttributes.end(); i++) if (i->second->isAttribute()) {
59 std::string sMode;
60 if (i->second->getNumberOfUse() == myType.getNumberOfUse()) sMode = "compulsory";
61 else {
62 char sNumber[64];
63 int iPercentOfUse = (100 * i->second->getNumberOfUse()) / myType.getNumberOfUse();
64 sprintf(sNumber, "optional[%d%%]", iPercentOfUse);
65 sMode = sNumber;
66 }
67 myXMLFile.writeAttribute(i->second->getAttributeType().getName(), sMode);
68 }
69 myXMLFile.writeEndOfAttributes();
70 for (i = listOfAttributes.begin(); i != listOfAttributes.end(); i++) if (!i->second->isAttribute()) {
71 generateXMLFile(myXMLFile, i->second->getAttributeType(), listForAvoidingCycles);
72 }
73 bool bElements = false;
74 const std::map<std::string, DtaFollowingAttributeInfo*>& listOfElements = myType.getAttributeElements();
75 for (i = listOfElements.begin(); i != listOfElements.end(); i++) if (i->second->isAttribute()) {
76 if (!bElements) {
77 myXMLFile.writeBeginningOfObject("__ARRAY_TYPE");
78 bElements = true;
79 }
80 std::string sMode;
81 if (i->second->getNumberOfUse() == myType.getNumberOfElementUse()) sMode = "compulsory";
82 else {
83 char sNumber[64];
84 int iPercentOfUse = (100 * i->second->getNumberOfUse()) / myType.getNumberOfElementUse();
85 sprintf(sNumber, "optional[%d%%]", iPercentOfUse);
86 sMode = sNumber;
87 }
88 myXMLFile.writeAttribute(i->second->getAttributeType().getName(), sMode);
89 }
90 if (bElements) myXMLFile.writeEndOfAttributes();
91 for (i = listOfElements.begin(); i != listOfElements.end(); i++) if (!i->second->isAttribute()) {
92 if (!bElements) {
93 myXMLFile.writeBeginningOfObject("__ARRAY_TYPE");
94 myXMLFile.writeEndOfAttributes();
95 bElements = true;
96 }
97 generateXMLFile(myXMLFile, i->second->getAttributeType(), listForAvoidingCycles);
98 }
99 if (bElements) myXMLFile.writeEndOfObject("__ARRAY_TYPE");
100 myXMLFile.writeEndOfObject(myType.getName());
101 listForAvoidingCycles.erase(myType.getName());
102 }
103
104 //##protect##"implementation"
105
compileCpp(CppCompilerEnvironment & theCompilerEnvironment) const106 void GrfSaveProjectTypes::compileCpp(CppCompilerEnvironment& theCompilerEnvironment) const {
107 CW_BODY_INDENT << "CGRuntime::saveProjectTypes(";
108 _pXMLFileName->compileCppString(theCompilerEnvironment);
109 CW_BODY_STREAM << ");";
110 CW_BODY_ENDL;
111 }
112 }
113