package tkuri.jxy.arch;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import tkuri.jxy.Util;
import tkuri.uty.Bs;
import tkuri.uty.Buf;
/**
*
*/
public class HeaderList implements Iterable<HeaderPair> {
private List<HeaderPair> list_ = new ArrayList<HeaderPair>();
private Bs activeName_ = Bs.BLANK;
/**
* @param aLine
*/
public void addLine(Bs aLine) {
Bs value = _parse(aLine);
list_.add(new HeaderPair(activeName_, value.trim()));
}
Bs _parse(Bs aLine) {
if (aLine.byteAt(0) == '\t') {
return aLine.trim();
}
int colon = aLine.indexOf(':');
if (colon < 0) {
// ?
return aLine.trim();
} else {
activeName_ = aLine.slice(0, colon).trim();
return aLine.sub(colon + 1).trim();
}
}
public Bs value(Bs aName) {
return value(aName, 0);
}
/**
* @param aName
* @param sDelim
* @return 存在しなければ null
*/
public Bs value(Bs aName, int aDelim) {
Buf buf = new Buf();
for (HeaderPair hp: list_) {
if (hp.name.equalsIgnoreCase(aName)) {
if (0 < buf.length() && aDelim != 0) {
buf.append(aDelim);
}
buf.append(hp.value);
}
}
return buf.toBs();
}
/**
*
* @param aName
* @param aDelim
* @return
*/
public List<Bs> values(Bs aName, int aDelim) {
List<Bs> r = new ArrayList<Bs>();
for (HeaderPair hp: list_) {
if (hp.name.equalsIgnoreCase(aName)) {
for (Bs v: Util.splitTrim(hp.value, aDelim)) {
r.add(v);
}
}
}
return r;
}
/**
*/
@Override
public Iterator<HeaderPair> iterator() {
return list_.iterator();
}
}