001/**
002 * Java Web Archive Toolkit - Software to read and validate ARC, WARC
003 * and GZip files. (http://jwat.org/)
004 * Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/)
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.jwat.common;
019
020import java.io.UnsupportedEncodingException;
021import java.util.HashMap;
022import java.util.Iterator;
023import java.util.LinkedList;
024import java.util.List;
025import java.util.Map;
026
027/**
028 * Internet-Draft: draft-kunze-anvl-02.txt
029 * http://tools.ietf.org/id/draft-kunze-anvl-02.txt
030 *
031 * @author nicl
032 */
033public class ANVLRecord {
034
035    public static final String CRLF = "\r\n";
036
037    protected List<String> list = new LinkedList<String>();
038
039    protected Map<String, String> map = new HashMap<String, String>();
040
041    public ANVLRecord() {
042    }
043
044    public void addLabelValue(String name, String value) {
045        int c = -1;
046        if (value == null) {
047            throw new IllegalArgumentException("Invalid value!");
048        } else if (value.length() > 0) {
049            c = value.charAt(0);
050        }
051        if (name == null || name.length() == 0) {
052            throw new IllegalArgumentException("Invalid name!");
053        }
054        if (c != -1 && c != ' ' && c != '\t') {
055            list.add(name + ": " + value);
056        } else {
057            list.add(name + ":" + value);
058        }
059        map.put(name, value);
060    }
061
062    public void addValue(String value) {
063        if (value == null || value.length() == 0) {
064            throw new IllegalArgumentException("Invalid value!");
065        }
066        list.add(value);
067    }
068
069    public String toString() {
070        StringBuilder sb = new StringBuilder();
071        Iterator<String> iter = list.iterator();
072        while (iter.hasNext()) {
073            sb.append(iter.next());
074            sb.append(CRLF);
075        }
076        sb.append(CRLF);
077        return sb.toString();
078    }
079
080    public byte[] getBytes(String charsetName) throws UnsupportedEncodingException {
081        return toString().getBytes(charsetName);
082    }
083
084    public byte[] getUTF8Bytes() throws UnsupportedEncodingException {
085        return getBytes("UTF-8");
086    }
087
088    public ANVLRecord parse() {
089        return null;
090    }
091
092}