Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * Wikitty :: api
   * %%
   * Copyright (C) 2009 - 2010 CodeLutin, Benjamin Poussin
   * %%
   * This program 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 3 of the 
  * License, or (at your option) any later version.
  * 
  * This program 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 General Lesser Public License for more details.
  * 
  * You should have received a copy of the GNU General Lesser Public 
  * License along with this program.  If not, see
  * <http://www.gnu.org/licenses/lgpl-3.0.html>.
  * #L%
  */
 
 package org.nuiton.wikitty.addons.importexport;
 
 import java.io.Reader;
 import java.io.Writer;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 

Author(s):
poussin
Version:
$Revision$ Last update: $Date$ by : $Author$
 
 public class ImportExportXML implements ImportExportMethod {

    
to use log facility, just put in your code: log.info(\"...\");
 
     static private Log log = LogFactory.getLog(ImportExportXML.class);
 
     @Override
     public void importReader(WikittyClient clientReader readerthrows Exception {
         XmlPullParserFactory factory = XmlPullParserFactory.newInstance(
                 System.getProperty(.), null);
         factory.setNamespaceAware(true);
         XmlPullParser xpp = factory.newPullParser();
         xpp.setInput(reader);
 
         WikittyExtension ext = null;
         Wikitty w = null;
         String CDATA = null;
 
         int eventType;
         do {
             eventType = xpp.next();
             String objectVersion = null;
             if (eventType == .) {
                 .info("start XML import at " + new Date());
             } else if (eventType == .) {
                 String name = xpp.getName();
                 if ("extension".equals(name)) {
                     String extName = xpp.getAttributeValue(null"name");
                     String version = xpp.getAttributeValue(null"version");
                     String requires = xpp.getAttributeValue(null"requires");
                     // FIXME poussin 20111120 il faut aussi avoir les tagValues de l'extension
                     ext = new WikittyExtension(extNameversionnullrequires,
                             new LinkedHashMap<StringFieldType>());
                 } else if ("object".equals(name)) {
                     String id = xpp.getAttributeValue(null"id");
                     objectVersion = xpp.getAttributeValue(null"version");
                     String extensions = xpp.getAttributeValue(null"extensions");
                     w = new WikittyImpl(id);
                     String[] extensionList = extensions.split(",");
                     for (String extId : extensionList) {
                        String extName = WikittyExtension.computeName(extId);
                        String extVersion = WikittyExtension.computeVersion(extId);
                        extId = WikittyExtension.computeId(extNameextVersion);
                        WikittyExtension e = client.restoreExtension(extId);
                        if (e == null) {
                            throw new WikittyException("Extension not found : " + extId);
                        }
                        w.addExtension(e);
                    }
                }
            } else if (eventType == .) {
                String name = xpp.getName();
                if ("extension".equals(name)) {
                    client.storeExtension(Collections.singleton(ext));
                    ext = null;
                } else if ("object".equals(name)) {
                    w.setWikittyVersion(objectVersion);
                    client.getWikittyService().store(client.getSecurityToken(),
                            Collections.singleton(w), true);
                    w = null;
                } else if (ext != null && "field".equals(name)) {
                    FieldType type = new FieldType();
                    String fieldName = WikittyUtil.parseField(CDATAtype);
                    ext.addField(fieldNametype);
                } else if (ext != null && "tagvalues".equals(name)) {
                    Map<StringStringtagValues = WikittyUtil.tagValuesToMap(CDATA);
                    ext.setTagValues(tagValues);
                } else if (w != null) {
                    String extensionName = WikittyExtension.extractExtensionName(name);
                    String fieldName = WikittyExtension.extractFieldName(name);
                    FieldType fieldType = w.getFieldType(name);
                    Object v = WikittyUtil.fromStringForExport(fieldTypeCDATA);
                    if (fieldType.isCollection()) {
                        w.addToField(extensionNamefieldNamev);
                    } else {
                        w.setField(extensionNamefieldNamev);
                    }
                }
            } else if (eventType == .) {
                CDATA = xpp.getText();
            }
        } while (eventType != .);
    }
    @Override
    public void exportWriter(WikittyClient clientWriter result,
            WikittyQueryResult<WikittyqueryResultthrows Exception {
        // keep extension already done
        Set<StringextDone = new HashSet<String>();
        result.write("<wikitty>\n");
        List<Wikittywikitties = queryResult.getAll();
        for (Wikitty w : wikitties) {
            String extensionList = "";
            for (WikittyExtension ext : w.getExtensions()) {
                String id = ext.getId();
                extensionList += "," + id;
                if (!extDone.contains(id)) {
                    extDone.add(id);
                    String requires = StringUtils.join(ext.getRequires(), ",");
                    result.write("  <extension name='" + ext.getName()
                            + "' version='" + ext.getVersion()
                            + (StringUtils.isBlank(requires) ? "":"' requires='" + requires)
                            + "'>\n");
                    Map<StringStringtagValues = ext.getTagValues();
                    result.write("    <tagvalues>"
                            + WikittyUtil.tagValuesToString(tagValues)
                            + "</tagvalues>\n");
                    for (String fieldName : ext.getFieldNames()) {
                        String def = ext.getFieldType(fieldName).toDefinition(fieldName);
                        result.write("    <field>" + def + "</field>\n");
                    }
                    result.write("  </extension>\n");
                }
            }
            if (!"".equals(extensionList)) {
                // delete first ','
                extensionList = extensionList.substring(1);
            }
            result.write("  <object id='" + w.getWikittyId() + "' version='" + w.getWikittyVersion() + "' extensions='" + extensionList + "'>\n");
            for (String fieldName : w.fieldNames()) {
                FieldType type = w.getFieldType(fieldName);
                if (type.isCollection()) {
                    Object fqField = w.getFqField(fieldName);
                    if (fqField != null) {
                        for (Object o : (CollectionfqField) {
                            String fqFieldValue = WikittyUtil.toStringForExport(typeo);
                            if (fqFieldValue != null) {
                                fqFieldValue = StringEscapeUtils.escapeXml(fqFieldValue);
                                result.write("    <" + fieldName + ">" + fqFieldValue + "</" + fieldName + ">\n");
                            }
                        }
                    }
                } else {
                    String fqFieldValue = WikittyUtil.toStringForExport(typew.getFqField(fieldName));
                    if (fqFieldValue != null) {
                        fqFieldValue = StringEscapeUtils.escapeXml(fqFieldValue);
                        result.write("    <" + fieldName + ">" + fqFieldValue + "</" + fieldName + ">\n");
                    }
                }
            }
            result.write("  </object>\n");
        }
        result.write("</wikitty>\n");
    }
    @Override
    public void importReader(String securityTokenReader reader,
            WikittyService wsthrows Exception {
        WikittyClient client = new WikittyClient(nullwssecurityToken);
        importReader(clientreader);
    }
    @Override
    public void exportWriter(String securityTokenWriter result,
            WikittyService wsPagedResult<WikittypagedResultthrows Exception {
        WikittyClient client = new WikittyClient(nullwssecurityToken);
        WikittyQueryResult<WikittyqueryResult = new WikittyQueryResult(
                pagedResult.getCriteriaName(), pagedResult.getFirstIndice(),
                pagedResult.getNumFound(), nullpagedResult.getQueryString(),
                pagedResult.getAll(), nullnull,null, 0, 0);
        exportWriter(clientresultqueryResult);
    }
New to GrepCode? Check out our FAQ X