-
-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XWIKI-21699: Add new API to help evaluate xobjects
XWIKI-21473: Improve search suggest evaluation
- Loading branch information
Showing
16 changed files
with
726 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...tform-core/xwiki-platform-oldcore/src/main/java/org/xwiki/evaluation/ObjectEvaluator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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.evaluation; | ||
|
||
import java.util.Map; | ||
|
||
import org.xwiki.component.annotation.Role; | ||
import org.xwiki.evaluation.internal.DefaultObjectEvaluator; | ||
import org.xwiki.stability.Unstable; | ||
|
||
import com.xpn.xwiki.objects.BaseObject; | ||
|
||
/** | ||
* Evaluates the properties of an object and returns a Map that stores the evaluation results, in which keys are the | ||
* field names of the properties, and values their evaluated content. | ||
* Implement an instance with a hint corresponding to the class name of the object you want to evaluate and use | ||
* {@link DefaultObjectEvaluator} that will proxy the calls to the right implementation. | ||
* This ensures that the properties being evaluated are only the ones referenced explicitly by the provided | ||
* implementation, in order to avoid accidental evaluation of properties that should only be used in specific contexts. | ||
* | ||
* @version $Id$ | ||
* @since 14.10.21 | ||
* @since 15.5.5 | ||
* @since 15.10.2 | ||
*/ | ||
@Unstable | ||
@Role | ||
public interface ObjectEvaluator | ||
{ | ||
/** | ||
* Evaluates the properties of an object. | ||
* | ||
* @param object the object to evaluate | ||
* @return a Map storing the evaluated properties | ||
* @throws ObjectEvaluatorException if the evaluation fails | ||
*/ | ||
Map<String, String> evaluate(BaseObject object) throws ObjectEvaluatorException; | ||
} |
55 changes: 55 additions & 0 deletions
55
...e/xwiki-platform-oldcore/src/main/java/org/xwiki/evaluation/ObjectEvaluatorException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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.evaluation; | ||
|
||
import org.xwiki.stability.Unstable; | ||
|
||
/** | ||
* Exception raised during evaluation of XObjects properties. | ||
* | ||
* @version $Id$ | ||
* @since 14.10.21 | ||
* @since 15.5.5 | ||
* @since 15.10.2 | ||
*/ | ||
@Unstable | ||
public class ObjectEvaluatorException extends Exception | ||
{ | ||
/** | ||
* Creates an instance of ObjectEvaluatorException with a message and a cause. | ||
* | ||
* @param message the message detailing the issue | ||
* @param cause the cause of the exception | ||
*/ | ||
public ObjectEvaluatorException(String message, Throwable cause) | ||
{ | ||
super(message, cause); | ||
} | ||
|
||
/** | ||
* Creates an instance of ObjectEvaluatorException with a message. | ||
* | ||
* @param message the message detailing the issue | ||
*/ | ||
public ObjectEvaluatorException(String message) | ||
{ | ||
this(message, null); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...re/xwiki-platform-oldcore/src/main/java/org/xwiki/evaluation/ObjectPropertyEvaluator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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.evaluation; | ||
|
||
import java.util.Map; | ||
|
||
import org.xwiki.component.annotation.Role; | ||
import org.xwiki.stability.Unstable; | ||
|
||
import com.xpn.xwiki.objects.BaseObject; | ||
|
||
/** | ||
* Evaluates the properties of an object and returns a Map that stores the evaluation results, in which keys are the | ||
* field names of the properties, and values their evaluated content. | ||
* Instances of this interface should be used as helpers by implementations of {@link ObjectEvaluator}. | ||
* | ||
* @version $Id$ | ||
* @since 14.10.21 | ||
* @since 15.5.5 | ||
* @since 15.10.2 | ||
*/ | ||
@Unstable | ||
@Role | ||
public interface ObjectPropertyEvaluator | ||
{ | ||
/** | ||
* Evaluates the properties of an object. | ||
* | ||
* @param object the object to evaluate | ||
* @param properties the names of the properties to evaluate | ||
* @return a Map storing the evaluated properties | ||
* @throws ObjectEvaluatorException if the evaluation fails | ||
*/ | ||
Map<String, String> evaluateProperties(BaseObject object, String... properties) throws ObjectEvaluatorException; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} |
82 changes: 82 additions & 0 deletions
82
...-platform-oldcore/src/main/java/org/xwiki/evaluation/internal/DefaultObjectEvaluator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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.evaluation.internal; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.inject.Provider; | ||
import javax.inject.Singleton; | ||
|
||
import org.xwiki.component.annotation.Component; | ||
import org.xwiki.component.manager.ComponentLookupException; | ||
import org.xwiki.component.manager.ComponentManager; | ||
import org.xwiki.evaluation.ObjectEvaluator; | ||
import org.xwiki.evaluation.ObjectEvaluatorException; | ||
import org.xwiki.model.reference.EntityReferenceSerializer; | ||
|
||
import com.xpn.xwiki.objects.BaseObject; | ||
|
||
/** | ||
* Evaluator that proxies the actual evaluation to the right ObjectEvaluator implementation, based on the XClass of | ||
* the object. | ||
* | ||
* @version $Id$ | ||
* @since 14.10.21 | ||
* @since 15.5.5 | ||
* @since 15.10.2 | ||
*/ | ||
@Component | ||
@Singleton | ||
public class DefaultObjectEvaluator implements ObjectEvaluator | ||
{ | ||
@Inject | ||
@Named("context") | ||
private Provider<ComponentManager> contextComponentManagerProvider; | ||
|
||
@Inject | ||
@Named("local") | ||
private EntityReferenceSerializer<String> entityReferenceSerializer; | ||
|
||
@Override | ||
public Map<String, String> evaluate(BaseObject object) throws ObjectEvaluatorException | ||
{ | ||
if (object == null) { | ||
return Collections.emptyMap(); | ||
} | ||
|
||
String xClassName = this.entityReferenceSerializer.serialize(object.getXClassReference()); | ||
ComponentManager componentManager = this.contextComponentManagerProvider.get(); | ||
if (!componentManager.hasComponent(ObjectEvaluator.class, xClassName)) { | ||
throw new ObjectEvaluatorException(String.format("Could not find an instance of 'ObjectEvaluator' for " | ||
This comment has been minimized.
Sorry, something went wrong. |
||
+ "XObject of class '%s'.", xClassName)); | ||
} | ||
|
||
try { | ||
ObjectEvaluator objectEvaluator = componentManager.getInstance(ObjectEvaluator.class, xClassName); | ||
return objectEvaluator.evaluate(object); | ||
} catch (ComponentLookupException e) { | ||
throw new ObjectEvaluatorException(String.format("Could not instantiate 'ObjectEvaluator' for XObject of " | ||
+ "class '%s'.", xClassName), e); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
} | ||
} |
Oops, something went wrong.
why not simply
evaluate()
(since it's a property evamuator)?