DataLayoutProvider.java

  1. /**
  2.  *
  3.  * Copyright 2014 Anno van Vliet
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.xdatalayout.provider;

  18. import java.io.IOException;
  19. import java.util.List;

  20. import org.jivesoftware.smack.SmackException;
  21. import org.jivesoftware.smackx.xdatalayout.packet.DataLayout;
  22. import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.DataFormLayoutElement;
  23. import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Fieldref;
  24. import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Reportedref;
  25. import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Section;
  26. import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Text;
  27. import org.xmlpull.v1.XmlPullParser;
  28. import org.xmlpull.v1.XmlPullParserException;

  29. /**
  30.  * Extension Provider for Page layout of forms.
  31.  *
  32.  * @author Anno van Vliet
  33.  *
  34.  */
  35. public class DataLayoutProvider {

  36.     public static DataLayout parse(XmlPullParser parser) throws XmlPullParserException, IOException,
  37.                     SmackException {
  38.         DataLayout dataLayout = new DataLayout(parser.getAttributeValue("", "label"));
  39.         parseLayout(dataLayout.getPageLayout(), parser);
  40.         return dataLayout;
  41.     }

  42.     private static Section parseSection(XmlPullParser parser) throws XmlPullParserException, IOException {
  43.         Section layout = new Section(parser.getAttributeValue("", "label"));
  44.         parseLayout(layout.getSectionLayout(), parser);
  45.         return layout;
  46.     }

  47.     private static void parseLayout(List<DataFormLayoutElement> layout, XmlPullParser parser) throws XmlPullParserException, IOException {
  48.         final int initialDepth = parser.getDepth();
  49.         outerloop: while (true) {
  50.             int eventType = parser.next();
  51.             switch (eventType) {
  52.             case XmlPullParser.START_TAG:
  53.                 switch (parser.getName()) {
  54.                 case Text.ELEMENT:
  55.                     layout.add(new Text(parser.nextText()));
  56.                     break;
  57.                 case Section.ELEMENT:
  58.                     layout.add(parseSection(parser));
  59.                     break;
  60.                 case Fieldref.ELEMENT:
  61.                     layout.add(parseFieldref(parser));
  62.                     break;
  63.                 case Reportedref.ELEMENT:
  64.                     layout.add(new Reportedref());
  65.                     break;
  66.                 default:
  67.                     break;
  68.                 }
  69.                 break;
  70.             case XmlPullParser.END_TAG:
  71.                 if (parser.getDepth() == initialDepth) {
  72.                     break outerloop;
  73.                 }
  74.                 break;
  75.             }
  76.         }
  77.     }

  78.     private static Fieldref parseFieldref(XmlPullParser parser) throws XmlPullParserException, IOException {
  79.         final int initialDepth = parser.getDepth();
  80.         Fieldref fieldref = new Fieldref(parser.getAttributeValue("", "var"));
  81.         outerloop: while (true) {
  82.             int eventType = parser.next();
  83.             if (eventType == XmlPullParser.END_TAG && parser.getDepth() == initialDepth) {
  84.                 break outerloop;
  85.             }
  86.         }
  87.         return fieldref;
  88.     }

  89. }