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; 021 022import java.util.logging.Level; 023import java.util.logging.Logger; 024 025import io.konik.zugferd.entity.Parameter; 026import io.konik.zugferd.profile.ConformanceLevel; 027import io.konik.zugferd.profile.Profile; 028import io.konik.zugferd.profile.ProfileVersion; 029 030import javax.xml.bind.annotation.adapters.XmlAdapter; 031 032/** 033 * 034 * JaxB Adapter for mapping Parameter to Profile Enum. 035 */ 036public class ParameterProfileAdapter extends XmlAdapter<Parameter, Profile> { 037 private final static Logger LOG = Logger.getLogger(ParameterProfileAdapter.class.getName()); 038 039 private static final String DELIMITER = ":"; 040 041 @Override 042 public Profile unmarshal(Parameter p) throws Exception { 043 if (p == null) { return null; } 044 String fullName = p.getId(); 045 try { 046 ProfileVersion version = ProfileVersion.extractVersion(fullName); 047 ConformanceLevel conformanceLevel = ConformanceLevel.extractConformanceLevel(fullName); 048 String ns = getNamespace(fullName); 049 return new Profile(ns, version, conformanceLevel); 050 } catch (RuntimeException e) { 051 LOG.log(WARNING, "Could not parse the profile. Fallback to BASIC latest version", e); 052 return new Profile(ConformanceLevel.BASIC); 053 } 054 } 055 056 private static String getNamespace(String fullName) { 057 String[] tokens = fullName.split(DELIMITER); 058 StringBuilder ns = new StringBuilder(); 059 for (int i = 0; i < tokens.length - 2; i++) { 060 ns.append(tokens[i]).append(DELIMITER); 061 } 062 return ns.toString(); 063 } 064 065 @Override 066 public Parameter marshal(Profile profile) throws Exception { 067 if (profile == null) { return null; } 068 return new Parameter(profile.fullName()); 069 } 070}