001/**
002 *
003 * Copyright 2014 Anno van Vliet
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.xdatalayout.provider;
018
019import java.io.IOException;
020import java.util.List;
021
022import org.jivesoftware.smackx.xdatalayout.packet.DataLayout;
023import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.DataFormLayoutElement;
024import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Fieldref;
025import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Reportedref;
026import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Section;
027import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Text;
028import org.xmlpull.v1.XmlPullParser;
029import org.xmlpull.v1.XmlPullParserException;
030
031/**
032 * Extension Provider for Page layout of forms.
033 *
034 * @author Anno van Vliet
035 *
036 */
037public class DataLayoutProvider {
038
039    public static DataLayout parse(XmlPullParser parser) throws XmlPullParserException, IOException {
040        DataLayout dataLayout = new DataLayout(parser.getAttributeValue("", "label"));
041        parseLayout(dataLayout.getPageLayout(), parser);
042        return dataLayout;
043    }
044
045    private static Section parseSection(XmlPullParser parser) throws XmlPullParserException, IOException {
046        Section layout = new Section(parser.getAttributeValue("", "label"));
047        parseLayout(layout.getSectionLayout(), parser);
048        return layout;
049    }
050
051    private static void parseLayout(List<DataFormLayoutElement> layout, XmlPullParser parser) throws XmlPullParserException, IOException {
052        final int initialDepth = parser.getDepth();
053        outerloop: while (true) {
054            int eventType = parser.next();
055            switch (eventType) {
056            case XmlPullParser.START_TAG:
057                switch (parser.getName()) {
058                case Text.ELEMENT:
059                    layout.add(new Text(parser.nextText()));
060                    break;
061                case Section.ELEMENT:
062                    layout.add(parseSection(parser));
063                    break;
064                case Fieldref.ELEMENT:
065                    layout.add(parseFieldref(parser));
066                    break;
067                case Reportedref.ELEMENT:
068                    layout.add(new Reportedref());
069                    break;
070                default:
071                    break;
072                }
073                break;
074            case XmlPullParser.END_TAG:
075                if (parser.getDepth() == initialDepth) {
076                    break outerloop;
077                }
078                break;
079            }
080        }
081    }
082
083    private static Fieldref parseFieldref(XmlPullParser parser) throws XmlPullParserException, IOException {
084        final int initialDepth = parser.getDepth();
085        Fieldref fieldref = new Fieldref(parser.getAttributeValue("", "var"));
086        outerloop: while (true) {
087            int eventType = parser.next();
088            if (eventType == XmlPullParser.END_TAG && parser.getDepth() == initialDepth) {
089                break outerloop;
090            }
091        }
092        return fieldref;
093    }
094
095}