On Sun, Dec 20, 2009 at 16:44, asiri <platform-notifications(a)xwiki.org> wrote:
Author: asiri
Date: 2009-12-20 16:44:32 +0100 (Sun, 20 Dec 2009)
New Revision: 25854
Added:
platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/officeimporter/OfficeImporterVelocityBridge.java
Removed:
platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/officeimporter/internal/OfficeImporterVelocityBridge.java
Modified:
platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/officeimporter/internal/OfficeImporterVelocityContextInitializer.java
Log:
[misc] improving officeimporter code - step 1
* Moved OfficeImporterVelocityBridge class out from org.xwiki.officeimporter.internal
package into org.xwiki.officeimporter package since a velocity bridge represents a public
API.
Added:
platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/officeimporter/OfficeImporterVelocityBridge.java
===================================================================
---
platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/officeimporter/OfficeImporterVelocityBridge.java
(rev 0)
+++
platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/officeimporter/OfficeImporterVelocityBridge.java
2009-12-20 15:44:32 UTC (rev 25854)
@@ -0,0 +1,172 @@
+/*
+ * See the NOTICE file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * 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.
+ */
+package org.xwiki.officeimporter;
+
+import java.io.ByteArrayInputStream;
+import java.util.Map;
+
+import org.xwiki.bridge.DocumentAccessBridge;
+import org.xwiki.component.logging.Logger;
+import org.xwiki.component.manager.ComponentManager;
+import org.xwiki.context.Execution;
+import org.xwiki.model.DocumentNameFactory;
+
+/**
+ * A bridge between velocity and office importer.
+ *
+ * @version $Id: OfficeImporterVelocityBridge.java 24508 2009-10-15 10:05:22Z asiri $
+ * @since 1.8M1
+ */
+public class OfficeImporterVelocityBridge
+{
+ /**
+ * The key used to place any error messages while importing office documents.
+ */
+ public static final String OFFICE_IMPORTER_ERROR =
"OFFICE_IMPORTER_ERROR";
+
+ /**
+ * The {@link Execution} component.
+ */
+ private Execution execution;
+
+ /**
+ * Internal {@link OfficeImporter} component.
+ */
+ private OfficeImporter importer;
+
+ /**
+ * The {@link DocumentAccessBridge} component.
+ */
+ private DocumentAccessBridge docBridge;
+
+ /**
+ * Used for converting string document names to {@link DocumentName} instances.
+ */
+ private DocumentNameFactory nameFactory;
+
+ /**
+ * The {@link Logger} instance.
+ */
+ private Logger logger;
+
+ /**
+ * Default constructor.
+ *
+ * @param componentManager used to lookup for other necessary components.
+ * @param logger logger.
+ */
+ public OfficeImporterVelocityBridge(ComponentManager componentManager, Logger
logger)
+ {
+ this.logger = logger;
+ try {
+ this.execution = componentManager.lookup(Execution.class);
+ this.importer = componentManager.lookup(OfficeImporter.class);
+ this.docBridge = componentManager.lookup(DocumentAccessBridge.class);
+ this.nameFactory = componentManager.lookup(DocumentNameFactory.class);
+ } catch (Exception ex) {
+ logger.error("Error while initializing office importer velocity
bridge.", ex);
Shouldn't it be an exception instead of a log ? Since this class
depends on this component you should not be able to created it if one
of these is missing like when you create a component.
+ }
+ }
+
+ /**
+ * Imports the passed Office document into the target wiki page.
+ *
+ * @param fileContent the binary content of the input document.
+ * @param fileName the name of the source document (should have a valid extension
since the extension is used to
+ * find out the office document's format).
+ * @param targetDocument the name of the resulting wiki page.
+ * @param options the optional parameters for the conversion.
+ * @return true if the operation was a success.
+ */
+ public boolean importDocument(byte[] fileContent, String fileName, String
targetDocument,
+ Map<String, String> options)
+ {
+ boolean success = false;
+ try {
+ validateRequest(targetDocument, options);
+ importer.importStream(new ByteArrayInputStream(fileContent), fileName,
targetDocument, options);
+ success = true;
+ } catch (OfficeImporterException ex) {
+ logger.error(ex.getMessage(), ex);
+ execution.getContext().setProperty(OFFICE_IMPORTER_ERROR, ex.getMessage());
+ } catch (Exception ex) {
+ logger.error(ex.getMessage(), ex);
+ setErrorMessage("Internal error while finalizing the target
document.");
+ }
+ return success;
+ }
+
+ /**
+ * @return an error message set inside current execution (during import process) or
null.
+ */
+ public String getErrorMessage()
+ {
+ return (String) execution.getContext().getProperty(OFFICE_IMPORTER_ERROR);
+ }
+
+ /**
+ * Utility method for setting an error message inside current execution.
+ *
+ * @param message error message.
+ */
+ private void setErrorMessage(String message)
+ {
+ execution.getContext().setProperty(OFFICE_IMPORTER_ERROR, message);
+ }
+
+ /**
+ * @return any error messages thrown while importing.
+ * @deprecated use {@link #getErrorMessage()} instead since 2.2M1.
+ */
+ @Deprecated
+ public String getLastErrorMessage()
+ {
+ return getErrorMessage();
+ }
+
+ /**
+ * Checks if this request is valid. For a request to be valid, the target document
should be editable by the current
+ * user. And if this is not an append request, the target document should not
exist.
+ *
+ * @param targetDocument the target document.
+ * @param options additional parameters passed in for the import operation.
+ * @throws OfficeImporterException if the request is invalid.
+ */
+ private void validateRequest(String targetDocument, Map<String, String>
options) throws OfficeImporterException
+ {
+ if
(!docBridge.isDocumentEditable(nameFactory.createDocumentName(targetDocument))) {
+ throw new OfficeImporterException("Inadequate privileges.");
+ } else if (docBridge.exists(targetDocument) &&
!isAppendRequest(options)) {
+ throw new OfficeImporterException("The target document " +
targetDocument + " already exists.");
+ }
+ }
+
+ /**
+ * Utility method for checking if a request is made to append the importer result to
an existing page.
+ *
+ * @param options additional parameters passed in for the import operation.
+ * @return true if the params indicate that this is an append request.
+ */
+ private boolean isAppendRequest(Map<String, String> options)
+ {
+ String appendParam = options.get("appendContent");
+ return (appendParam != null) ? appendParam.equals("true") : false;
+ }
+}
Deleted:
platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/officeimporter/internal/OfficeImporterVelocityBridge.java
===================================================================
---
platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/officeimporter/internal/OfficeImporterVelocityBridge.java
2009-12-20 02:37:05 UTC (rev 25853)
+++
platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/officeimporter/internal/OfficeImporterVelocityBridge.java
2009-12-20 15:44:32 UTC (rev 25854)
@@ -1,147 +0,0 @@
-/*
- * See the NOTICE file distributed with this work for additional
- * information regarding copyright ownership.
- *
- * 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.
- */
-package org.xwiki.officeimporter.internal;
-
-import java.io.ByteArrayInputStream;
-import java.util.Map;
-
-import org.xwiki.bridge.DocumentAccessBridge;
-import org.xwiki.component.logging.Logger;
-import org.xwiki.context.Execution;
-import org.xwiki.officeimporter.OfficeImporter;
-import org.xwiki.officeimporter.OfficeImporterException;
-
-/**
- * A bridge between velocity and office importer.
- *
- * @version $Id$
- * @since 1.8M1
- */
-public class OfficeImporterVelocityBridge
-{
- /**
- * The key used to place any error messages while importing office documents.
- */
- public static final String OFFICE_IMPORTER_ERROR =
"OFFICE_IMPORTER_ERROR";
-
- /**
- * The {@link Execution} component.
- */
- private Execution execution;
-
- /**
- * Internal {@link OfficeImporter} component.
- */
- private OfficeImporter importer;
-
- /**
- * The {@link DocumentAccessBridge} component.
- */
- private DocumentAccessBridge docBridge;
-
- /**
- * The {@link Logger} instance.
- */
- private Logger logger;
-
- /**
- * Default constructor.
- *
- * @param execution current execution.
- * @param importer internal office importer component.
- * @param docBridge document access bridge.
- * @param logger logger.
- */
- public OfficeImporterVelocityBridge(Execution execution, OfficeImporter importer,
DocumentAccessBridge docBridge,
- Logger logger)
- {
- this.execution = execution;
- this.importer = importer;
- this.docBridge = docBridge;
- this.logger = logger;
- }
-
- /**
- * Imports the passed Office document into the target wiki page.
- *
- * @param fileContent the binary content of the input document.
- * @param fileName the name of the source document (should have a valid extension
since the extension is used to
- * find out the office document's format).
- * @param targetDocument the name of the resulting wiki page.
- * @param options the optional parameters for the conversion.
- * @return true if the operation was a success.
- */
- public boolean importDocument(byte[] fileContent, String fileName, String
targetDocument,
- Map<String, String> options)
- {
- boolean success = false;
- try {
- validateRequest(targetDocument, options);
- importer.importStream(new ByteArrayInputStream(fileContent), fileName,
targetDocument, options);
- success = true;
- } catch (OfficeImporterException ex) {
- logger.error(ex.getMessage(), ex);
- execution.getContext().setProperty(OFFICE_IMPORTER_ERROR, ex.getMessage());
- } catch (Exception ex) {
- logger.error(ex.getMessage(), ex);
- execution.getContext().setProperty(OFFICE_IMPORTER_ERROR,
- "Internal error while finalizing the target document.");
- }
- return success;
- }
-
- /**
- * Checks if this request is valid. For a request to be valid, the target document
should be editable by the current
- * user. And if this is not an append request, the target document should not
exist.
- *
- * @param targetDocument the target document.
- * @param options additional parameters passed in for the import operation.
- * @throws OfficeImporterException if the request is invalid.
- */
- private void validateRequest(String targetDocument, Map<String, String>
options) throws OfficeImporterException
- {
- if (!docBridge.isDocumentEditable(targetDocument)) {
- throw new OfficeImporterException("Inadequate privileges.");
- } else if (docBridge.exists(targetDocument) &&
!isAppendRequest(options)) {
- throw new OfficeImporterException("The target document " +
targetDocument + " already exists.");
- }
- }
-
- /**
- * Utility method for checking if a request is made to append the importer result to
an existing page.
- *
- * @param options additional parameters passed in for the import operation.
- * @return true if the params indicate that this is an append request.
- */
- private boolean isAppendRequest(Map<String, String> options)
- {
- String appendParam = options.get("appendContent");
- return (appendParam != null) ? appendParam.equals("true") : false;
- }
-
- /**
- * @return any error messages thrown while importing.
- */
- public String getLastErrorMessage()
- {
- Object error = execution.getContext().getProperty(OFFICE_IMPORTER_ERROR);
- return (error != null) ? (String) error : null;
- }
-}
Modified:
platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/officeimporter/internal/OfficeImporterVelocityContextInitializer.java
===================================================================
---
platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/officeimporter/internal/OfficeImporterVelocityContextInitializer.java
2009-12-20 02:37:05 UTC (rev 25853)
+++
platform/core/trunk/xwiki-officeimporter/src/main/java/org/xwiki/officeimporter/internal/OfficeImporterVelocityContextInitializer.java
2009-12-20 15:44:32 UTC (rev 25854)
@@ -20,12 +20,11 @@
package org.xwiki.officeimporter.internal;
import org.apache.velocity.VelocityContext;
-import org.xwiki.bridge.DocumentAccessBridge;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.Requirement;
import org.xwiki.component.logging.AbstractLogEnabled;
-import org.xwiki.context.Execution;
-import org.xwiki.officeimporter.OfficeImporter;
+import org.xwiki.component.manager.ComponentManager;
+import org.xwiki.officeimporter.OfficeImporterVelocityBridge;
import org.xwiki.velocity.VelocityContextInitializer;
/**
@@ -43,29 +42,16 @@
public static final String VELOCITY_CONTEXT_KEY = "officeimporter";
/**
- * The {@link Execution} component.
+ * Used to lookup other components.
*/
@Requirement
- private Execution execution;
+ private ComponentManager componentManager;
/**
- * The {@link OfficeImporter} component.
- */
- @Requirement
- private OfficeImporter officeImporter;
-
- /**
- * The {@link DocumentAccessBridge}.
- */
- @Requirement
- private DocumentAccessBridge docBridge;
-
- /**
* {@inheritDoc}
*/
public void initialize(VelocityContext context)
{
- context.put(VELOCITY_CONTEXT_KEY, new OfficeImporterVelocityBridge(execution,
officeImporter, docBridge,
- getLogger()));
+ context.put(VELOCITY_CONTEXT_KEY, new
OfficeImporterVelocityBridge(componentManager, getLogger()));
}
}
_______________________________________________
notifications mailing list
notifications(a)xwiki.org
http://lists.xwiki.org/mailman/listinfo/notifications