001/* Copyright (C) 2014 konik.io 002 * 003 * This file is part of the Konik library. 004 * 005 * The Konik library is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * The Konik library 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 Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with the Konik library. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package io.konik.jaxb.adapter; 019 020import static java.util.logging.Level.WARNING; 021import io.konik.zugferd.entity.Parameter; 022import io.konik.zugferd.profile.ConformanceLevel; 023import io.konik.zugferd.profile.Profile; 024import io.konik.zugferd.profile.ProfileVersion; 025 026import java.util.logging.Logger; 027 028import javax.xml.bind.annotation.adapters.XmlAdapter; 029 030/** 031 * 032 * JaxB Adapter for mapping Parameter to Profile Enum. 033 */ 034public class ParameterProfileAdapter extends XmlAdapter<Parameter, Profile> { 035 036 private static final Logger LOG = Logger.getLogger(ParameterProfileAdapter.class.getName()); 037 private static final String DELIMITER = ":"; 038 039 @Override 040 public Profile unmarshal(Parameter p) throws Exception { 041 if (p == null) { return null; } 042 String fullName = p.getId(); 043 try { 044 ProfileVersion version = ProfileVersion.extractVersion(fullName); 045 ConformanceLevel conformanceLevel = ConformanceLevel.extractConformanceLevel(fullName); 046 String ns = getNamespace(fullName); 047 return new Profile(ns, version, conformanceLevel); 048 } catch (RuntimeException e) { 049 LOG.log(WARNING, "Could not parse the profile. Fallback to BASIC latest version", e); 050 return new Profile(ConformanceLevel.BASIC); 051 } 052 } 053 054 private static String getNamespace(String fullName) { 055 String[] tokens = fullName.split(DELIMITER); 056 StringBuilder ns = new StringBuilder(); 057 for (int i = 0; i < tokens.length - 2; i++) { 058 ns.append(tokens[i]).append(DELIMITER); 059 } 060 return ns.toString(); 061 } 062 063 @Override 064 public Parameter marshal(Profile profile) throws Exception { 065 if (profile == null) { return null; } 066 return new Parameter(profile.fullName()); 067 } 068}