Search in sources :

Example 41 with IOMessageImpl

use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.

the class INSPIRECodeListReader method addEntry.

private void addEntry(Node item, INSPIRECodeList codelist, XPath xpath, IOReporter reporter) throws Exception {
    String name = null;
    String description = null;
    String identifier = null;
    String namespace = null;
    identifier = (String) xpath.evaluate("@id", item, XPathConstants.STRING);
    if (identifier == null) {
        reporter.warn(new IOMessageImpl("No id attribute present in a value of the INSPIRE codelist. Skipping value.", null));
        return;
    }
    // XXX what about multiple labels or definitions?
    NodeList labels = (NodeList) xpath.evaluate("label", item, XPathConstants.NODESET);
    if (labels.getLength() > 0)
        name = labels.item(0).getTextContent();
    else {
        reporter.warn(new IOMessageImpl("No label present in a value of the INSPIRE codelist.", null));
        name = identifier;
    }
    NodeList definitions = (NodeList) xpath.evaluate("definition", item, XPathConstants.NODESET);
    if (definitions.getLength() > 0)
        description = definitions.item(0).getTextContent();
    // in schema no description, but in data; anyways, ignore it for now
    // also ignore status, register, applicationschema and theme
    namespace = (String) xpath.evaluate("codelist/@id", item, XPathConstants.STRING);
    // XXX I guess namespace has to be the same as the codelist. Check this?
    codelist.addEntry(new CodeEntry(name, description, identifier, namespace));
}
Also used : CodeEntry(eu.esdihumboldt.hale.common.codelist.CodeList.CodeEntry) NodeList(org.w3c.dom.NodeList) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)

Example 42 with IOMessageImpl

use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.

the class ArchiveProjectReader method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    // copy resources to a temporary directory
    tempDir = Files.createTempDir();
    IOUtils.extract(tempDir, getSource().getInput());
    // create the project file via XMLProjectReader
    File baseFile = new File(tempDir, "project.halex");
    ProjectReader reader;
    if (!baseFile.exists()) {
        // look for project file
        log.debug("Default project file not found, looking for other candidates.");
        String candidate = ProjectIO.findProjectFile(tempDir);
        if (candidate != null) {
            log.info(MessageFormat.format("Loading {0} as project file from archive", candidate));
            baseFile = new File(tempDir, candidate);
        }
        reader = HaleIO.findIOProvider(ProjectReader.class, new FileIOSupplier(baseFile), candidate);
        if (reader == null) {
            reporter.error(new IOMessageImpl("Could not find reader for project file " + candidate, null));
            reporter.setSuccess(false);
            return reporter;
        }
    } else {
        // default project file
        reader = new XMLProjectReader();
    }
    LocatableInputSupplier<InputStream> tempSource = new FileIOSupplier(baseFile);
    // save old save configuration
    LocatableInputSupplier<? extends InputStream> oldSource = getSource();
    setSource(tempSource);
    reader.setSource(tempSource);
    reader.setProjectFiles(getProjectFiles());
    IOReport report = reader.execute(progress);
    Project readProject = reader.getProject();
    if (readProject != null) {
        /*
			 * Because the original source is only available here, update the
			 * project's resource paths here.
			 * 
			 * The only drawback is that the UILocationUpdater cannot be used.
			 */
        LocationUpdater updater = new LocationUpdater(readProject, tempSource.getLocation());
        // update resources
        // resources are made absolute (else they can't be found afterwards)
        updater.updateProject(false);
    }
    // set the real source
    setSource(oldSource);
    // set the read project
    setProjectChecked(readProject, reporter);
    return report;
}
Also used : Project(eu.esdihumboldt.hale.common.core.io.project.model.Project) LocationUpdater(eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater) InputStream(java.io.InputStream) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) File(java.io.File) ProjectReader(eu.esdihumboldt.hale.common.core.io.project.ProjectReader)

Example 43 with IOMessageImpl

use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.

the class JaxbAlignmentReader method loadAlignment.

/**
 * @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
 */
@Override
protected MutableAlignment loadAlignment(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Load hale alignment", ProgressIndicator.UNKNOWN);
    InputStream in = getSource().getInput();
    MutableAlignment alignment = null;
    try {
        EntityResolver entityResolver = null;
        if (getServiceProvider() != null) {
            entityResolver = getServiceProvider().getService(EntityResolver.class);
        }
        alignment = JaxbAlignmentIO.load(in, reporter, getSourceSchema(), getTargetSchema(), getPathUpdater(), entityResolver, getServiceProvider());
    } catch (Exception e) {
        reporter.error(new IOMessageImpl(e.getMessage(), e));
        reporter.setSuccess(false);
        return alignment;
    } finally {
        in.close();
    }
    progress.end();
    reporter.setSuccess(true);
    return alignment;
}
Also used : InputStream(java.io.InputStream) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) MutableAlignment(eu.esdihumboldt.hale.common.align.model.MutableAlignment) EntityResolver(eu.esdihumboldt.hale.common.align.io.EntityResolver) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException)

Example 44 with IOMessageImpl

use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.

the class AbstractBaseAlignmentLoader method getCell.

/**
 * Returns the cell in question or null, if it could not be found in which
 * case a suitable warning was generated.
 *
 * @param alignment the alignment which contains the cell
 * @param cellId the cell id
 * @param defaultPrefix the prefix to use if the cell id does not contain a
 *            prefix, may be <code>null</code>
 * @param prefixMapping the prefix map to transform the prefix of the cell
 *            id with, if it has one
 * @param reporter the I/O reporter to report any errors to, may be
 *            <code>null</code>
 * @return the cell in question or <code>null</code>
 */
private Cell getCell(Alignment alignment, String cellId, String defaultPrefix, Map<String, String> prefixMapping, IOReporter reporter) {
    String prefix = defaultPrefix;
    // check if the cell id references another base alignment
    int prefixSplit = cellId.indexOf(':');
    if (prefixSplit != -1) {
        prefix = prefixMapping.get(cellId.substring(0, prefixSplit));
        if (prefix == null) {
            reporter.warn(new IOMessageImpl("A modifier used an unknown cell prefix", null));
            return null;
        }
        cellId = cellId.substring(prefixSplit + 1);
    }
    if (prefix != null)
        cellId = prefix + ':' + cellId;
    Cell cell = alignment.getCell(cellId);
    if (cell == null)
        reporter.warn(new IOMessageImpl("A cell referenced by a modifier could not be found", null));
    return cell;
}
Also used : IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) Cell(eu.esdihumboldt.hale.common.align.model.Cell) ModifiableCell(eu.esdihumboldt.hale.common.align.model.ModifiableCell) MutableCell(eu.esdihumboldt.hale.common.align.model.MutableCell) BaseAlignmentCell(eu.esdihumboldt.hale.common.align.model.BaseAlignmentCell) UnmigratedCell(eu.esdihumboldt.hale.common.align.migrate.impl.UnmigratedCell)

Example 45 with IOMessageImpl

use of eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl in project hale by halestudio.

the class AbstractBaseAlignmentLoader method generatePrefixMapping.

/**
 * Function to fill the prefixMapping and alignmentToInfo maps.
 *
 * @param start the main alignment representation
 * @param prefixMapping gets filled with a mapping from local to global
 *            prefixes
 * @param alignmentToInfo gets filled with a mapping from base alignment
 *            representations to prefixes and URIs
 * @param updater the location updater to use for base alignments
 * @param reporter the reporter
 * @throws IOException if a base alignment couldn't be loaded
 */
private void generatePrefixMapping(A start, Map<A, Map<String, String>> prefixMapping, Map<A, AlignmentInfo> alignmentToInfo, PathUpdate updater, IOReporter reporter) throws IOException {
    // XXX What if the project file path would change?
    // Alignment is a project file, so it is in the same directory.
    URI currentAbsolute = updater.getNewLocation();
    Map<String, URI> base = getBases(start);
    // also a mapping for this alignment itself in case the same URI is
    // defined for two prefixes
    prefixMapping.put(start, new HashMap<String, String>());
    // set of already seen URIs
    Set<URI> knownURIs = new HashSet<URI>();
    // reverse map of base
    Map<URI, String> uriToPrefix = new HashMap<URI, String>();
    // queue of base alignments to process
    LinkedList<URIPair> queue = new LinkedList<URIPair>();
    // check base for doubles, and invert it for later
    for (Entry<String, URI> baseEntry : base.entrySet()) {
        URI rawBaseURI = baseEntry.getValue();
        URI usedBaseURI = updater.findLocation(rawBaseURI, true, false, true);
        if (usedBaseURI == null) {
            throw new IOException("Couldn't load an included alignment (" + rawBaseURI + "). File not found.", null);
        }
        URI absoluteBaseURI = usedBaseURI;
        if (!absoluteBaseURI.isAbsolute())
            absoluteBaseURI = currentAbsolute.resolve(absoluteBaseURI);
        if (knownURIs.contains(absoluteBaseURI)) {
            reporter.warn(new IOMessageImpl("The same base alignment (" + rawBaseURI + ") was included twice.", null));
            prefixMapping.get(start).put(baseEntry.getKey(), uriToPrefix.get(absoluteBaseURI));
        } else {
            knownURIs.add(absoluteBaseURI);
            prefixMapping.get(start).put(baseEntry.getKey(), baseEntry.getKey());
            uriToPrefix.put(absoluteBaseURI, baseEntry.getKey());
            queue.add(new URIPair(absoluteBaseURI, usedBaseURI));
        }
    }
    // find all alignments to load (also missing ones) and load the beans
    while (!queue.isEmpty()) {
        URIPair baseURI = queue.pollFirst();
        A baseA;
        try {
            baseA = loadAlignment(new DefaultInputSupplier(baseURI.absoluteURI).getInput(), reporter);
        } catch (IOException e) {
            throw new IOException("Couldn't load an included alignment (" + baseURI + ").", e);
        }
        // add to alignment info map
        alignmentToInfo.put(baseA, new AlignmentInfo(uriToPrefix.get(baseURI.absoluteURI), baseURI));
        prefixMapping.put(baseA, new HashMap<String, String>());
        // load "missing" base alignments, too, add prefix mapping
        for (Entry<String, URI> baseEntry : getBases(baseA).entrySet()) {
            // rawURI may be relative
            URI rawURI = baseEntry.getValue();
            URI absoluteURI = baseURI.absoluteURI.resolve(rawURI);
            // try updater again, it might help, and it shows whether the
            // file is readable
            absoluteURI = updater.findLocation(absoluteURI, true, false, false);
            if (absoluteURI == null)
                throw new IOException("Couldn't find an included alignment (" + rawURI + ").");
            URI usedURI = absoluteURI;
            // a relative URI.
            if (!baseURI.usedURI.isAbsolute() && !rawURI.isAbsolute())
                usedURI = IOUtils.getRelativePath(absoluteURI, currentAbsolute);
            if (!knownURIs.contains(absoluteURI)) {
                reporter.info(new IOMessageImpl("A base alignment referenced another base alignment (" + absoluteURI + ") that was not yet known. It is now included, too.", null));
                queue.add(new URIPair(absoluteURI, usedURI));
                knownURIs.add(absoluteURI);
                String prefix = generatePrefix(base.keySet());
                base.put(prefix, usedURI);
                uriToPrefix.put(absoluteURI, prefix);
                prefixMapping.get(start).put(prefix, prefix);
            }
            // add prefix mapping
            prefixMapping.get(baseA).put(baseEntry.getKey(), uriToPrefix.get(absoluteURI));
        }
    }
}
Also used : DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) HashMap(java.util.HashMap) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOException(java.io.IOException) URI(java.net.URI) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Aggregations

IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)85 IOException (java.io.IOException)43 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)33 QName (javax.xml.namespace.QName)20 URI (java.net.URI)15 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)14 InputStream (java.io.InputStream)13 File (java.io.File)12 HashMap (java.util.HashMap)11 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)9 FileOutputStream (java.io.FileOutputStream)9 ArrayList (java.util.ArrayList)9 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)8 OutputStream (java.io.OutputStream)8 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)7 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)7 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)6 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)5 PathUpdate (eu.esdihumboldt.hale.common.core.io.PathUpdate)4 Value (eu.esdihumboldt.hale.common.core.io.Value)4