io.legaldocml.akn.AkomaNtosoContext Maven / Gradle / Ivy
The newest version!
package io.legaldocml.akn;
import com.google.common.collect.ImmutableMap;
import io.legaldocml.akn.attribute.Id;
import io.legaldocml.akn.attribute.Src;
import io.legaldocml.akn.type.ManifestationURI;
import io.legaldocml.akn.type.NoWhiteSpace;
import io.legaldocml.io.XmlReaderContext;
import io.legaldocml.io.XmlWriter;
import io.legaldocml.model.ModelProvider;
import io.legaldocml.module.AknModule;
import io.legaldocml.module.Module;
import io.legaldocml.unsafe.UnsafeString;
import io.legaldocml.util.CharArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.function.BiConsumer;
/**
* @author Jacques Militello
*/
public abstract class AkomaNtosoContext implements XmlReaderContext {
/**
* SLF4J Logger.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(AkomaNtosoContext.class);
private static final ImmutableMap> REFS;
static {
REFS = ImmutableMap.>builder()
.put(AknAttributes.EID, (context, aknObject) -> context.eids.put(((Id)aknObject).getEid(), (Id)aknObject))
.put(AknAttributes.SRC, (context, aknObject) -> context.srcs.put(((Src)aknObject).getSrc(), (Src)aknObject))
.build();
}
private final HashMap eids = new HashMap<>();
private final HashMap srcs = new HashMap<>();
/**
* Holder of all namespaces define for this "akomantoso".
*/
private final Map modules = new HashMap<>(4);
/**
* Akn module for this context
*/
private final AknModule aknModule;
protected AkomaNtosoContext(AknModule aknModule) {
this.aknModule = aknModule;
add(aknModule);
}
public AknModule getAkoXmlModule() {
return this.aknModule;
}
public Module getModule(CharArray array) {
return this.modules.get(array);
}
void writeModules(XmlWriter writer) throws IOException {
for (Module module : this.modules.values()) {
module.writeNamespace(writer);
}
}
public void add(Module module) {
this.modules.put(module.namespace(), module);
}
@SuppressWarnings("unchecked")
public void update(String name, AknObject akn) {
BiConsumer consumer = REFS.get(name);
if (consumer != null) {
consumer.accept(this, akn);
}
}
public Id getId(String id) {
return this.eids.get(NoWhiteSpace.valueOf(UnsafeString.getChars(id)));
}
public Id getId(NoWhiteSpace id) {
return this.eids.get(id);
}
public Iterator getIds() {
return this.eids.values().iterator();
}
public abstract ModelProvider getModelProvider();
}