r1356 - xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework

Jeremi Joslin jeremi at users.forge.objectweb.org
Tue Oct 3 15:58:49 CEST 2006


Author: jeremi
Date: 2006-10-03 15:58:48 +0200 (Tue, 03 Oct 2006)
New Revision: 1356

Added:
   xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/DefaultImportFilterImpl.java
Modified:
   xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/FrameworkConstant.java
   xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/FrameworkManagerPlugin.java
   xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/ImportFilter.java
Log:
[GELC]
* refactor to be able to define an importer

Added: xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/DefaultImportFilterImpl.java
===================================================================
--- xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/DefaultImportFilterImpl.java	2006-10-03 13:53:24 UTC (rev 1355)
+++ xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/DefaultImportFilterImpl.java	2006-10-03 13:58:48 UTC (rev 1356)
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This 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.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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 this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author jeremi
+ */
+package org.gelc.xwiki.plugins.framework;
+
+import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
+import com.xpn.xwiki.plugin.PluginException;
+import org.dom4j.Document;
+import org.dom4j.DocumentException;
+import org.dom4j.Element;
+import org.dom4j.io.SAXReader;
+
+import java.io.InputStream;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+
+public class DefaultImportFilterImpl implements ImportFilter, FrameworkConstant{
+
+    public String getPageName(Object item, XWikiContext context) {
+        Element itemEl = (Element) item;
+        return itemEl.element(LEARNING_STANDARD_DOCUMENT_ITEM_IDENTIFIER).getText();
+    }    
+
+    public Framework readFramework(String frameworkName, InputStream iStream, XWikiContext context) throws XWikiException {
+        SAXReader reader = new SAXReader();
+        Document domdoc;
+
+        try {
+            domdoc = reader.read(iStream);
+        } catch (DocumentException e) {
+            throw new PluginException(PLUGIN_NAME, ERROR_FRAMEWORK_CANNOT_IMPORT_DOCUMENT, "Cannont read the xml file of the framework to import", e);
+        }
+        Element docEl = domdoc.getRootElement().element(LEARNING_STANDARD_DOCUMENT_ROOT);
+        context.put(CONTEXT_KEY_IMPORT_DOC_EL, docEl);
+        Framework doc = (Framework) context.getWiki().getDocument(FRAMEWORK_PREFIX + frameworkName, FRAMEWORK_HOME, context).newDocument(Framework.class.getName(), context);
+        if (!doc.isNew())
+            throw new PluginException(PLUGIN_NAME, ERROR_FRAMEWORK_ALREADY_EXIST, "the framework " + frameworkName + " already exist, choose another name");
+        doc.setTitle(docEl.element(LEARNING_STANDARD_DOCUMENT_TITLE).getText());
+        doc.setCustomClass(Framework.class.getName());
+        doc.newObject(FRAMEWORK_CLASS_FULLNAME);
+        return doc;
+    }
+
+    public Collection readFrameworkItems(Framework framework, InputStream iStream, XWikiContext context) throws XWikiException {
+        Element el = (Element) context.get(CONTEXT_KEY_IMPORT_DOC_EL);
+        List items = el.elements(LEARNING_STANDARD_DOCUMENT_ITEM);
+        Map itemsDoc = new HashMap();
+        Iterator it = items.iterator();
+        while(it.hasNext()){
+            Element itemEl = (Element) it.next();
+            FrameworkItem item = readFrameworkItem(itemEl, this, framework.getWeb(), context);
+            itemsDoc.put(item.getIdentifier(context), item);
+        }
+
+        resetItemsParents(itemsDoc, context);
+
+        return itemsDoc.values();
+    }
+
+    private FrameworkItem readFrameworkItem(Element itemEl, ImportFilter filter, String space, XWikiContext context) throws XWikiException {
+        FrameworkItem doc = new FrameworkItem(context.getWiki().getDocument(space, filter.getPageName(itemEl, context), context), context);
+        if (!doc.isNew())
+            throw new PluginException(PLUGIN_NAME, ERROR_FRAMEWORK_ITEM_ALREADY_EXIST, "the framework item " + itemEl + " already exist, choose another name");
+
+        Element descEl = itemEl.element(LEARNING_STANDARD_DOCUMENT_ITEM_DESCRIPTION);
+        if (descEl != null && descEl.element(LEARNING_STANDARD_DOCUMENT_ITEM_TEXT) != null){
+
+            String title = descEl.element(LEARNING_STANDARD_DOCUMENT_ITEM_TEXT).getText();
+            if (title.length() > 250)
+                title = title.substring(0, 250);
+            doc.setTitle(title);
+        }
+
+        doc.setIdentifier(itemEl.element(LEARNING_STANDARD_DOCUMENT_ITEM_IDENTIFIER).getText());
+
+        if (itemEl.element(LEARNING_STANDARD_DOCUMENT_ITEM_PARENT) != null)
+            doc.setParentIdentifier(itemEl.element(LEARNING_STANDARD_DOCUMENT_ITEM_PARENT).getText());
+
+        doc.setParent(space + "." + FRAMEWORK_HOME);
+        doc.setCustomClass(FrameworkItem.class.getName());
+        return doc;
+    }
+
+    private void resetItemsParents(Map items, XWikiContext context) {
+        Iterator it = items.values().iterator();
+        while(it.hasNext()){
+            FrameworkItem doc = (FrameworkItem) it.next();
+            String id = doc.getParentIdentifier(context);
+            if (id != null && id.length() > 0)
+            {
+                FrameworkItem parentItem = (FrameworkItem) items.get(id);
+                doc.setParent(parentItem.getFullName());
+            }
+        }
+    }
+}


Property changes on: xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/DefaultImportFilterImpl.java
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/FrameworkConstant.java
===================================================================
--- xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/FrameworkConstant.java	2006-10-03 13:53:24 UTC (rev 1355)
+++ xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/FrameworkConstant.java	2006-10-03 13:58:48 UTC (rev 1356)
@@ -1,3 +1,24 @@
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This 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.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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 this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author jeremi
+ */
 package org.gelc.xwiki.plugins.framework;
 
 /**
@@ -9,6 +30,8 @@
  */
 public interface FrameworkConstant {
 
+    public final static String PLUGIN_NAME = "framework_manager";
+
     public final static String FRAMEWORK_HOME = "WebHome";
     public final static String FRAMEWORK_PREFIX = "FW_";
 
@@ -41,9 +64,9 @@
     public final static int ERROR_FRAMEWORK_ITEM_ALREADY_EXIST = 4;
     public final static int ERROR_FRAMEWORK_PATH_ERROR = 5;
     public final static int ERROR_FRAMEWORK_RECURSIVE_PATH = 6;
+    public final static int ERROR_FRAMEWORK_INPUTSTREAM_MARK_NOT_IMPLEMENTED = 7;
 
 
-
     public final static int INTEGRITY_CHECK_LEVEL_WARNING = 0;
     public final static int INTEGRITY_CHECK_LEVEL_ERROR = 1;
 
@@ -57,4 +80,7 @@
 
     public final static String CONTEXT_KEY_ERRORS_MSG = "FrameworkManagerPlugin_Errors";
     public final static String CONTEXT_KEY_ERRORS_CODE = "FrameworkManagerPlugin_ErrorsCode";
+    public final static String CONTEXT_KEY_IMPORT_FILTER = "FrameworkManagerPlugin_ImportFilter";
+
+    public final static String CONTEXT_KEY_IMPORT_DOC_EL = "FrameworkManagerPlugin_DocumentElement";
 }

Modified: xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/FrameworkManagerPlugin.java
===================================================================
--- xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/FrameworkManagerPlugin.java	2006-10-03 13:53:24 UTC (rev 1355)
+++ xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/FrameworkManagerPlugin.java	2006-10-03 13:58:48 UTC (rev 1356)
@@ -1,3 +1,24 @@
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This 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.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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 this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author jeremi
+ */
 package org.gelc.xwiki.plugins.framework;
 
 import com.xpn.xwiki.XWiki;
@@ -9,22 +30,18 @@
 import com.xpn.xwiki.plugin.PluginException;
 import com.xpn.xwiki.plugin.XWikiDefaultPlugin;
 import com.xpn.xwiki.plugin.XWikiPluginInterface;
-import org.dom4j.Document;
-import org.dom4j.DocumentException;
-import org.dom4j.Element;
-import org.dom4j.io.SAXReader;
 
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Collection;
 import java.util.Vector;
 
 
-public class FrameworkManagerPlugin   extends XWikiDefaultPlugin implements XWikiPluginInterface, FrameworkConstant {
+public class FrameworkManagerPlugin extends XWikiDefaultPlugin implements XWikiPluginInterface, FrameworkConstant {
     private Map errorMessageMap = null;
     private Map errorLevelMap = null;
 
@@ -49,7 +66,7 @@
     }
 
     public String getName() {
-        return "framework_manager";
+        return PLUGIN_NAME;
     }
 
     public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) {
@@ -60,20 +77,18 @@
         return false;
     }
 
-    public boolean importFramework(String frameworkName, InputStream xmlStream, XWikiContext context) throws XWikiException {
-        SAXReader reader = new SAXReader();
-        Document domdoc;
+    public boolean importFramework(String frameworkName, InputStream iStream, XWikiContext context) throws XWikiException {
 
-        try {
-            domdoc = reader.read(xmlStream);
-        } catch (DocumentException e) {
-            throw new PluginException(getName(), ERROR_FRAMEWORK_CANNOT_IMPORT_DOCUMENT, "Cannont read the xml file of the framework to import", e);
+
+/*        if (!iStream.markSupported()){
+            throw new PluginException(getName(), ERROR_FRAMEWORK_INPUTSTREAM_MARK_NOT_IMPLEMENTED, "");
         }
-        Element docEl = domdoc.getRootElement().element(LEARNING_STANDARD_DOCUMENT_ROOT);
-        com.xpn.xwiki.api.Document frameworkDoc = readFramework(frameworkName, docEl, context);
+        iStream.mark(Integer.MAX_VALUE);   */
 
-        Collection items = readFrameworkItems(frameworkDoc.getWeb(), docEl, context);
+        Framework frameworkDoc = getFilter(context).readFramework(frameworkName, iStream, context);
 
+        Collection items = getFilter(context).readFrameworkItems(frameworkDoc, iStream, context);
+
         frameworkDoc.save();
 
         saveAllItems(items, context);
@@ -89,74 +104,12 @@
         }
     }
 
-    private com.xpn.xwiki.api.Document readFramework(String frameworkName, Element el, XWikiContext context) throws XWikiException {
-        com.xpn.xwiki.api.Document doc = context.getWiki().getDocument(FRAMEWORK_PREFIX + frameworkName, FRAMEWORK_HOME, context).newDocument(context);
-        if (!doc.isNew())
-            throw new PluginException(getName(), ERROR_FRAMEWORK_ALREADY_EXIST, "the framework " + frameworkName + " already exist, choose another name");
-        doc.setTitle(el.element(LEARNING_STANDARD_DOCUMENT_TITLE).getText());
-        doc.setCustomClass(Framework.class.getName());
-        doc.newObject(FRAMEWORK_CLASS_FULLNAME);
-        return doc;
-    }
-
     private ImportFilter getFilter(XWikiContext context){
-        return new Importer();
+        if (context.get(CONTEXT_KEY_IMPORT_FILTER) == null)
+            context.put(CONTEXT_KEY_IMPORT_FILTER, new DefaultImportFilterImpl());
+        return (ImportFilter) context.get(CONTEXT_KEY_IMPORT_FILTER);
     }
 
-    //LEARNING_STANDARD_DOCUMENT_ITEM
-    private Collection readFrameworkItems(String space, Element el, XWikiContext context) throws XWikiException {
-        List items = el.elements(LEARNING_STANDARD_DOCUMENT_ITEM);
-        Map itemsDoc = new HashMap();
-        Iterator it = items.iterator();
-        ImportFilter filter = getFilter(context);
-        while(it.hasNext()){
-            Element itemEl = (Element) it.next();
-            FrameworkItem item = readFrameworkItem(itemEl, filter, space, context);
-            itemsDoc.put(item.getIdentifier(context), item);
-        }
-
-        resetItemsParents(itemsDoc, context);
-
-        return itemsDoc.values();
-    }
-
-    private void resetItemsParents(Map items, XWikiContext context) {
-        Iterator it = items.values().iterator();
-        while(it.hasNext()){
-            FrameworkItem doc = (FrameworkItem) it.next();
-            String id = doc.getParentIdentifier(context);
-            if (id != null && id.length() > 0)
-            {
-                FrameworkItem parentItem = (FrameworkItem) items.get(id);
-                doc.setParent(parentItem.getFullName());
-            }
-        }
-    }
-
-    private FrameworkItem readFrameworkItem(Element itemEl, ImportFilter filter, String space, XWikiContext context) throws XWikiException {
-        FrameworkItem doc = new FrameworkItem(context.getWiki().getDocument(space, filter.getPageName(itemEl, context), context), context);
-        if (!doc.isNew())
-            throw new PluginException(getName(), ERROR_FRAMEWORK_ITEM_ALREADY_EXIST, "the framework item " + itemEl + " already exist, choose another name");
-
-        Element descEl = itemEl.element(LEARNING_STANDARD_DOCUMENT_ITEM_DESCRIPTION);
-        if (descEl != null && descEl.element(LEARNING_STANDARD_DOCUMENT_ITEM_TEXT) != null){
-
-            String title = descEl.element(LEARNING_STANDARD_DOCUMENT_ITEM_TEXT).getText();
-            if (title.length() > 250)
-                title = title.substring(0, 250);
-            doc.setTitle(title);
-        }
-
-        doc.setIdentifier(itemEl.element(LEARNING_STANDARD_DOCUMENT_ITEM_IDENTIFIER).getText());
-
-        if (itemEl.element(LEARNING_STANDARD_DOCUMENT_ITEM_PARENT) != null)
-            doc.setParentIdentifier(itemEl.element(LEARNING_STANDARD_DOCUMENT_ITEM_PARENT).getText());
-
-        doc.setParent(space + "." + FRAMEWORK_HOME);
-        doc.setCustomClass(FrameworkItem.class.getName());
-        return doc;
-    }
-
     public boolean setParent(FrameworkItem itemChild, FrameworkItem itemParent, XWikiContext context){
         return false;
     }
@@ -360,11 +313,6 @@
         return path;
     }
 
-    public class Importer implements ImportFilter{
 
-        public String getPageName(Element el, XWikiContext context) {
-            return el.element(LEARNING_STANDARD_DOCUMENT_ITEM_IDENTIFIER).getText();
-        }
-    }
 
 }

Modified: xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/ImportFilter.java
===================================================================
--- xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/ImportFilter.java	2006-10-03 13:53:24 UTC (rev 1355)
+++ xwiki-apps/gelc/gelcv1/trunk/gelcplugins/src/main/java/org/gelc/xwiki/plugins/framework/ImportFilter.java	2006-10-03 13:58:48 UTC (rev 1356)
@@ -1,8 +1,32 @@
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This 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.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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 this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author jeremi
+ */
 package org.gelc.xwiki.plugins.framework;
 
-import org.dom4j.Element;
 import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.XWikiException;
 
+import java.io.InputStream;
+import java.util.Collection;
+
 /**
  * Created by IntelliJ IDEA.
  * User: jeremi
@@ -11,5 +35,8 @@
  * To change this template use File | Settings | File Templates.
  */
 public interface ImportFilter {
-    String getPageName(Element el, XWikiContext context);
+    String      getPageName(Object item, XWikiContext context);
+    Framework   readFramework(String frameworkName, InputStream iStream, XWikiContext context) throws XWikiException;
+    Collection  readFrameworkItems(Framework framework, InputStream iStream, XWikiContext context) throws XWikiException;
+
 }





More information about the Xwiki-notifications mailing list