001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.kernel.bo.service.dependency;
017
018 import org.qedeq.base.io.Parameters;
019 import org.qedeq.base.trace.Trace;
020 import org.qedeq.kernel.bo.module.ControlVisitor;
021 import org.qedeq.kernel.bo.module.InternalServiceProcess;
022 import org.qedeq.kernel.bo.module.KernelModuleReferenceList;
023 import org.qedeq.kernel.bo.module.KernelQedeqBo;
024 import org.qedeq.kernel.bo.module.PluginExecutor;
025 import org.qedeq.kernel.se.base.module.Import;
026 import org.qedeq.kernel.se.base.module.ImportList;
027 import org.qedeq.kernel.se.common.ModuleContext;
028 import org.qedeq.kernel.se.common.ModuleDataException;
029 import org.qedeq.kernel.se.common.Plugin;
030 import org.qedeq.kernel.se.common.SourceFileExceptionList;
031 import org.qedeq.kernel.se.state.LoadingImportsState;
032
033
034 /**
035 * Load all directly imported QEDEQ modules.
036 *
037 * @author Michael Meyling
038 */
039 public final class LoadDirectlyRequiredModulesExecutor extends ControlVisitor
040 implements PluginExecutor {
041
042 /** This class. */
043 private static final Class CLASS = LoadDirectlyRequiredModulesExecutor.class;
044
045 /** List of required QEDEQ modules. */
046 private KernelModuleReferenceList required;
047
048 /**
049 * Constructor.
050 *
051 * @param plugin Plugin we work for.
052 * @param prop Internal QedeqBo.
053 * @param parameter Currently ignored.
054 */
055 public LoadDirectlyRequiredModulesExecutor(final Plugin plugin, final KernelQedeqBo prop,
056 final Parameters parameter) {
057 super(plugin, prop);
058 }
059
060 public Object executePlugin(final InternalServiceProcess process, final Object data) {
061 if (getQedeqBo().hasLoadedImports()) {
062 return getQedeqBo().getRequiredModules();
063 }
064 this.required = new KernelModuleReferenceList();
065 try {
066 super.traverse(process);
067 getQedeqBo().setLoadedImports(required);
068 } catch (final SourceFileExceptionList sfl) {
069 getQedeqBo().setLoadingImportsFailureState(
070 LoadingImportsState.STATE_LOADING_IMPORTS_FAILED, sfl);
071 }
072 return required;
073 }
074
075 /**
076 * Get list of directly referenced modules.
077 *
078 * @return List of directly required modules.
079 */
080 public KernelModuleReferenceList getRequired() {
081 return required;
082 }
083
084 /**
085 * Visit import. Loads referenced QEDEQ module and saves reference.
086 *
087 * @param imp Begin visit of this element.
088 * @throws ModuleDataException Major problem occurred.
089 */
090 public void visitEnter(final Import imp) throws ModuleDataException {
091 final ModuleContext context = getCurrentContext();
092 context.setLocationWithinModule(context.getLocationWithinModule() + ".getLabel()");
093 try {
094 final KernelQedeqBo propNew = getQedeqBo().getKernelServices().loadModule(
095 getQedeqBo().getModuleAddress(), imp.getSpecification());
096 getRequired().addLabelUnique(context, imp.getLabel(), propNew);
097 Trace.param(CLASS, "visitEnter(Import)", "adding context", getCurrentContext());
098 } catch (SourceFileExceptionList e) {
099 final ModuleDataException me = new LoadRequiredModuleException(e.get(0).getErrorCode(),
100 "import of module with label \"" + imp.getLabel() + "\" failed: "
101 + e.get(0).getMessage(), context);
102 // TODO mime 20080227: also include reference area in sf creation?
103 addError(me);
104 Trace.trace(CLASS, this, "visitEnter(Import)", e);
105 }
106 }
107
108 /**
109 * End of visit of import list. Blocks further visits.
110 *
111 * @param imports This visit has just ended.
112 */
113 public void visitLeave(final ImportList imports) {
114 setBlocked(true); // block further traverse
115 }
116
117 }
|