Example usage for javax.xml.bind Unmarshaller setAdapter

List of usage examples for javax.xml.bind Unmarshaller setAdapter

Introduction

In this page you can find the example usage for javax.xml.bind Unmarshaller setAdapter.

Prototype

public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter);

Source Link

Document

Associates a configured instance of XmlAdapter with this unmarshaller.

Usage

From source file:org.azyva.dragom.execcontext.plugins.impl.DefaultWorkspacePluginFactory.java

/**
 * TODO: Review//from  w  w  w .ja va2  s .c  o  m
 * Create the WorkspacePlugin using the following strategy:
 *
 * - For the WorkspacePlugin, the workspace directory is considered. If the system
 *   property org.azyva.dragom.WorkspaceDir is defined, the path specified is
 *   taken as the workspace. Otherwise the current working directory is used.
 * - If the system property org.azyva.dragom.WorkspaceFactory is defined, the
 *   static method getWorkspaceInstance of the specified class is called to obtain
 *   the WorkspacePlugin instance to set in ExecContext.
 * - Otherwise if the workspace directory contains the
 *   .dragom/workspace-init.properties file, the class identified by the
 *   workspace.factory property is used as the factory.
 * - Otherwise (if the workspace directory does not contain the
 *   .dragom/workspace-init.properties file), if the system property
 *   org.azyva.dragom.DefaultWorkspaceFactory is defined, the specified class is
 *   used as the factory.
 * - Otherwise, DefaultWorkspaceFactory is used as the factory.
 * - In all cases once a WorkspacePlugin instance is obtained, it is initialized by
 *   calling either init or load depending on whether the workspace directory
 *   contained the .dragom/workspace-init.properties file.
 *
 * @return See description.
 */
public static WorkspacePlugin getInstance(Properties propertiesInit) {
    ExecContext execContext;
    String workspaceVersion;
    boolean indWorkspaceInit;
    Path pathWorkspace;
    Path pathDragomMetadataDir;
    WorkspaceDefaultImpl workspaceDefaultImpl;

    execContext = ExecContextHolder.get();

    workspaceVersion = execContext.getProperty(DefaultWorkspacePluginFactory.WORKSPACE_VERSION_PROPERTY);

    // We infer the fact that the workspace is initialized or not based on the
    // presence of the workspace-version property. We do not take into consideration
    // whether the workspace is empty or not. This allows for example initializing a
    // Dragom workspace within an Eclipse workspace.
    if (workspaceVersion == null) {
        execContext.setProperty(DefaultWorkspacePluginFactory.WORKSPACE_VERSION_PROPERTY,
                DefaultWorkspacePluginFactory.WORKSPACE_VERSION);
        indWorkspaceInit = false;
    } else {
        if (!workspaceVersion.equals(DefaultWorkspacePluginFactory.WORKSPACE_VERSION)) {
            throw new RuntimeException("Unsupported workspace version " + workspaceVersion + ". Only version "
                    + DefaultWorkspacePluginFactory.WORKSPACE_VERSION
                    + " is supported by this WorkspacePlugin factory.");
        }

        indWorkspaceInit = true;
    }

    pathWorkspace = DefaultExecContextFactory.getPathWorkspace(propertiesInit);
    pathDragomMetadataDir = pathWorkspace.resolve(DefaultExecContextFactory.DRAGOM_METADATA_DIR);

    if (indWorkspaceInit) {
        File fileWorkspaceMetadata;
        JAXBContext jaxbContext;
        Unmarshaller unmarshaller;

        try {
            fileWorkspaceMetadata = pathDragomMetadataDir
                    .resolve(DefaultWorkspacePluginFactory.WORKSPACE_METADATA_FILE).toFile();
            jaxbContext = JAXBContext.newInstance(WorkspaceDefaultImpl.class);
            unmarshaller = jaxbContext.createUnmarshaller();

            // We need to set the MapWorkspaceDirPathXmlAdapter in advance since it requires a
            // non-default constructor.
            unmarshaller.setAdapter(MapWorkspaceDirPathXmlAdapter.class,
                    new MapWorkspaceDirPathXmlAdapter(pathWorkspace));

            workspaceDefaultImpl = (WorkspaceDefaultImpl) unmarshaller.unmarshal(fileWorkspaceMetadata);
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }

        workspaceDefaultImpl.pathWorkspace = pathWorkspace;
        workspaceDefaultImpl.pathDragomMetadataDir = pathDragomMetadataDir;
    } else {
        workspaceDefaultImpl = new WorkspaceDefaultImpl(pathWorkspace, pathDragomMetadataDir);
    }

    return workspaceDefaultImpl;
}

From source file:org.paxle.core.doc.impl.BasicDocumentFactory.java

public <Doc> Doc unmarshal(InputStream input, Map<String, DataHandler> attachments) throws IOException {
    try {//from ww w  .  jav a  2s .c o  m
        final Unmarshaller u = context.createUnmarshaller();
        u.setAdapter(JaxbFileAdapter.class, new JaxbFileAdapter(this.tempFileManager, attachments));
        u.setAdapter(JaxbFieldMapAdapter.class, new JaxbFieldMapAdapter(this.tempFileManager));
        u.setAttachmentUnmarshaller(new JaxbAttachmentUnmarshaller(attachments));
        //         u.setProperty("com.sun.xml.bind.ObjectFactory", new BasicJaxbFactory());

        @SuppressWarnings("unchecked")
        final Doc document = (Doc) u.unmarshal(input);
        return document;
    } catch (JAXBException e) {
        final IOException ioe = new IOException(
                String.format("Unable to unmarshal the document from the stream."));
        ioe.initCause(e);
        throw ioe;
    }
}