📄 intarraymodelpersister.java
字号:
/*******************************************************************************
* Copyright (c) 2004 Stefan Zeiger and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.novocode.com/legal/epl-v10.html
*
* Contributors:
* Stefan Zeiger (szeiger@novocode.com) - initial API and implementation
*******************************************************************************/
package com.novocode.naf.persist;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.novocode.naf.app.NAFException;
import com.novocode.naf.model.IObjectModel;
/**
* Persister for int[] models.
*
* @author Stefan Zeiger (szeiger@novocode.com)
* @since Oct 21, 2004
* @version $Id: IntArrayModelPersister.java,v 1.3 2004/12/16 14:52:04 szeiger Exp $
*/
public class IntArrayModelPersister<T extends IObjectModel<int[]>> implements IPersister<T>
{
public void toModel(Element e, T model) throws NAFException
{
if("null".equals(e.getLocalName())) model.setValue(null);
else
{
ArrayList<String> l = new ArrayList<String>();
StringTokenizer tok = new StringTokenizer(e.getTextContent(), ",");
while(tok.hasMoreTokens()) l.add(tok.nextToken().trim());
int[] a = new int[l.size()];
for(int i=0; i<a.length; i++) a[i] = Integer.parseInt(l.get(i));
model.setValue(a);
}
}
public Element toDOM(Document doc, T model) throws NAFException
{
int[] val = model.getValue();
if(val == null) return doc.createElement("null");
Element e = doc.createElement("int-array");
StringBuilder b = new StringBuilder();
for(int v : val)
{
if(b.length() != 0) b.append(',');
b.append(v);
}
e.setTextContent(b.toString());
return e;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -