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.smack.SmackException; 023import org.jivesoftware.smackx.xdatalayout.packet.DataLayout; 024import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.DataFormLayoutElement; 025import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Fieldref; 026import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Reportedref; 027import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Section; 028import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Text; 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 SmackException { 042 DataLayout dataLayout = new DataLayout(parser.getAttributeValue("", "label")); 043 parseLayout(dataLayout.getPageLayout(), parser); 044 return dataLayout; 045 } 046 047 private static Section parseSection(XmlPullParser parser) throws XmlPullParserException, IOException { 048 Section layout = new Section(parser.getAttributeValue("", "label")); 049 parseLayout(layout.getSectionLayout(), parser); 050 return layout; 051 } 052 053 private static void parseLayout(List<DataFormLayoutElement> layout, XmlPullParser parser) throws XmlPullParserException, IOException { 054 final int initialDepth = parser.getDepth(); 055 outerloop: while (true) { 056 int eventType = parser.next(); 057 switch (eventType) { 058 case XmlPullParser.START_TAG: 059 switch (parser.getName()) { 060 case Text.ELEMENT: 061 layout.add(new Text(parser.nextText())); 062 break; 063 case Section.ELEMENT: 064 layout.add(parseSection(parser)); 065 break; 066 case Fieldref.ELEMENT: 067 layout.add(parseFieldref(parser)); 068 break; 069 case Reportedref.ELEMENT: 070 layout.add(new Reportedref()); 071 break; 072 default: 073 break; 074 } 075 break; 076 case XmlPullParser.END_TAG: 077 if (parser.getDepth() == initialDepth) { 078 break outerloop; 079 } 080 break; 081 } 082 } 083 } 084 085 private static Fieldref parseFieldref(XmlPullParser parser) throws XmlPullParserException, IOException { 086 final int initialDepth = parser.getDepth(); 087 Fieldref fieldref = new Fieldref(parser.getAttributeValue("", "var")); 088 outerloop: while (true) { 089 int eventType = parser.next(); 090 if (eventType == XmlPullParser.END_TAG && parser.getDepth() == initialDepth) { 091 break outerloop; 092 } 093 } 094 return fieldref; 095 } 096 097}