Example usage for javax.xml.bind JAXB unmarshal

List of usage examples for javax.xml.bind JAXB unmarshal

Introduction

In this page you can find the example usage for javax.xml.bind JAXB unmarshal.

Prototype

public static <T> T unmarshal(Source xml, Class<T> type) 

Source Link

Document

Reads in a Java object tree from the given XML input.

Usage

From source file:org.ebayopensource.turmeric.services.monitoringservice.junit.AbstractSOAQueryMetricsTest.java

/**
 * Test get metrics data./*from  w  ww .jav a  2s . c o m*/
 * 
 * @param requestXmlPath
 *            the request xml path
 * @param respXmlPath
 *            the resp xml path
 * @throws Exception
 *             the exception
 */
public void testGetMetricsData(String requestXmlPath, String respXmlPath) throws Exception {
    ClassLoader cl = AbstractSOAQueryMetricsTest.class.getClassLoader();
    InputStreamReader requestis = new InputStreamReader(cl.getResourceAsStream(requestXmlPath));
    InputStreamReader responseis = new InputStreamReader(cl.getResourceAsStream(respXmlPath));

    GetMetricsRequest req = JAXB.unmarshal(requestis, GetMetricsRequest.class);

    GetMetricsResponse resp = consumer.getMetricsData(req);
    ByteArrayOutputStream xmlResp = new ByteArrayOutputStream();
    JAXB.marshal(resp, xmlResp);
    StringReader str = new StringReader(xmlResp.toString());
    System.out.println(xmlResp.toString());
    assertXMLEqual(responseis, str);
}

From source file:org.ebayopensource.turmeric.tools.library.builders.CodeGenTypeLibraryGenerator.java

/**
 * //ww w  .j a va2s  .  co  m
 * @param projectRoot
 * @param libraryName
 * @param stagingLocation
 * @param dependentTypeLibs
 * @param codeGenCtx 
 * @throws Exception
 */
public static void genTypeCleanBuildTypeLibrary(String projectRoot, String libraryName, String stagingLocation,
        String dependentTypeLibs, TypeLibraryCodeGenContext codeGenCtx)
        throws CodeGenFailedException, Exception {

    // TODO: This entire process should be a class of its own, that looks up information in a pre-described order.

    String libraryVersion = codeGenCtx.getTypeLibraryInputOptions().getLibraryVersion();
    String libNamespace = codeGenCtx.getTypeLibraryInputOptions().getLibraryNamespace();
    String libCategory = codeGenCtx.getTypeLibraryInputOptions().getLibraryCategory();

    // Are any of these values not specified on the command line?
    if (CodeGenUtil.isEmptyString(libraryVersion) || CodeGenUtil.isEmptyString(libNamespace)
            || CodeGenUtil.isEmptyString(libCategory)) {
        // We need these values from the "type_library_project.properties" file.
        String propertiesFilePath = TypeLibraryUtilities.toOSFilePath(projectRoot)
                + TypeLibraryProjectPropertiesGenerator.TYPE_LIB_PRJ_PROPERTIES_FILE_NAME;

        Properties typeLibraryProperties = TypeLibraryUtilities.getPropertiesFromFile(propertiesFilePath);

        if (CodeGenUtil.isEmptyString(libraryVersion)) {
            libraryVersion = typeLibraryProperties.getProperty(TypeLibraryConstants.TYPE_LIBRARY_VERSION);
        }

        if (CodeGenUtil.isEmptyString(libNamespace)) {
            libNamespace = typeLibraryProperties.getProperty(TypeLibraryConstants.TYPE_LIBRARY_NAMESPACE);
        }

        if (CodeGenUtil.isEmptyString(libNamespace)) {
            libCategory = typeLibraryProperties.getProperty(TypeLibraryConstants.TYPE_LIBRARY_CATEGORY);
        }
    }

    // Are any of these values not specified from the properties file?
    if (CodeGenUtil.isEmptyString(libraryVersion) || CodeGenUtil.isEmptyString(libNamespace)
            || CodeGenUtil.isEmptyString(libCategory)) {
        // We need these values from the "TypeInformation.xml" file.
        String typeInfoXMLFilePath = TypeLibraryUtilities.getTypeInfoFolder(codeGenCtx, libraryName)
                + File.separator + TYPE_INFORMATION_FILE_NAME;

        File typeInfoFile = new File(typeInfoXMLFilePath);
        if (typeInfoFile.exists()) {
            TypeLibraryType typeLibraryType = JAXB.unmarshal(typeInfoFile, TypeLibraryType.class);
            if (CodeGenUtil.isEmptyString(libraryVersion)) {
                libraryVersion = typeLibraryType.getVersion();
            }
            if (CodeGenUtil.isEmptyString(libNamespace)) {
                libNamespace = typeLibraryType.getLibraryNamespace();
            }
            if (CodeGenUtil.isEmptyString(libCategory)) {
                libCategory = typeLibraryType.getCategory();
            }
        }
    }

    // Default any values that are *still* unspecified
    if (CodeGenUtil.isEmptyString(libraryVersion)) {
        libraryVersion = TypeLibraryConstants.TYPE_LIBRARY_DEFAULT_VERSION;
    }
    if (CodeGenUtil.isEmptyString(libNamespace)) {
        libNamespace = TypeLibraryConstants.TYPE_INFORMATION_NAMESPACE;
    }
    if (CodeGenUtil.isEmptyString(libCategory)) {
        libCategory = TypeLibraryConstants.TYPE_LIBRARY_DEFAULT_CATEGORY;
    }

    //Setting codegenContext
    codeGenCtx.getLibrariesNamespace().put(libraryName, libNamespace);

    genTypeCleanBuildTypeLibraryPrivate(libraryName, libraryVersion, libCategory, stagingLocation,
            dependentTypeLibs, libNamespace, codeGenCtx);
}

From source file:org.ebayopensource.turmeric.tools.library.builders.CodeGenTypeLibraryGenerator.java

/**
 * /*  w  w w  . j  a  v a2 s .  com*/
 * @param projectRoot
 * @param libraryName
 * @return
 * @throws Exception
 */
private static String getNamespaceOfLibrary(TypeLibraryCodeGenContext codeGenCtx, String libraryName)
        throws BadInputValueException, Exception {

    String libraryNamespace = null;

    String typeInfoXMLFilePath = TypeLibraryUtilities.getTypeInfoFolder(codeGenCtx, libraryName)
            + File.separator + TYPE_INFORMATION_FILE_NAME;

    File typeInfoFile = new File(typeInfoXMLFilePath);
    if (!typeInfoFile.exists())
        throw new Exception("Could not find the TypeInformation.xml file for library " + libraryName
                + "  at location : " + typeInfoXMLFilePath);

    try {
        TypeLibraryType typeLibraryType = JAXB.unmarshal(typeInfoFile, TypeLibraryType.class);
        TypeInformationParser.getInstance().populateTypeInfoGlobalTable(typeLibraryType, libraryName);
        libraryNamespace = typeLibraryType.getLibraryNamespace();

    } catch (Throwable t) {
        String errMsg = "Unable to parse the TypeInformation.xml file, of library " + libraryName
                + " its content could be invalid";
        getLogger().log(Level.SEVERE, errMsg, t);
        throw new BadInputValueException(errMsg, t);
    }

    return libraryNamespace;
}

From source file:org.ebayopensource.turmeric.tools.library.builders.CodeGenTypeLibraryGenerator.java

private static void updateGlobalTableWithNewLibrary(TypeLibraryCodeGenContext codeGenCtx, String libraryName)
        throws Exception {
    String typeInfoXMLFilePath = TypeLibraryUtilities.getTypeInfoFolder(codeGenCtx, libraryName)
            + File.separator + TYPE_INFORMATION_FILE_NAME;

    File typeInfoFile = new File(typeInfoXMLFilePath);
    if (!typeInfoFile.exists())
        throw new Exception("Could not find the TypeInformation.xml file for library " + libraryName
                + "  at location : " + typeInfoXMLFilePath);

    try {//ww  w. j a v  a2 s. c om
        TypeLibraryType typeLibraryType = JAXB.unmarshal(typeInfoFile, TypeLibraryType.class);
        TypeInformationParser.getInstance().populateTypeInfoGlobalTable(typeLibraryType, libraryName);

    } catch (Throwable t) {
        getLogger().log(Level.SEVERE, "Unable to parse the TypeInformation.xml file, of library " + libraryName
                + " its content could be invalid", t);
        throw new Exception(t);
    }

}

From source file:org.ebayopensource.turmeric.tools.library.builders.CodeGenTypeLibraryGenerator.java

private static void deleteTheGeneratedDependentTypesForV3(String dependentJarPath,
        TypeLibraryCodeGenContext codeGenCtx) {

    if (TypeLibraryUtilities.isEmptyString(dependentJarPath))
        return;//from w w  w .j a v  a  2  s.c o m

    String[] dependentLibrariesJarPaths = dependentJarPath.split(DEPENDENT_JARS_DELIMITER);

    if (dependentLibrariesJarPaths.length == 0)
        return;

    //identify the possible java types to be deleted
    List<String> javaTypesToDelete = new ArrayList<String>();
    //Need to check if ObjectFactory is being deleted.
    //One ObjectFactory per library.Need to get a set of all namespaces being referred.
    Set<String> dependentLibsNamespace = new HashSet<String>();

    for (String currDepLibraryJar : dependentLibrariesJarPaths) {
        String currLibraryName = getLibraryNameFromJarFilePath(currDepLibraryJar);
        //for referred libraries  the TypeInformation.xml from the related jar . this jar won't be available in classpath and hence has to be mnaually processed
        String typeInformationFileRelativePath = TypeLibraryConstants.META_INF_FOLDER + File.separator
                + currLibraryName + File.separator + TypeLibraryConstants.TYPE_INFORMATION_FILE_NAME;

        File theJarFile = new File(currDepLibraryJar);
        if (!theJarFile.exists())
            continue;

        JarFile jarFile = null;
        try {
            jarFile = new JarFile(currDepLibraryJar);
            JarEntry entry = jarFile.getJarEntry(typeInformationFileRelativePath);
            if (entry == null)
                entry = jarFile.getJarEntry(typeInformationFileRelativePath.replace("\\", "/"));
            if (entry == null) {
                getLogger().log(Level.WARNING,
                        "Could not find the TypeInformation.xml file for the dependent library represented by the jar : "
                                + currDepLibraryJar);
                continue;
            }
            InputStream inputStream = jarFile.getInputStream(entry);

            if (inputStream != null) {

                TypeLibraryType typeLibraryType = JAXB.unmarshal(inputStream, TypeLibraryType.class);
                if (typeLibraryType != null) {
                    dependentLibsNamespace.add(typeLibraryType.getLibraryNamespace());
                    for (TypeInformationType typeInformationType : typeLibraryType.getType())
                        javaTypesToDelete.add(typeInformationType.getJavaTypeName());
                }

            }

        } catch (IOException e) {
            getLogger().log(Level.WARNING,
                    "Exception while parsing the TypeInformation.xml of jar " + currDepLibraryJar, e);
        }
    }

    deleteJavaTypes(javaTypesToDelete, codeGenCtx);
    findPackageForObjectFactoriesAndDelete(codeGenCtx, dependentLibsNamespace);
}

From source file:org.ebayopensource.turmeric.tools.library.TypeLibraryRegistryInterfaceTest.java

@Test
public void addDependencyToRegistry() throws Exception {
    String typeDependenciesFilePath = TypeLibraryConstants.META_INF_FOLDER + "/" + m_libraryName + "/"
            + "NewTypeDependencies.xml";
    ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader();
    TypeLibraryDependencyType typeLibraryDependencyType = null;
    String typeName = "harddiskType";

    String libNS = getTypeLibrary(m_libraryName).getLibraryNamespace();
    LibraryType libraryType = new LibraryType();
    libraryType.setName(typeName);/*from w w w . j  a  v  a  2 s .  c  o m*/
    libraryType.setNamespace(libNS);

    int initialSize = m_soaTypeRegistry.getDependentParentTypeFiles(libraryType).size();

    InputStream inStream = null;
    try {
        inStream = myClassLoader.getResourceAsStream(typeDependenciesFilePath);
        typeLibraryDependencyType = JAXB.unmarshal(inStream, TypeLibraryDependencyType.class);

        for (TypeDependencyType type : typeLibraryDependencyType.getType()) {
            if (type.getName().equals(typeName)) {
                m_soaTypeRegistry.addDependencyToRegistry(type, m_libraryName);
            }
        }

        int finalSize = m_soaTypeRegistry.getDependentParentTypeFiles(libraryType).size();
        assertTrue(finalSize > initialSize);
    } finally {
        IOUtils.closeQuietly(inStream);
    }
}

From source file:org.ebayopensource.turmeric.tools.library.TypeLibraryRegistryInterfaceTest.java

@Test
public void removalOfPartialDependencyFromTheDependencyFile() throws Exception {
    String typeDependenciesFilePath = TypeLibraryConstants.META_INF_FOLDER + "/" + m_libraryName + "/"
            + "NewTypeDependencies.xml";
    ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader();
    TypeLibraryDependencyType typeLibraryDependencyType = null;
    String typeName = "speeddetailsramType";

    String libNS = getTypeLibrary(m_libraryName).getLibraryNamespace();
    LibraryType libraryType = new LibraryType();
    libraryType.setName(typeName);/*from  w  w  w  . ja  v  a  2 s . com*/
    libraryType.setNamespace(libNS);

    int initialSize = m_soaTypeRegistry.getDependentChildTypeFiles(libraryType).size();
    InputStream inStream = null;
    try {
        inStream = myClassLoader.getResourceAsStream(typeDependenciesFilePath);
        typeLibraryDependencyType = JAXB.unmarshal(inStream, TypeLibraryDependencyType.class);

        for (TypeDependencyType type : typeLibraryDependencyType.getType()) {
            m_soaTypeRegistry.addDependencyToRegistry(type, m_libraryName);
        }

        int finalSize = m_soaTypeRegistry.getDependentChildTypeFiles(libraryType).size();
        assertTrue(initialSize > finalSize);
    } finally {
        IOUtils.closeQuietly(inStream);
    }
}

From source file:org.ebayopensource.turmeric.tools.library.TypeLibraryRegistryInterfaceTest.java

@Test
public void removalOfTotalDependencyFromTheDependencyFile() throws Exception {
    List<String> lib = new ArrayList<String>();
    lib.add(m_libraryName);/*from  ww w.  j a v  a 2 s .  co  m*/

    m_soaTypeRegistry.populateRegistryWithTypeLibraries(lib);

    String libNS = getTypeLibrary(m_libraryName).getLibraryNamespace();
    String typeName = "processorType";
    LibraryType libraryType = new LibraryType();
    libraryType.setName(typeName);
    libraryType.setNamespace(libNS);

    int initialNumberOfDirectParents = m_soaTypeRegistry.getDependentParentTypeFiles(libraryType, 1).size();

    String typeName2 = "processordetailsType";
    LibraryType libraryType2 = new LibraryType();
    libraryType2.setName(typeName2);
    libraryType2.setNamespace(libNS);
    int initialNumberOfDirectChilds = m_soaTypeRegistry.getDependentChildTypeFiles(libraryType2, 1).size();

    String typeDependenciesFilePath = TypeLibraryConstants.META_INF_FOLDER + "/" + m_libraryName + "/"
            + "NewTypeDependenciesDependencyTotallyRemoved.xml";
    ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader();
    TypeLibraryDependencyType typeLibraryDependencyType = null;

    InputStream inStream = null;
    try {
        inStream = myClassLoader.getResourceAsStream(typeDependenciesFilePath);
        typeLibraryDependencyType = JAXB.unmarshal(inStream, TypeLibraryDependencyType.class);

        TypeDependencyParser typeDependencyParser = TypeDependencyParser.getInstance();
        typeDependencyParser.processTypeLibraryDependencyType(typeLibraryDependencyType);

        int finalNumberOfParents = m_soaTypeRegistry.getDependentParentTypeFiles(libraryType, 1).size();
        int finalNumberOfDirectChilds = m_soaTypeRegistry.getDependentChildTypeFiles(libraryType2, 1).size();

        boolean areParentsGood = initialNumberOfDirectParents > 0 && finalNumberOfParents == 0;
        boolean areChildsGood = ((initialNumberOfDirectChilds - finalNumberOfDirectChilds) == 1) ? true : false;

        assertTrue(areParentsGood && areChildsGood);
    } finally {
        IOUtils.closeQuietly(inStream);
    }
}

From source file:org.ebayopensource.turmeric.tools.library.utils.TypeLibraryUtilities.java

@Deprecated
public static Set<String> findDependentLibrariesForAType(TypeLibraryCodeGenContext ctx, String typeLibraryName,
        String typeName) throws Exception {
    getLogger().log(Level.INFO,/*from   w  ww .j a v a 2 s .c  o  m*/
            "Input params for findDependentLibrariesForAType \n" + "projectRoot :" + ctx.getProjectRoot() + "\n"
                    + "typelibrary name :" + typeLibraryName + "\n" + "typeName :" + typeName);

    Set<String> depLibraryNames = new HashSet<String>();

    TypeLibraryDependencyType typeLibraryDependencyType = null;

    if (!ctx.isProjectRootBlank()) {
        String typeDefsFolder = TypeLibraryUtilities.getTypeDepFolder(ctx, typeLibraryName);
        File typeDepFile = new File(
                typeDefsFolder + File.separator + TypeLibraryConstants.TYPE_DEPENDENCIES_FILE_NAME);
        if (!typeDepFile.exists())
            return depLibraryNames; //Its not mandatory for a project to have TypeDependencies.xml file

        FileInputStream fis = null;
        try {
            fis = new FileInputStream(typeDepFile);
            typeLibraryDependencyType = JAXB.unmarshal(fis, TypeLibraryDependencyType.class);
        } catch (IOException e) {
            getLogger().log(Level.WARNING, "Could not find the "
                    + TypeLibraryConstants.TYPE_DEPENDENCIES_FILE_NAME + " for the library " + typeLibraryName
                    + "  in the method findDependentLibrariesForAType in TypeLibraryUtilities , \n the file was searched at location "
                    + typeDepFile.getAbsolutePath(), e);
            return depLibraryNames;
        } finally {
            IOUtils.closeQuietly(fis);
        }
    }

    if (typeLibraryDependencyType == null)
        typeLibraryDependencyType = getTypeLibraryDependencyTypeForLibrary(typeLibraryName);

    getLogger().log(Level.INFO,
            "Calling function findDependantLibrariesRecursivelyAtInvidualTypeLevel for type : " + typeName);

    ProcessedType typeToBeProcessed = new ProcessedType(typeName, typeLibraryName);
    Set<ProcessedType> processedTypes = new HashSet<ProcessedType>();

    findDependantLibrariesRecursivelyAtIndividualTypeLevel(typeLibraryDependencyType, typeToBeProcessed,
            depLibraryNames, processedTypes);

    getLogger().exiting();

    return depLibraryNames;
}

From source file:org.ebayopensource.turmeric.tools.library.utils.TypeLibraryUtilities.java

private static TypeLibraryDependencyType getTypeLibraryDependencyTypeForLibrary(String libraryName) {
    getLogger().log(Level.INFO, "Entering method getTypeLibraryDependencyTypeForLibrary for : " + libraryName);

    String defaultTypeDepFilePath = TypeLibraryConstants.META_INF_FOLDER + "/" + libraryName + "/"
            + TypeLibraryConstants.TYPE_DEPENDENCIES_FILE_NAME;

    ClassLoader myClassLoader = TypeLibraryUtilities.class.getClassLoader();
    InputStream inStream = null;//from w ww .  j a v  a  2s .c o  m
    try {
        inStream = myClassLoader.getResourceAsStream(defaultTypeDepFilePath);

        if (inStream == null) {
            getLogger().log(Level.WARNING,
                    "Could not find the " + TypeLibraryConstants.TYPE_DEPENDENCIES_FILE_NAME
                            + " for the library " + libraryName + " using the first classloader "
                            + myClassLoader);

            myClassLoader = Thread.currentThread().getContextClassLoader();
            inStream = myClassLoader.getResourceAsStream(defaultTypeDepFilePath);
        }

        if (inStream == null) {
            getLogger().log(Level.WARNING, "Could not find the "
                    + TypeLibraryConstants.TYPE_DEPENDENCIES_FILE_NAME + " for the library " + libraryName
                    + "  in the method getTypeLibraryDependencyTypeForLibrary in TypeLibraryUtilities using classloader "
                    + myClassLoader);
            return null;
        }

        getLogger().log(Level.INFO, "Found the " + TypeLibraryConstants.TYPE_DEPENDENCIES_FILE_NAME
                + " for the library " + libraryName
                + "  in the method getTypeLibraryDependencyTypeForLibrary in TypeLibraryUtilities using classloader "
                + myClassLoader + "\n" + "The path used was : " + defaultTypeDepFilePath);

        TypeLibraryDependencyType typeLibraryDependencyType = JAXB.unmarshal(inStream,
                TypeLibraryDependencyType.class);

        return typeLibraryDependencyType;
    } finally {
        CodeGenUtil.closeQuietly(inStream);
    }
}