001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * $Id$ 019 */ 020 021package org.nuxeo.runtime.model.impl; 022 023import java.io.IOException; 024import java.io.InputStream; 025 026import org.nuxeo.common.xmap.Context; 027import org.nuxeo.common.xmap.XMap; 028import org.nuxeo.common.xmap.XValueFactory; 029import org.nuxeo.runtime.Version; 030import org.nuxeo.runtime.model.ComponentName; 031import org.nuxeo.runtime.model.RuntimeContext; 032 033/** 034 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 035 */ 036public class ExtensionDescriptorReader { 037 038 protected final XMap xmap; 039 040 public ExtensionDescriptorReader() { 041 xmap = new XMap(); 042 xmap.setValueFactory(ComponentName.class, new XValueFactory() { 043 @Override 044 public Object deserialize(Context context, String value) { 045 return new ComponentName(value); 046 } 047 048 @Override 049 public String serialize(Context context, Object value) { 050 if (value != null) { 051 return value.toString(); 052 } 053 return null; 054 } 055 }); 056 xmap.setValueFactory(Version.class, new XValueFactory() { 057 058 @Override 059 public Object deserialize(Context context, String value) { 060 return Version.parseString(value); 061 } 062 063 @Override 064 public String serialize(Context context, Object value) { 065 if (value != null) { 066 return value.toString(); 067 } 068 return null; 069 } 070 }); 071 xmap.register(ExtensionImpl.class); 072 } 073 074 public ExtensionImpl read(RuntimeContext ctx, InputStream in) throws IOException { 075 Object[] result = xmap.loadAll(new XMapContext(ctx), in); 076 if (result.length > 0) { 077 return (ExtensionImpl) result[0]; 078 } 079 return null; 080 } 081 082 public XMap getXMap() { 083 return xmap; 084 } 085 086}