--- /dev/null
+/*
+ *
+ * Copyright
+ * 2012 axYus - www.axyus.com
+ * 2012 Christophe Marchand <christophe.marchand@axyus.com>
+ *
+ * This file is part of XEMELIOS.
+ *
+ * XEMELIOS is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * XEMELIOS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with XEMELIOS; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+package fr.gouv.finances.dgfip.xemelios.rsa.importer;
+
+import fr.gouv.finances.cp.utils.PropertiesExpansion;
+import fr.gouv.finances.dgfip.utils.Pair;
+import fr.gouv.finances.dgfip.utils.xml.SAXWriter;
+import fr.gouv.finances.dgfip.xemelios.auth.XemeliosUser;
+import fr.gouv.finances.dgfip.xemelios.common.Constants;
+import fr.gouv.finances.dgfip.xemelios.common.FileInfo;
+import fr.gouv.finances.dgfip.xemelios.importers.DefaultImporter;
+import fr.gouv.finances.dgfip.xemelios.utils.XmlUtils;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.util.Stack;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ *
+ * @author Christophe Marchand <christophe.marchand@axyus.com>
+ */
+public class RSARecetteImporter extends DefaultImporter {
+ private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(RSARecetteImporter.class);
+
+ public static final String COLLECTIVITE_TYPE_PATH = "/Racine/InfosCreanceFoyerRSA/IdentificationRSA/Partenaire/TYPEPARTE";
+ public static final String COLLECTIVITE_LIB_PATH = "/Racine/InfosCreanceFoyerRSA/IdentificationRSA/Partenaire/IDEPARTE";
+ public static final String BUDGET_PATH1 = "/Racine/InfosCreanceFoyerRSA/IdentificationRSA/Organisme/FONORG";
+ public static final String BUDGET_PATH2 = "/Racine/InfosCreanceFoyerRSA/IdentificationRSA/Organisme/NUMORG";
+ private static final String EXPECTED_ERROR = "EXPECTED_ERROR";
+ public static final String ETAT_BALISE = "CreancesRSA";
+ private File outputTmpFile;
+
+ public RSARecetteImporter(XemeliosUser user, PropertiesExpansion applicationProperties) {
+ super(user, applicationProperties);
+ }
+
+ @Override
+ protected FileInfo importFile(File f) throws Exception {
+ getImpSvcProvider().startLongWait();
+ File newFile = wrapFile(f);
+ getImpSvcProvider().endLongWait();
+ FileInfo fi = super.importFile(newFile);
+ newFile.delete();
+ return fi;
+
+ }
+
+ @Override
+ public void done() {
+ super.done();
+ }
+
+
+
+ protected File wrapFile(File f) {
+ SAXParserFactory fact = SAXParserFactory.newInstance();
+ fact.setNamespaceAware(true);
+ outputTmpFile = new File(getTmpDir(),f.getName());
+ while(outputTmpFile.exists()) {
+ if(outputTmpFile.equals(f)) {
+ File tmpDir = outputTmpFile.getParentFile();
+ tmpDir = new File(tmpDir,"TMP-DIR");
+ tmpDir.mkdirs();
+ outputTmpFile = new File(tmpDir,f.getName());
+ } else {
+ outputTmpFile.delete();
+ }
+ }
+ FileOutputStream fos = null;
+ try {
+ SAXParser collParser = fact.newSAXParser();
+ CollHandler handler = new CollHandler(getDocumentModel().getNamespaces());
+ String collectivite="";
+ try {
+ collParser.parse(f, handler);
+ } catch(SAXException saxEx) {
+ if(EXPECTED_ERROR.equals(saxEx.getMessage())) {
+ collectivite = handler.getCollectivite();
+ } else {
+ throw saxEx;
+ }
+ }
+ fos = new FileOutputStream(outputTmpFile);
+ String encoding = getFileEncoding(f);
+ final String passedColl = collectivite;
+ final Pair budget = new Pair(handler.getBudgetCode(),handler.getBudgetLibelle());
+ SAXWriter writer = new SAXWriter(fos, encoding) {
+ private boolean alreadyCreatedEtat = false;
+ @Override
+ public void startDocument() throws SAXException {
+ super.startDocument();
+ startPrefixMapping("added", Constants.ADDED_NS_URI);
+ }
+ @Override
+ public void endDocument() throws SAXException {
+ endPrefixMapping("added");
+ super.endDocument();
+ }
+
+ @Override
+ public void processingInstruction(String target, String data) throws SAXException {
+ }
+
+ @Override
+ public void endElement(String uri, String localName, String qName) throws SAXException {
+ super.endElement(uri, localName, qName);
+ }
+
+ @Override
+ public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
+ if(!alreadyCreatedEtat && localName.equals("InfosCreanceFoyerRSA")) {
+ startElement(XMLConstants.NULL_NS_URI, ETAT_BALISE, new QName(XMLConstants.NULL_NS_URI, ETAT_BALISE,"").toString(), new AttributesImpl());
+ alreadyCreatedEtat=true;
+ }
+ if(alreadyCreatedEtat && localName.equals("TransmissionFlux")) {
+ endElement(XMLConstants.NULL_NS_URI, ETAT_BALISE, new QName(XMLConstants.NULL_NS_URI, ETAT_BALISE,"").toString());
+ }
+ super.startElement(uri, localName, qName, atts);
+ if(localName.equals("Racine")) {
+ startElement(Constants.ADDED_NS_URI, "Collectivite", new QName(Constants.ADDED_NS_URI, "Collectivite", "added").toString(),new AttributesImpl());
+ characters(passedColl.toCharArray(), 0, passedColl.length());
+ endElement(Constants.ADDED_NS_URI, "Collectivite", new QName(Constants.ADDED_NS_URI, "Collectivite", "added").toString());
+ AttributesImpl attrs = new AttributesImpl();
+ attrs.addAttribute(Constants.ADDED_NS_URI, "code", "", "xs:string", budget.key);
+ attrs.addAttribute(Constants.ADDED_NS_URI, "libelle", "", "xs:string", budget.libelle);
+ startElement(Constants.ADDED_NS_URI, "Budget", new QName(Constants.ADDED_NS_URI, "Budget", "added").toString(),attrs);
+ endElement(Constants.ADDED_NS_URI, "Budget", new QName(Constants.ADDED_NS_URI, "Budget", "added").toString());
+ }
+ }
+
+
+ };
+ writer.setPrettyPrint(false);
+ SAXParser parser = fact.newSAXParser();
+ parser.parse(f, writer);
+
+ } catch(Exception ex) {
+ logger.error("wrapFile(File):",ex);
+ } finally {
+ try {
+ fos.flush();
+ fos.close();
+ } catch(Throwable t) {}
+ }
+ return outputTmpFile;
+ }
+
+ private class CollHandler extends DefaultHandler {
+ Stack<QName> stack;
+ NamespaceContext nsCtx;
+ Stack<StringBuffer> textContent;
+ private StringBuffer collectivite, budgetLib;
+ private String collFirstPart, collSecondPart, budgFirstPart, budgSecondPart, budgCode;
+ private boolean collFirstPartDone=false, collSecondPartDone=false, budgFirstPartDone=false, budgSecondPartDone=false;
+
+ public String getCollectivite() { return collectivite.toString(); }
+ public String getBudgetCode() { return budgCode; }
+ public String getBudgetLibelle() { return budgetLib.toString(); }
+
+ public CollHandler(NamespaceContext nsCtx) {
+ super();
+ this.nsCtx=nsCtx;
+ stack = new Stack<QName>();
+ textContent = new Stack<StringBuffer>();
+ collectivite = new StringBuffer();
+ budgetLib = new StringBuffer();
+ }
+
+ @Override
+ public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
+ super.startElement(uri, localName, qName, attributes);
+ QName qname = XmlUtils.getQName(uri, localName, qName);
+ stack.push(qname);
+ textContent.push(new StringBuffer());
+ }
+
+ @Override
+ public void endElement(String uri, String localName, String qName) throws SAXException {
+ super.endElement(uri, localName, qName);
+ String pathTag = XmlUtils.getPath(stack, nsCtx);
+ if(COLLECTIVITE_TYPE_PATH.equals(pathTag)) {
+ collFirstPart = textContent.peek().toString().trim();
+ collFirstPartDone = true;
+ } else if(COLLECTIVITE_LIB_PATH.equals(pathTag)) {
+ collSecondPart = textContent.peek().toString().trim();
+ collSecondPartDone = true;
+ } else if(BUDGET_PATH1.equals(pathTag)) {
+ budgFirstPart = textContent.peek().toString().trim();
+ budgFirstPartDone = true;
+ } else if(BUDGET_PATH2.equals(pathTag)) {
+ budgSecondPart = textContent.peek().toString().trim();
+ budgSecondPartDone = true;
+ }
+ if(collFirstPartDone && collSecondPartDone && budgFirstPartDone && budgSecondPartDone) {
+ collectivite.append(collFirstPart).append(collSecondPart);
+ budgetLib.append(budgFirstPart).append(budgSecondPart);
+ throw new SAXException(EXPECTED_ERROR);
+ }
+ stack.pop();
+ }
+
+ @Override
+ public void characters(char[] ch, int start, int length) throws SAXException {
+ super.characters(ch, start, length);
+ String s = new String(ch,start,length);
+ textContent.peek().append(s);
+ }
+ }
+}