void ConvertXIBToNib(FILE* fpOut, pugi::xml_document& doc) {
    pugi::xml_node dataNode = doc.first_element_by_path("/archive/data");

    XIBObject* root = new XIBObject();
    root->ScanXIBNode(dataNode);
    XIBObject::ParseAllXIBMembers();

    XIBObject* Objects = root->FindMember("IBDocument.Objects");
    XIBObject* objectRecords = Objects->FindMember("objectRecords");
    XIBDictionary* properties = (XIBDictionary*)Objects->FindMember("flattenedProperties");

    XIBObject* orderedObjects = objectRecords->FindMember("orderedObjects");

    //  Go through each ordered object to find replacements
    for (memberList::iterator cur = orderedObjects->_members.begin(); cur != orderedObjects->_members.end(); cur++) {
        XIBMember* curMember = *cur;
        XIBObject* curObject = curMember->_obj;

        if (strcmp(curObject->ClassName(), "IBObjectRecord") == 0) {
            XIBObject* obj = curObject->FindMember("object");

            if (obj) {
                XIBObject* objectId = curObject->FindMember("objectID");
                if (!objectId) {
                    objectId = curObject->FindMember("id");
                }
                int objId = objectId->intValue();

                //  Attempt to find any associated custom class name
                char szPropName[255];
                sprintf(szPropName, "%d.CustomClassName", objId);
                const char* pClassName = obj->ClassName();

                XIBObject* customName = properties->ObjectForKey(szPropName);
                if (customName) {
                    const char* pCustomName = customName->stringValue();
                    obj->SetSwappedClassName(pCustomName);
                }

                for (memberList::iterator prop = properties->_members.begin(); prop != properties->_members.end(); prop++) {
                    char szMeta[255];
                    sprintf(szMeta, "%d.", objId);
                    if (strncmp(szMeta, (*prop)->_name, strlen(szMeta)) == 0) {
                        obj->AddMember(&(*prop)->_name[strlen(szMeta)], (*prop)->_obj);
                    }
                }
            }
        }
    }

    //  Create connections list
    XIBArray* connections = new XIBArray();
    XIBObject* connectionrecords = Objects->FindMember("connectionRecords");

    for (memberList::iterator cur = connectionrecords->_members.begin(); cur != connectionrecords->_members.end(); cur++) {
        XIBMember* curMember = *cur;
        XIBObject* curObject = curMember->_obj;

        if (strcmp(curObject->ClassName(), "IBConnectionRecord") == 0) {
            XIBObject* obj = curObject->FindMember("connection");

            if (obj) {
                connections->AddMember(NULL, obj);
            }
        }
    }

    //  Sort connection records alphabetically using stable, uh, bubble sort
    for (;;) {
        bool didSwap = false;

        for (memberList::iterator cur = connections->_members.begin(); cur != connections->_members.end(); cur++) {
            if ((cur + 1) == connections->_members.end())
                break;
            XIBMember* curMember = (*cur);
            XIBMember* nextMember = (*(cur + 1));

            if (curMember->_name != NULL)
                continue;

            //  Event connections first
            if (strcmp(curMember->_obj->_className, "IBCocoaTouchOutletConnection") == 0 &&
                strcmp(nextMember->_obj->_className, "IBCocoaTouchEventConnection") == 0) {
                *cur = nextMember;
                *(cur + 1) = curMember;
                didSwap = true;
                continue;
            }

            if (strcmp(curMember->_obj->_className, nextMember->_obj->_className) == 0) {
                const char* label1 = curMember->_obj->FindMember("label")->stringValue();
                const char* label2 = nextMember->_obj->FindMember("label")->stringValue();

                if (strcmp(label1, label2) > 0) {
                    *cur = nextMember;
                    *(cur + 1) = curMember;
                    didSwap = true;
                }
            }
        }

        if (!didSwap)
            break;
    }

    //  Construct root object
    XIBObject* rootObjects = root->FindMember("IBDocument.RootObjects");
    rootObjects->_className = "NSArray";
    connections->_className = "NSArray";

    XIBArray* allObjects = new XIBArray();
    XIBArray* accessibilityObjects = new XIBAccessibilityArray();
    XIBArray* visibleWindows = new XIBArray();

    XIBObject* nibRoot = new XIBArray();
    nibRoot->_className = "NSObject";
    nibRoot->_members.clear();
    nibRoot->AddMember("UINibTopLevelObjectsKey", rootObjects);
    nibRoot->AddMember("UINibObjectsKey", allObjects);
    nibRoot->AddMember("UINibConnectionsKey", connections);
    nibRoot->AddMember("UINibVisibleWindowsKey", visibleWindows);
    nibRoot->AddMember("UINibAccessibilityConfigurationsKey", accessibilityObjects);
    nibRoot->AddMember("UINibKeyValuePairsKey", new XIBArray());

    NIBWriter* writer = new NIBWriter(fpOut);
    writer->_allUIObjects = allObjects;
    writer->_visibleWindows = visibleWindows;
    writer->AddOutputObject(nibRoot);
    writer->WriteData();
}
Esempio n. 2
0
void NIBWriter::WriteObjects()
{
    XIBObject *nibRoot = new XIBArray();

    nibRoot->_className = "NSObject";
    nibRoot->_members.clear();
    nibRoot->AddMember("UINibTopLevelObjectsKey", _topObjects);
    nibRoot->AddMember("UINibObjectsKey", _allUIObjects);
    nibRoot->AddMember("UINibConnectionsKey", _connections);
    nibRoot->AddMember("UINibVisibleWindowsKey", _visibleWindows);
    nibRoot->AddMember("UINibAccessibilityConfigurationsKey", _accessibilityObjects);
    nibRoot->AddMember("UINibKeyValuePairsKey", new XIBArray());

    AddOutputObject(nibRoot);
    //  Sort connection records alphabetically using stable, uh, bubble sort
    for ( ;; ) {
        bool didSwap = false;

        for ( memberList::iterator cur = _connections->_outputMembers.begin(); cur != _connections->_outputMembers.end(); cur ++ ) {
            if ( (cur + 1) == _connections->_outputMembers.end() ) break;
            XIBMember *curMember = (*cur);
            XIBMember *nextMember = (*(cur + 1));

            if ( strcmp(curMember->_name, "UINibEncoderEmptyKey") != 0 ) continue;

            //  Event connections first
            if ( strcmp(curMember->_obj->_className, "UIRuntimeOutletConnection") == 0 &&
                 strcmp(nextMember->_obj->_className, "UIRuntimeEventConnection") == 0 ) {
                *cur = nextMember;
                *(cur + 1) = curMember;
                didSwap = true;
                continue;
            }

            if ( strcmp(curMember->_obj->_className, nextMember->_obj->_className) == 0 ) {
                const char *label1, *label2;

                if ( strcmp(curMember->_obj->_className, "UIRuntimeEventConnection") == 0 ) {
                    UIRuntimeEventConnection *conn1 = (UIRuntimeEventConnection *) curMember->_obj;
                    UIRuntimeEventConnection *conn2 = (UIRuntimeEventConnection *) nextMember->_obj;

                    label1 = conn1->_label;
                    label2 = conn2->_label;
                } else {
                    UIRuntimeOutletConnection *conn1 = (UIRuntimeOutletConnection *) curMember->_obj;
                    UIRuntimeOutletConnection *conn2 = (UIRuntimeOutletConnection *) nextMember->_obj;

                    label1 = conn1->_label;
                    label2 = conn2->_label;
                }

                if ( strcmp(label1, label2) > 0 ) {
                    *cur = nextMember;
                    *(cur + 1) = curMember;
                    didSwap = true;
                }
            }
        }

        if ( !didSwap ) break;
    }

    WriteData();
}