Search in sources :

Example 1 with EmbeddableComponentManager

use of org.xwiki.component.embed.EmbeddableComponentManager in project OsmAnd-tools by osmandapp.

the class WikiDatabasePreparation method mainTest.

public static void mainTest(String[] args) throws ConversionException, ComponentLookupException, ParseException, IOException {
    EmbeddableComponentManager cm = new EmbeddableComponentManager();
    cm.initialize(WikiDatabasePreparation.class.getClassLoader());
    Parser parser = cm.getInstance(Parser.class, Syntax.MEDIAWIKI_1_0.toIdString());
    FileReader fr = new FileReader(new File("/Users/victorshcherb/Documents/b.src.html"));
    BufferedReader br = new BufferedReader(fr);
    String content = "";
    String s;
    while ((s = br.readLine()) != null) {
        content += s;
    }
    content = removeMacroBlocks(content, new HashMap<>());
    XDOM xdom = parser.parse(new StringReader(content));
    // Find all links and make them italic
    for (Block block : xdom.getBlocks(new ClassBlockMatcher(LinkBlock.class), Block.Axes.DESCENDANT)) {
        Block parentBlock = block.getParent();
        Block newBlock = new FormatBlock(Collections.<Block>singletonList(block), Format.ITALIC);
        parentBlock.replaceChild(newBlock, block);
    }
    // for (Block block : xdom.getBlocks(new ClassBlockMatcher(ParagraphBlock.class), Block.Axes.DESCENDANT)) {
    // ParagraphBlock b = (ParagraphBlock) block;
    // block.getParent().removeBlock(block);
    // }
    WikiPrinter printer = new DefaultWikiPrinter();
    // BlockRenderer renderer = cm.getInstance(BlockRenderer.class, Syntax.XHTML_1_0.toIdString());
    // renderer.render(xdom, printer);
    // System.out.println(printer.toString());
    Converter converter = cm.getInstance(Converter.class);
    // Convert input in XWiki Syntax 2.1 into XHTML. The result is stored in the printer.
    printer = new DefaultWikiPrinter();
    converter.convert(new FileReader(new File("/Users/victorshcherb/Documents/a.src.html")), Syntax.MEDIAWIKI_1_0, Syntax.XHTML_1_0, printer);
    System.out.println(printer.toString());
    final HTMLConverter nconverter = new HTMLConverter(false);
    String lang = "be";
    WikiModel wikiModel = new WikiModel("http://" + lang + ".wikipedia.com/wiki/${image}", "http://" + lang + ".wikipedia.com/wiki/${title}");
// String plainStr = wikiModel.render(nconverter, content);
// System.out.println(plainStr);
// downloadPage("https://be.m.wikipedia.org/wiki/%D0%93%D0%BE%D1%80%D0%B0%D0%B4_%D0%9C%D1%96%D0%BD%D1%81%D0%BA",
// "/Users/victorshcherb/Documents/a.wiki.html");
}
Also used : HTMLConverter(info.bliki.wiki.filter.HTMLConverter) XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FormatBlock(org.xwiki.rendering.block.FormatBlock) WikiModel(info.bliki.wiki.model.WikiModel) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) SAXParser(javax.xml.parsers.SAXParser) Parser(org.xwiki.rendering.parser.Parser) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) LinkBlock(org.xwiki.rendering.block.LinkBlock) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) FormatBlock(org.xwiki.rendering.block.FormatBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) HTMLConverter(info.bliki.wiki.filter.HTMLConverter) Converter(org.xwiki.rendering.converter.Converter) EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) FileReader(java.io.FileReader) File(java.io.File)

Example 2 with EmbeddableComponentManager

use of org.xwiki.component.embed.EmbeddableComponentManager in project xwiki-platform by xwiki.

the class InstallMojo method getModelFactory.

private ModelFactory getModelFactory() throws Exception {
    // Initialize XWiki Component system
    EmbeddableComponentManager ecm = new EmbeddableComponentManager();
    ecm.initialize(this.getClass().getClassLoader());
    return ecm.getInstance(ModelFactory.class);
}
Also used : EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager)

Example 3 with EmbeddableComponentManager

use of org.xwiki.component.embed.EmbeddableComponentManager in project xwiki-platform by xwiki.

the class AbstractHttpTest method initializeSystem.

public static void initializeSystem() throws Exception {
    ComponentManager componentManager = new EmbeddableComponentManager();
    // Only load the minimal number of components required for the test framework, for both performance reasons
    // and for avoiding having to declare dependencies such as HttpServletRequest.
    ComponentAnnotationLoader loader = new ComponentAnnotationLoader();
    List<ComponentDeclaration> componentDeclarations = new ArrayList<>();
    componentDeclarations.add(new ComponentDeclaration(DefaultStringEntityReferenceResolver.class.getName()));
    componentDeclarations.add(new ComponentDeclaration(DefaultStringEntityReferenceSerializer.class.getName()));
    componentDeclarations.add(new ComponentDeclaration(RelativeStringEntityReferenceResolver.class.getName()));
    componentDeclarations.add(new ComponentDeclaration(DefaultEntityReferenceProvider.class.getName()));
    componentDeclarations.add(new ComponentDeclaration(DefaultModelConfiguration.class.getName()));
    componentDeclarations.add(new ComponentDeclaration(DefaultSymbolScheme.class.getName()));
    loader.initialize(componentManager, AbstractTest.class.getClassLoader(), componentDeclarations);
    TestUtils.initializeComponent(componentManager);
}
Also used : AbstractTest(org.xwiki.test.ui.AbstractTest) ComponentDeclaration(org.xwiki.component.annotation.ComponentDeclaration) EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ComponentManager(org.xwiki.component.manager.ComponentManager) ArrayList(java.util.ArrayList) EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ComponentAnnotationLoader(org.xwiki.component.annotation.ComponentAnnotationLoader)

Example 4 with EmbeddableComponentManager

use of org.xwiki.component.embed.EmbeddableComponentManager in project xwiki-platform by xwiki.

the class AbstractTest method initializeSystem.

/**
 * Used so that AllTests can set the persistent test context.
 */
public static void initializeSystem(PersistentTestContext context) throws Exception {
    AbstractTest.context = context;
    BaseElement.setContext(context);
    TestUtils.setContext(context);
    AbstractTest.componentManager = new EmbeddableComponentManager();
    // Only load the minimal number of components required for the test framework, for both performance reasons
    // and for avoiding having to declare dependencies such as HttpServletRequest.
    ComponentAnnotationLoader loader = new ComponentAnnotationLoader();
    List<ComponentDeclaration> componentDeclarations = new ArrayList<>();
    componentDeclarations.add(new ComponentDeclaration(DefaultStringEntityReferenceResolver.class.getName()));
    componentDeclarations.add(new ComponentDeclaration(DefaultStringEntityReferenceSerializer.class.getName()));
    componentDeclarations.add(new ComponentDeclaration(RelativeStringEntityReferenceResolver.class.getName()));
    componentDeclarations.add(new ComponentDeclaration(DefaultEntityReferenceProvider.class.getName()));
    componentDeclarations.add(new ComponentDeclaration(DefaultModelConfiguration.class.getName()));
    componentDeclarations.add(new ComponentDeclaration(DefaultSymbolScheme.class.getName()));
    loader.initialize(AbstractTest.componentManager, AbstractTest.class.getClassLoader(), componentDeclarations);
    TestUtils.initializeComponent(AbstractTest.componentManager);
}
Also used : ComponentDeclaration(org.xwiki.component.annotation.ComponentDeclaration) ArrayList(java.util.ArrayList) EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ComponentAnnotationLoader(org.xwiki.component.annotation.ComponentAnnotationLoader)

Example 5 with EmbeddableComponentManager

use of org.xwiki.component.embed.EmbeddableComponentManager in project xwiki-platform by xwiki.

the class XWikiServletContextListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
    // Initializes the Embeddable Component Manager
    EmbeddableComponentManager ecm = new EmbeddableComponentManager();
    // Initialize all the components. Note that this can fail with a Runtime Exception. This is done voluntarily so
    // that the XWiki webapp will not be available if one component fails to load. It's better to fail-fast.
    ecm.initialize(this.getClass().getClassLoader());
    this.componentManager = ecm;
    // This is a temporary bridge to allow non XWiki components to lookup XWiki components.
    // We're putting the XWiki Component Manager instance in the Servlet Context so that it's
    // available in the XWikiAction class which in turn puts it into the XWikiContext instance.
    // Class that need to lookup then just need to get it from the XWikiContext instance.
    // This is of course not necessary for XWiki components since they just need to implement
    // the Composable interface to get access to the Component Manager or better they simply
    // need to declare their components requirements using the @Inject annotation of the xwiki
    // component manager together with a private class member, for automatic injection by the CM on init.
    servletContextEvent.getServletContext().setAttribute(org.xwiki.component.manager.ComponentManager.class.getName(), this.componentManager);
    // Use a Component Event Manager that stacks Component instance creation events till we tell it to flush them.
    // The reason is that the Observation Manager used to send the events but we need the Application Context to
    // be set up before we start sending events since there can be Observation Listener components that require
    // the Application Context (this is the case for example for the Office Importer Lifecycle Listener).
    StackingComponentEventManager eventManager = new StackingComponentEventManager();
    this.componentManager.setComponentEventManager(eventManager);
    // Initialize the Environment
    try {
        ServletEnvironment servletEnvironment = this.componentManager.getInstance(Environment.class);
        servletEnvironment.setServletContext(servletContextEvent.getServletContext());
    } catch (ComponentLookupException e) {
        throw new RuntimeException("Failed to initialize the Servlet Environment", e);
    }
    // below in an Event Listener and move it to the legacy module.
    try {
        ServletContainerInitializer containerInitializer = this.componentManager.getInstance(ServletContainerInitializer.class);
        containerInitializer.initializeApplicationContext(servletContextEvent.getServletContext());
    } catch (ComponentLookupException e) {
        throw new RuntimeException("Failed to initialize the Application Context", e);
    }
    // Send an Observation event to signal the XWiki application is started. This allows components who need to do
    // something on startup to do it.
    ObservationManager observationManager;
    try {
        observationManager = this.componentManager.getInstance(ObservationManager.class);
    } catch (ComponentLookupException e) {
        throw new RuntimeException("Failed to find the Observation Manager component", e);
    }
    // Now that the Application Context is set up, send the Component instance creation events we had stacked up.
    eventManager.setObservationManager(observationManager);
    eventManager.shouldStack(false);
    eventManager.flushEvents();
    // Make sure installed extensions are initialized before sending ApplicationStartedEvent
    try {
        this.componentManager.getInstance(ExtensionInitializer.class);
    } catch (ComponentLookupException e) {
        throw new RuntimeException("Failed to initialize installed extensions", e);
    }
    // Indicate to the various components that XWiki is ready
    observationManager.notify(new ApplicationStartedEvent(), this);
}
Also used : EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ApplicationStartedEvent(org.xwiki.observation.event.ApplicationStartedEvent) EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ObservationManager(org.xwiki.observation.ObservationManager) StackingComponentEventManager(org.xwiki.component.internal.StackingComponentEventManager) ServletEnvironment(org.xwiki.environment.internal.ServletEnvironment)

Aggregations

EmbeddableComponentManager (org.xwiki.component.embed.EmbeddableComponentManager)7 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)3 ArrayList (java.util.ArrayList)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 ComponentAnnotationLoader (org.xwiki.component.annotation.ComponentAnnotationLoader)2 ComponentDeclaration (org.xwiki.component.annotation.ComponentDeclaration)2 ComponentManager (org.xwiki.component.manager.ComponentManager)2 ExecutionContext (org.xwiki.context.ExecutionContext)2 ExecutionContextException (org.xwiki.context.ExecutionContextException)2 ExecutionContextManager (org.xwiki.context.ExecutionContextManager)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 HTMLConverter (info.bliki.wiki.filter.HTMLConverter)1 WikiModel (info.bliki.wiki.model.WikiModel)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 StringReader (java.io.StringReader)1 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1