Statistics
| Branch: | Revision:

root / src / nedxml / nedtools.cc @ 7c7d95e5

History | View | Annotate | Download (3.66 KB)

1
//==========================================================================
2
// nedtools.cc -
3
//
4
//                     OMNeT++/OMNEST
5
//            Discrete System Simulation in C++
6
//
7
//==========================================================================
8

    
9
/*--------------------------------------------------------------*
10
  Copyright (C) 2002-2008 Andras Varga
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16

    
17
#include "nedtools.h"
18
#include "nederror.h"
19
#include "neddtdvalidator.h"
20
#include "fileutil.h"
21

    
22
USING_NAMESPACE
23

    
24

    
25
void NEDTools::repairNEDElementTree(NEDElement *tree)
26
{
27
    while (true)
28
    {
29
        // try DTD validation, and find first problem
30
        NEDErrorStore errors;
31
        NEDDTDValidator dtdvalidator(&errors);
32
        dtdvalidator.validate(tree);
33
        if (errors.empty())
34
            break; // we're done
35
        NEDElement *errnode = errors.errorContext(0);
36
        if (!errnode)
37
            break; // this shouldn't happen, but if it does we can't go on
38
        if (!errnode->getParent())
39
            break; // we can't help if root node is wrong
40

    
41
        // throw out problem node, and try again
42
        //printf("DBG: repairNEDElementTree: discarding <%s>\n", errnode->getTagName());
43
        errnode->getParent()->removeChild(errnode);
44
    }
45
}
46

    
47
void NEDTools::splitToFiles(FilesElement *tree)
48
{
49
    FilesElement *tmpTree = new FilesElement();
50
    for (NEDElement *child=tree->getFirstChild(); child; child = child->getNextSibling())
51
    {
52
        // ignore msg files
53
        if (child->getTagCode()!=NED_NED_FILE)
54
            continue;
55

    
56
        NedFileElement *fileNode = (NedFileElement *)child;
57

    
58
        // we'll generate new files into the directory of the original file
59
        std::string directory;
60
        std::string filename;
61
        splitFileName(fileNode->getFilename(), directory, filename);
62

    
63
        // go through NED components in the file, and create new NED files for them
64
        for (NEDElement *child=fileNode->getFirstChild(); child; )
65
        {
66
            int type = child->getTagCode();
67
            if (type!=NED_SIMPLE_MODULE && type!=NED_COMPOUND_MODULE &&
68
                type!=NED_CHANNEL && type!=NED_MODULE_INTERFACE &&
69
                type!=NED_CHANNEL_INTERFACE)
70
            {
71
                child = child->getNextSibling();
72
                continue;
73
            }
74

    
75
            // process NED component
76
            NEDElement *componentNode = child;
77
            const char *componentName = componentNode->getAttribute("name");
78

    
79
            // create new file for it
80
            NedFileElement *newFileNode = fileNode->dup();
81
            std::string newFileName = directory + "/" + componentName + ".ned";
82
            newFileNode->setFilename(newFileName.c_str());
83
            tmpTree->appendChild(newFileNode);
84

    
85
            // copy comments and imports from old file
86
            for (NEDElement *child2=fileNode->getFirstChild(); child2; child2 = child2->getNextSibling())
87
                if (child2->getTagCode()==NED_COMMENT || child2->getTagCode()==NED_IMPORT)
88
                    newFileNode->appendChild(child2->dupTree());
89

    
90
            // move NED component into new file
91
            child = child->getNextSibling(); // adjust iterator
92
            fileNode->removeChild(componentNode);
93
            newFileNode->appendChild(componentNode);
94
        }
95

    
96
        // rename original file
97
        fileNode->setFilename((std::string(fileNode->getFilename())+"-STRIPPED").c_str());
98
    }
99

    
100
    while (tmpTree->getFirstChild())
101
        tree->appendChild(tmpTree->removeChild(tmpTree->getFirstChild()));
102
}
103

    
104