*/
package fr.gouv.finances.dgfip.xemelios.batch.imports;
-import fr.gouv.finances.dgfip.xemelios.utils.XmlUtils;
-import fr.gouv.finances.dgfip.utils.Pair;
-import fr.gouv.finances.dgfip.utils.xml.FactoryProvider;
-import fr.gouv.finances.dgfip.utils.xml.NamespaceContextImpl;
-import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
-import fr.gouv.finances.dgfip.xemelios.common.config.EtatModel;
import java.io.File;
import java.io.IOException;
import java.util.Stack;
+
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
+
import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.ext.DefaultHandler2;
+import fr.gouv.finances.dgfip.utils.Pair;
+import fr.gouv.finances.dgfip.utils.xml.FactoryProvider;
+import fr.gouv.finances.dgfip.utils.xml.NamespaceContextImpl;
+import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
+import fr.gouv.finances.dgfip.xemelios.common.config.EtatModel;
+import fr.gouv.finances.dgfip.xemelios.utils.XmlUtils;
+
/**
- *
+ *
* @author chm
*/
public class BudgCollParser {
- public static final transient String IGNORABLE_ERROR = "This is not an error, only an expected termination";
- private File xmlFile;
- private DocumentModel documentModel;
- private String collectivite, collectiviteLib, budget, budgetLib, errorMessage;
- private boolean inError = false;
+ public static final transient String IGNORABLE_ERROR = "This is not an error, only an expected termination";
+ private final File xmlFile;
+ private final DocumentModel documentModel;
+ private String collectivite, collectiviteLib, budget, budgetLib, errorMessage;
+ private boolean inError = false;
+
+ public BudgCollParser(final File xmlFile, final DocumentModel documentModel) {
+ super();
+ this.xmlFile = xmlFile;
+ this.documentModel = documentModel;
+ }
- public BudgCollParser(File xmlFile, DocumentModel documentModel) {
- super();
- this.xmlFile = xmlFile;
- this.documentModel = documentModel;
- }
+ /**
+ * Start investigation process
+ */
+ public void investigate() {
+ final SAXParserFactory saxFactory = FactoryProvider.getSaxParserFactory();
+ final ParserHandler ph = new ParserHandler(this.documentModel);
+ try {
+ final SAXParser parser = saxFactory.newSAXParser();
+ parser.parse(this.xmlFile, ph);
+ } catch (final ParserConfigurationException pcEx) {
+ pcEx.printStackTrace();
+ } catch (final SAXException saxEx) {
+ if (!saxEx.getMessage().equals(IGNORABLE_ERROR)) {
+ // file is not valid
+ this.setError(saxEx.getMessage());
+ }
+ } catch (final IOException ioEx) {
+ // should never happen
+ ioEx.printStackTrace();
+ } finally {
+ if (ph != null && !this.inError) {
+ if (ph.getCollectivite() != null) {
+ if (ph.collectivite.key != null && ph.collectivite.key.length() > 0 && (this.collectivite == null || this.collectivite.length() == 0)) {
+ this.collectivite = ph.collectivite.key;
+ }
+ if (ph.collectivite.libelle != null && ph.collectivite.libelle.length() > 0 && (this.collectiviteLib == null || this.collectiviteLib.length() == 0)) {
+ this.collectiviteLib = ph.collectivite.libelle;
+ }
+ }
+ if (ph.budget != null) {
+ if (ph.budget.key != null && ph.budget.key.length() > 0 && (this.budget == null || this.budget.length() == 0)) {
+ this.budget = ph.budget.key;
+ }
+ if (ph.budget.libelle != null && ph.budget.libelle.length() > 0 && (this.budgetLib == null || this.budgetLib.length() == 0)) {
+ this.budgetLib = ph.budget.libelle;
+ }
+ }
+ }
+ }
+ }
- /**
- * Start investigation process
- */
- public void investigate() {
- SAXParserFactory saxFactory = FactoryProvider.getSaxParserFactory();
- ParserHandler ph = new ParserHandler(documentModel);
- try {
- SAXParser parser = saxFactory.newSAXParser();
- parser.parse(xmlFile, ph);
- } catch (ParserConfigurationException pcEx) {
- pcEx.printStackTrace();
- } catch (SAXException saxEx) {
- if (!saxEx.getMessage().equals(IGNORABLE_ERROR)) {
- // file is not valid
- setError(saxEx.getMessage());
- }
- } catch(IOException ioEx) {
- // should never happen
- ioEx.printStackTrace();
- } finally {
- if(ph!=null && !inError) {
- if(ph.getCollectivite()!=null) {
- if(ph.collectivite.key!=null && ph.collectivite.key.length()>0 && (collectivite==null || collectivite.length()==0)) {
- collectivite = ph.collectivite.key;
- }
- if(ph.collectivite.libelle!=null && ph.collectivite.libelle.length()>0 && (collectiviteLib==null || collectiviteLib.length()==0)) {
- collectiviteLib = ph.collectivite.libelle;
- }
- }
- if(ph.budget!=null) {
- if(ph.budget.key!=null && ph.budget.key.length()>0 && (budget==null || budget.length()==0)) {
- budget = ph.budget.key;
- }
- if(ph.budget.libelle!=null && ph.budget.libelle.length()>0 && (budgetLib==null || budgetLib.length()==0)) {
- budgetLib = ph.budget.libelle;
- }
- }
- }
- }
- }
+ public String getBudget() {
+ return this.budget;
+ }
- public String getBudget() {
- return budget;
- }
+ public void setBudget(final String budget) {
+ this.budget = budget;
+ }
- public void setBudget(String budget) {
- this.budget = budget;
- }
+ public String getBudgetLib() {
+ return this.budgetLib;
+ }
- public String getBudgetLib() {
- return budgetLib;
- }
+ public void setBudgetLib(final String budgetLib) {
+ this.budgetLib = budgetLib;
+ }
- public void setBudgetLib(String budgetLib) {
- this.budgetLib = budgetLib;
- }
+ public String getCollectivite() {
+ return this.collectivite;
+ }
- public String getCollectivite() {
- return collectivite;
- }
+ public void setCollectivite(final String collectivite) {
+ this.collectivite = collectivite;
+ }
- public void setCollectivite(String collectivite) {
- this.collectivite = collectivite;
- }
+ public String getCollectiviteLib() {
+ return this.collectiviteLib;
+ }
- public String getCollectiviteLib() {
- return collectiviteLib;
- }
+ public void setCollectiviteLib(final String collectiviteLib) {
+ this.collectiviteLib = collectiviteLib;
+ }
- public void setCollectiviteLib(String collectiviteLib) {
- this.collectiviteLib = collectiviteLib;
- }
+ public void setError(final String errorMessage) {
+ this.inError = true;
+ this.errorMessage = errorMessage;
+ }
- public void setError(String errorMessage) {
- inError = true;
- this.errorMessage = errorMessage;
- }
+ public String getErrorMessage() {
+ return this.errorMessage;
+ }
- public String getErrorMessage() {
- return errorMessage;
- }
+ public boolean isInError() {
+ return this.inError;
+ }
- public boolean isInError() {
- return inError;
- }
+ private static class ParserHandler extends DefaultHandler2 {
+ private static final Logger logger = Logger.getLogger(ParserHandler.class);
+ private final NamespaceContextImpl nsCtx;
+ private final Stack<String> tagStack = new Stack<String>();
+ private final Stack<StringBuilder> charactersStack = new Stack<StringBuilder>();
- private static class ParserHandler extends DefaultHandler2 {
- private static final Logger logger = Logger.getLogger(ParserHandler.class);
- private NamespaceContextImpl nsCtx;
- private Stack<String> tagStack = new Stack<String>();
- private Stack<StringBuilder> charactersStack = new Stack<StringBuilder>();
+ private final DocumentModel dm;
+ private Pair budget, collectivite;
- private DocumentModel dm;
- private Pair budget, collectivite;
+ public ParserHandler(final DocumentModel dm) {
+ super();
+ this.dm = dm;
+ this.nsCtx = new NamespaceContextImpl();
+ }
- public ParserHandler(DocumentModel dm) {
- super();
- this.dm = dm;
- nsCtx = new NamespaceContextImpl();
- }
+ @Override
+ public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
+ final String tagName = XmlUtils.getShortTagName(uri, localName, qName);
+ switch (this.tagStack.size()) {
+ case 0: {
+ // si ce n'est pas le tag du doc que l'on importe, bye
+ if (!tagName.equals(this.dm.getBalise())) {
+ throw new SAXException("Le fichier importé n'est pas un " + this.dm.getTitre());
+ }
+ break;
+ }
+ case 1: {
+ if (this.dm.getEntetes().contains(tagName)) {
+ } else if (this.dm.getReferentiel().equals(tagName)) {
+ } else {
+ final EtatModel em = this.dm.getEtatByTagName(tagName);
+ if (em != null) {
+ throw new SAXException(IGNORABLE_ERROR);
+ }
+ }
+ break;
+ }
+ case 2: {
+ break;
+ }
+ }
+ this.tagStack.push(tagName);
+ this.charactersStack.push(new StringBuilder());
- @Override
- public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
- String tagName = XmlUtils.getShortTagName(uri, localName, qName);
- switch (tagStack.size()) {
- case 0: {
- // si ce n'est pas le tag du doc que l'on importe, bye
- if (!tagName.equals(dm.getBalise())) {
- throw new SAXException("Le fichier importé n'est pas un " + dm.getTitre());
- }
- break;
- }
- case 1: {
- if (dm.getEntetes().contains(tagName)) {
- } else if (dm.getReferentiel().equals(tagName)) {
- } else {
- EtatModel em = dm.getEtatByTagName(tagName);
- if(em!=null) throw new SAXException(IGNORABLE_ERROR);
- }
- break;
- }
- case 2: {
- break;
- }
- }
- tagStack.push(tagName);
- charactersStack.push(new StringBuilder());
+ final String path = XmlUtils.getPath(this.tagStack);
+ for (int i = 0; i < attributes.getLength(); i++) {
+ final String attrName = attributes.getLocalName(i);
+ final String value = attributes.getValue(i);
+ final String fullPath = path + "/@" + attrName;
+ this.checkBudgetCollectivite(fullPath, value);
+ }
+ }
- String path = XmlUtils.getPath(tagStack);
- for (int i = 0; i < attributes.getLength(); i++) {
- String attrName = attributes.getLocalName(i);
- String value = attributes.getValue(i);
- String fullPath = path + "/@" + attrName;
- checkBudgetCollectivite(fullPath, value);
- }
- }
- @Override
- public void endElement(String uri, String localName, String qName) throws SAXException {
- String tagName = XmlUtils.getShortTagName(uri, localName, qName);
- if(tagStack.size()==1) {
- if (!tagName.equals(dm.getBalise())) {
- throw new SAXException("Fermeture d'un element root inattendu");
- }
- }
- String path = XmlUtils.getPath(tagStack);
- String innerData = charactersStack.pop().toString();
- checkBudgetCollectivite(path, innerData);
- tagStack.pop();
- }
+ @Override
+ public void endElement(final String uri, final String localName, final String qName) throws SAXException {
+ final String tagName = XmlUtils.getShortTagName(uri, localName, qName);
+ if (this.tagStack.size() == 1) {
+ if (!tagName.equals(this.dm.getBalise())) {
+ throw new SAXException("Fermeture d'un element root inattendu");
+ }
+ }
+ final String path = XmlUtils.getPath(this.tagStack);
+ final String innerData = this.charactersStack.pop().toString();
+ this.checkBudgetCollectivite(path, innerData);
+ this.tagStack.pop();
+ }
- @Override
- public void endPrefixMapping(String prefix) throws SAXException {
- super.endPrefixMapping(prefix);
- nsCtx.removeMapping(prefix);
- }
+ @Override
+ public void endPrefixMapping(final String prefix) throws SAXException {
+ super.endPrefixMapping(prefix);
+ this.nsCtx.removeMapping(prefix);
+ }
- @Override
- public void startPrefixMapping(String prefix, String uri) throws SAXException {
- super.startPrefixMapping(prefix, uri);
- nsCtx.addMapping(prefix, uri);
- }
+ @Override
+ public void startPrefixMapping(final String prefix, final String uri) throws SAXException {
+ super.startPrefixMapping(prefix, uri);
+ this.nsCtx.addMapping(prefix, uri);
+ }
- private void checkBudgetCollectivite(String path, String value) throws SAXException {
-// logger.debug("checking coll/budg at"+path);
- if (dm.getBudgetPath() != null) {
- if (dm.getBudgetPath().getCodePath() != null && path.equals(dm.getBudgetPath().getCodePath().getPath())) {
- if (budget == null) {
- budget = new Pair();
- }
- budget.key = value;
- }
- if (dm.getBudgetPath().getLibellePath() != null && path.equals(dm.getBudgetPath().getLibellePath().getPath())) {
- if (budget == null) {
- budget = new Pair();
- }
- budget.libelle = value;
- }
- }
- if (dm.getCollectivitePath() != null) {
- if (dm.getCollectivitePath().getCodePath() != null && path.equals(dm.getCollectivitePath().getCodePath().getPath())) {
- if (collectivite == null) {
- collectivite = new Pair();
- }
- collectivite.key = value;
- }
- if (dm.getCollectivitePath().getLibellePath() != null && path.equals(dm.getCollectivitePath().getLibellePath().getPath())) {
- if (collectivite == null) {
- collectivite = new Pair();
- }
- collectivite.libelle = value;
- }
- }
- if(collectivite!=null &&collectivite.key!=null && collectivite.key.length()>0 && collectivite.libelle!=null && collectivite.libelle.length()>0
- && budget!=null && budget.key!=null && budget.key.length()>0 && budget.libelle!=null && budget.libelle.length()>0) {
- throw new SAXException(IGNORABLE_ERROR);
- }
- }
+ private void checkBudgetCollectivite(final String path, final String value) throws SAXException {
+ // logger.debug("checking coll/budg at"+path);
+ if (this.dm.getBudgetPath() != null) {
+ if (this.dm.getBudgetPath().getCodePath() != null && path.equals(this.dm.getBudgetPath().getCodePath().getPath())) {
+ if (this.budget == null) {
+ this.budget = new Pair();
+ }
+ this.budget.key = value;
+ }
+ if (this.dm.getBudgetPath().getLibellePath() != null && path.equals(this.dm.getBudgetPath().getLibellePath().getPath())) {
+ if (this.budget == null) {
+ this.budget = new Pair();
+ }
+ this.budget.libelle = value;
+ }
+ }
+ if (this.dm.getCollectivitePath() != null) {
+ if (this.dm.getCollectivitePath().getCodePath() != null && path.equals(this.dm.getCollectivitePath().getCodePath().getPath())) {
+ if (this.collectivite == null) {
+ this.collectivite = new Pair();
+ }
+ this.collectivite.key = value;
+ }
+ if (this.dm.getCollectivitePath().getLibellePath() != null && path.equals(this.dm.getCollectivitePath().getLibellePath().getPath())) {
+ if (this.collectivite == null) {
+ this.collectivite = new Pair();
+ }
+ this.collectivite.libelle = value;
+ }
+ }
+ if (this.collectivite != null && this.collectivite.key != null && this.collectivite.key.length() > 0 && this.collectivite.libelle != null && this.collectivite.libelle.length() > 0 && this.budget != null && this.budget.key != null && this.budget.key.length() > 0
+ && this.budget.libelle != null && this.budget.libelle.length() > 0) {
+ throw new SAXException(IGNORABLE_ERROR);
+ }
+ }
- public Pair getBudget() {
- return budget;
- }
+ public Pair getBudget() {
+ return this.budget;
+ }
- public Pair getCollectivite() {
- return collectivite;
- }
- }
+ public Pair getCollectivite() {
+ return this.collectivite;
+ }
+ }
}