00001 package org.openmobileis.modules.profils.embedded.data;
00002
00003 import java.io.IOException;
00004 import java.io.Reader;
00005
00006 import org.kxml2.io.KXmlParser;
00007 import org.openmobileis.common.util.collection.Array;
00008 import org.openmobileis.common.util.log.LogManager;
00009 import org.openmobileis.common.util.log.LogServices;
00010 import org.xmlpull.v1.XmlPullParserException;
00011
00012 public class ProfilParser {
00013
00014 private final static String TAG_XML = "xml";
00015 private final static String TAG_PROFIL = "profil";
00016 private final static String TAG_RUBRIC = "rubric";
00017 private final static String TAG_PROPERTY = "property";
00018 private final static String TAG_MODULE = "module";
00019 private final static String TAG_MODRUBRIC = "modrubric";
00020
00021 private Reader reader;
00022
00023 private ProfilRubric currentRubric = null;
00024 private ProfilModule currentModule = null;
00025
00026 private boolean propertyTagOpened = false;
00027 private String propertyName = null;
00028 private String propertyValue = null;
00029
00030 private String modrubricName = null;
00031
00032 private Array rubrics = new Array(5);
00033 private Array modules = new Array(5);
00034
00035 String currentTag;
00036
00037 public ProfilParser(Reader reader) {
00038 this.reader = reader;
00039 }
00040
00041 public void parse() throws XmlPullParserException, IOException {
00042 KXmlParser parser = new KXmlParser();
00043 parser.setInput(this.reader);
00044
00045 int eventType = parser.getEventType();
00046 do {
00047 if(eventType == KXmlParser.START_DOCUMENT) {
00048 } else if(eventType == KXmlParser.END_DOCUMENT) {
00049 } else if(eventType == KXmlParser.START_TAG) {
00050 this.processStartElement(parser);
00051 } else if(eventType == KXmlParser.END_TAG) {
00052 this.processEndElement(parser);
00053 } else if(eventType == KXmlParser.TEXT) {
00054 this.processText(parser);
00055 }
00056 eventType = parser.next();
00057 } while (eventType != KXmlParser.END_DOCUMENT);
00058 }
00059
00060 private void openRubricTag(KXmlParser parser) {
00061 if (currentRubric != null) {
00062 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, starting a new rubric while the preceding one isn't closed");
00063 }
00064 if (currentModule != null) {
00065 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, starting a new rubric while the preceding module isn't closed");
00066 currentModule = null;
00067 }
00068 currentRubric = new ProfilRubric();
00069 int count = parser.getAttributeCount();
00070 for (int i = 0; i < count; i++) {
00071 if (parser.getAttributeName(i).equals("name")) {
00072 currentRubric.setName(parser.getAttributeValue(i));
00073 } else {
00074 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, unrecognized attribute "+parser.getAttributeName(i)+" for rubric");
00075 }
00076 }
00077 }
00078
00079 private void closeRubricTag() {
00080 if (currentRubric == null) {
00081 LogManager.traceError(LogServices.SYNCHROSERVICE, "Corrupted profil file, found rubric closing tag while no rubric is open");
00082 return;
00083 }
00084 rubrics.add(currentRubric);
00085 currentRubric = null;
00086 }
00087
00088 private void openModuleTag(KXmlParser parser) {
00089 if (currentRubric != null) {
00090 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, starting a new module while the preceding rubric isn't closed");
00091 currentRubric = null;
00092 }
00093 if (currentModule != null) {
00094 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, starting a new module while the preceding one isn't closed");
00095 }
00096 currentModule = new ProfilModule();
00097 int count = parser.getAttributeCount();
00098 for (int i = 0; i < count; i++) {
00099 if (parser.getAttributeName(i).equals("name")) {
00100 currentModule.setName(parser.getAttributeValue(i));
00101 } else if (parser.getAttributeName(i).equals("version")) {
00102 currentModule.setVersion(parser.getAttributeValue(i));
00103 } else {
00104 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, unrecognized attribute "+parser.getAttributeName(i)+" for modrubric");
00105 }
00106 }
00107 }
00108
00109 private void closeModuleTag() {
00110 if (currentModule == null) {
00111 LogManager.traceError(LogServices.SYNCHROSERVICE, "Corrupted profil file, found module closing tag while no module is open");
00112 return;
00113 }
00114 modules.add(currentModule);
00115 currentModule = null;
00116 }
00117
00118 private void openPropertyTag(KXmlParser parser) {
00119 if (currentRubric != null && propertyTagOpened == true) {
00120 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, opening a new rubric property while the preceding one isn't closed");
00121 } else if (currentModule != null && propertyTagOpened == true) {
00122 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, opening a new rubric property while the preceding one isn't closed");
00123 } else if (currentModule == null && currentRubric == null){
00124 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, argument property must be included in a rubric or in a module");
00125 return;
00126 }
00127
00128 propertyTagOpened = true;
00129 propertyName = null;
00130 propertyValue = null;
00131
00132 int count = parser.getAttributeCount();
00133 for (int i = 0; i < count; i++) {
00134 if (parser.getAttributeName(i).equals("name")) {
00135 propertyName = parser.getAttributeValue(i);
00136 } else if (parser.getAttributeName(i).equals("value")) {
00137 propertyValue = parser.getAttributeValue(i);
00138 } else {
00139 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, unrecognized attribute "+parser.getAttributeName(i)+" for modrubric");
00140 }
00141 }
00142
00143 if (propertyName == null) {
00144 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, property tag with no name !");
00145 }
00146 if (propertyValue == null) {
00147 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, property tag "+propertyName+" with no value !");
00148 }
00149 }
00150
00151 private void closePropertyTag() {
00152 if (propertyTagOpened == false) {
00153 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, closing tag property while it's not opened");
00154 } else if (propertyName == null) {
00155 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, closing tag property while it has no name defined");
00156 } else if (propertyValue == null) {
00157 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, closing tag property "+propertyName+" while it has no value defined");
00158 } else {
00159 if (currentRubric != null) {
00160 currentRubric.addProperty(propertyName, propertyValue);
00161 } else if (currentModule != null) {
00162 currentModule.addProperty(propertyName, propertyValue);
00163 } else {
00164 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, property "+propertyName+" outside rubrics and modules");
00165 }
00166 }
00167 propertyTagOpened = false;
00168 propertyName = null;
00169 propertyValue = null;
00170 }
00171
00172 private void openModrubricTag(KXmlParser parser) {
00173 if (currentModule == null) {
00174 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, argument modrubric must be included in a module");;
00175 return;
00176 }
00177
00178 int count = parser.getAttributeCount();
00179 for (int i = 0; i < count; i++) {
00180 if (parser.getAttributeName(i).equals("name")) {
00181 modrubricName = parser.getAttributeValue(i);
00182 } else {
00183 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, unrecognized attribute "+parser.getAttributeName(i)+" for modrubric");
00184 }
00185 }
00186 }
00187
00188 private void closeModrubricTag() {
00189 if (currentModule == null) {
00190 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, argument modrubric must be included in a module");
00191 return;
00192 }
00193 if (modrubricName == null) {
00194 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, argument modrubric must have a name");
00195 return;
00196 }
00197 currentModule.addRubric(modrubricName);
00198 modrubricName = null;
00199 }
00200
00201 private void processStartElement (KXmlParser parser)
00202 {
00203 String name = parser.getName();
00204
00205 if (name.equals(ProfilParser.TAG_XML)) {
00206
00207 } else if (name.equals(ProfilParser.TAG_PROFIL)) {
00208
00209 } else if (name.equals(ProfilParser.TAG_RUBRIC)) {
00210 this.openRubricTag(parser);
00211 } else if (name.equals(ProfilParser.TAG_MODULE)) {
00212 this.openModuleTag(parser);
00213 } else if (name.equals(ProfilParser.TAG_PROPERTY)) {
00214 this.openPropertyTag(parser);
00215 } else if (name.equals(ProfilParser.TAG_MODRUBRIC)) {
00216 this.openModrubricTag(parser);
00217 } else {
00218 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, unknown opening tag : "+name);
00219 }
00220 }
00221
00222
00223 private void processEndElement (KXmlParser parser)
00224 {
00225 String name = parser.getName();
00226
00227 if (name.equals(ProfilParser.TAG_XML)) {
00228
00229 } else if (name.equals(ProfilParser.TAG_PROFIL)) {
00230
00231 } else if (name.equals(ProfilParser.TAG_RUBRIC)) {
00232 this.closeRubricTag();
00233 } else if (name.equals(ProfilParser.TAG_MODULE)) {
00234 this.closeModuleTag();
00235 } else if (name.equals(ProfilParser.TAG_PROPERTY)) {
00236 this.closePropertyTag();
00237 } else if (name.equals(ProfilParser.TAG_MODRUBRIC)) {
00238 this.closeModrubricTag();
00239 } else {
00240 LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, unknown closing tag : "+name);
00241 }
00242 }
00243
00244 private void processText (KXmlParser parser) throws XmlPullParserException
00245 {
00246
00247 }
00248
00249 public Array getRubrics() {
00250 return this.rubrics;
00251 }
00252
00253 public Array getModules() {
00254 return this.modules;
00255 }
00256 }