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; 028 029import org.xmlpull.v1.XmlPullParser; 030import org.xmlpull.v1.XmlPullParserException; 031 032/** 033 * Extension Provider for Page layout of forms. 034 * 035 * @author Anno van Vliet 036 * 037 */ 038public class DataLayoutProvider { 039 040 public static DataLayout parse(XmlPullParser parser) throws XmlPullParserException, IOException { 041 DataLayout dataLayout = new DataLayout(parser.getAttributeValue("", "label")); 042 parseLayout(dataLayout.getPageLayout(), parser); 043 return dataLayout; 044 } 045 046 private static Section parseSection(XmlPullParser parser) throws XmlPullParserException, IOException { 047 Section layout = new Section(parser.getAttributeValue("", "label")); 048 parseLayout(layout.getSectionLayout(), parser); 049 return layout; 050 } 051 052 private static void parseLayout(List<DataFormLayoutElement> layout, XmlPullParser parser) throws XmlPullParserException, IOException { 053 final int initialDepth = parser.getDepth(); 054 outerloop: while (true) { 055 int eventType = parser.next(); 056 switch (eventType) { 057 case XmlPullParser.START_TAG: 058 switch (parser.getName()) { 059 case Text.ELEMENT: 060 layout.add(new Text(parser.nextText())); 061 break; 062 case Section.ELEMENT: 063 layout.add(parseSection(parser)); 064 break; 065 case Fieldref.ELEMENT: 066 layout.add(parseFieldref(parser)); 067 break; 068 case Reportedref.ELEMENT: 069 layout.add(new Reportedref()); 070 break; 071 default: 072 break; 073 } 074 break; 075 case XmlPullParser.END_TAG: 076 if (parser.getDepth() == initialDepth) { 077 break outerloop; 078 } 079 break; 080 } 081 } 082 } 083 084 private static Fieldref parseFieldref(XmlPullParser parser) throws XmlPullParserException, IOException { 085 final int initialDepth = parser.getDepth(); 086 Fieldref fieldref = new Fieldref(parser.getAttributeValue("", "var")); 087 outerloop: while (true) { 088 int eventType = parser.next(); 089 if (eventType == XmlPullParser.END_TAG && parser.getDepth() == initialDepth) { 090 break outerloop; 091 } 092 } 093 return fieldref; 094 } 095 096}