001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Nuxeo - initial API and implementation 011 * $Id$ 012 */ 013 014package org.nuxeo.runtime.model.impl; 015 016import java.io.IOException; 017import java.io.InputStream; 018 019import org.nuxeo.common.xmap.Context; 020import org.nuxeo.common.xmap.XMap; 021import org.nuxeo.common.xmap.XValueFactory; 022import org.nuxeo.runtime.Version; 023import org.nuxeo.runtime.model.ComponentName; 024import org.nuxeo.runtime.model.RuntimeContext; 025 026/** 027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 028 */ 029public class ExtensionDescriptorReader { 030 031 protected final XMap xmap; 032 033 public ExtensionDescriptorReader() { 034 xmap = new XMap(); 035 xmap.setValueFactory(ComponentName.class, new XValueFactory() { 036 @Override 037 public Object deserialize(Context context, String value) { 038 return new ComponentName(value); 039 } 040 041 @Override 042 public String serialize(Context context, Object value) { 043 if (value != null) { 044 return value.toString(); 045 } 046 return null; 047 } 048 }); 049 xmap.setValueFactory(Version.class, new XValueFactory() { 050 051 @Override 052 public Object deserialize(Context context, String value) { 053 return Version.parseString(value); 054 } 055 056 @Override 057 public String serialize(Context context, Object value) { 058 if (value != null) { 059 return value.toString(); 060 } 061 return null; 062 } 063 }); 064 xmap.register(ExtensionImpl.class); 065 } 066 067 public ExtensionImpl read(RuntimeContext ctx, InputStream in) throws IOException { 068 Object[] result = xmap.loadAll(new XMapContext(ctx), in); 069 if (result.length > 0) { 070 return (ExtensionImpl) result[0]; 071 } 072 return null; 073 } 074 075 public XMap getXMap() { 076 return xmap; 077 } 078 079}