📄 sampleconfig.java
字号:
package abchr.gui;
import abchr.AbstractAttributable;
import abchr.ParseException;
import abchr.SampleHash;
import abchr.XMLable;
import abchr.audio.*;
import org.jdom.Element;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
public class SampleConfig extends AbstractAttributable implements Serializable,XMLable {
private static String convertToRelative(File file,File reference) throws IOException {
File ref=reference.getCanonicalFile();
File f=file.getCanonicalFile();
boolean success=false;
while(ref!=null) {
f=file.getParentFile();
while(f!=null) {
if(f.equals(ref)){success=true;break;}
f=f.getParentFile();
}
if(success){break;}
ref=ref.getParentFile();
}
if(!success){return file.getCanonicalPath();}
StringBuffer path=new StringBuffer();
while(!reference.equals(ref)) {
path.append("..");
path.append(File.separatorChar);
reference=reference.getParentFile();
}
StringBuffer p=new StringBuffer();
while(!file.equals(f)) {
p.insert(0,file.getName());
p.insert(0,File.separatorChar);
file=file.getParentFile();
}
p.deleteCharAt(0);
path.append(p);
return path.toString();
}
private File reference=null;
private String filename;
private Element xml;
public String getFilename(){return filename;}
public void setFilename(String filename){this.filename=filename;}
public File getReference(){return reference;}
public void setReference(File reference) {
absolutizeFilename();
this.reference=reference;
if(reference==null){return;}
try {
this.filename=convertToRelative(new File(filename),reference);
} catch(IOException e) {
e.printStackTrace();
}
}
public void absolutizeFilename() {
filename=getAbsoluteFile().getPath();
reference=null;
}
public File getAbsoluteFile() {
File f=new File(filename);
if(f.isAbsolute()){return f;}
if(reference!=null){f=new File(reference,filename);}
try {
return f.getCanonicalFile();
} catch(IOException e) {
e.printStackTrace();
}
return null;
}
public String toString() {
String s=(String)getAttribute("name");
if(s!=null && !s.equals("")){return s;}else{return filename;}
}
public SampleConfig(String filename,File reference) {
this.filename=filename;
this.reference=reference;
}
public SampleConfig(){this("",null);}
public Element toXML() {
Element root=new Element("sample");
root.addContent(new Element("location").setText(filename));
root.addContent(new Element("offset").setText(Integer.toString(SampleOffset.getOffset(this))));
root.addContent(new Element("gain").setText(Float.toString(SampleGain.getGain(this))));
if(SampleHash.hashPresent(this)) {
root.addContent(new Element("hash").setText(new sun.misc.BASE64Encoder().encode(SampleHash.getHash(this))));
}
List<SampleProcessor> queue=SampleProcessing.getProcessingQueue(this);
if(queue!=null && !queue.isEmpty()) {
Element e=new Element("processing");
SampleProcessor sp;
for(Iterator<SampleProcessor> it=queue.iterator();it.hasNext();) {
sp=it.next();
if(sp instanceof XMLable){e.addContent(((XMLable)sp).toXML());}
}
root.addContent(e);
}
return root;
}
public Element getXML(){return (Element)xml.clone();}
public static SampleConfig readFromXML(Element element,File reference) throws ParseException {
Element e=element.getChild("location");
if(e==null){throw new ParseException("Location element is missing.");}
String filename=e.getText();
SampleConfig config=new SampleConfig(filename.replace('\\',File.separatorChar).replace('/',File.separatorChar),reference);
config.xml=element;
e=element.getChild("offset");
if(e!=null) {
SampleOffset.setOffset(config,Integer.parseInt(e.getText()));
}
e=element.getChild("gain");
if(e!=null) {
SampleGain.setGain(config,Float.parseFloat(e.getText()));
}
e=element.getChild("hash");
if(e!=null) {
byte[] hash;
try {
hash=new sun.misc.BASE64Decoder().decodeBuffer(e.getText());
} catch(IOException ex) {
throw new ParseException("Invalid hash string.");
}
SampleHash.setHash(config,hash);
}
e=element.getChild("processing");
if(e!=null) {
List<SampleProcessor> queue=new ArrayList<SampleProcessor>(4);
List children=e.getChildren();
Element el;
for(Iterator it=children.iterator();it.hasNext();) {
el=(Element)it.next();
if(el.getName().equals("cli")) {
queue.add(CLIProcessor.construct(el));
}
}
SampleProcessing.setProcessingQueue(config,queue);
}
return config;
}
public static SampleConfig makeFromString(String s) {
String[] strings=s.split("\\|");
SampleConfig config=new SampleConfig(strings[0].trim(),null);
StringBuffer name=new StringBuffer(new File(strings[0].trim()).getName());
for(int i=1;i<strings.length;i++) {
name.append(" | ");
CLIProcessor p=CLIProcessor.makeFromString(strings[i]);
name.append(p.getCommandLine());
SampleProcessing.addToProcessingQueue(config,p);
}
config.setAttribute("name",name.toString());
return config;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -