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.xml.XmlPullParser; 023import org.jivesoftware.smack.xml.XmlPullParserException; 024 025import org.jivesoftware.smackx.xdatalayout.packet.DataLayout; 026import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.DataFormLayoutElement; 027import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Fieldref; 028import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Reportedref; 029import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Section; 030import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Text; 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 XmlPullParser.Event eventType = parser.next(); 056 switch (eventType) { 057 case START_ELEMENT: 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 END_ELEMENT: 076 if (parser.getDepth() == initialDepth) { 077 break outerloop; 078 } 079 break; 080 default: 081 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement. 082 break; 083 } 084 } 085 } 086 087 private static Fieldref parseFieldref(XmlPullParser parser) throws XmlPullParserException, IOException { 088 final int initialDepth = parser.getDepth(); 089 Fieldref fieldref = new Fieldref(parser.getAttributeValue("", "var")); 090 outerloop: while (true) { 091 XmlPullParser.Event eventType = parser.next(); 092 if (eventType == XmlPullParser.Event.END_ELEMENT && parser.getDepth() == initialDepth) { 093 break outerloop; 094 } 095 } 096 return fieldref; 097 } 098 099}