use of nu.xom.Elements in project CoreNLP by stanfordnlp.
the class ParsedGigawordReader method toAnnotation.
/*
* Old implementation based on JDOM.
* No longer maintained due to JDOM licensing issues.
private static Annotation toAnnotation(String xml) throws IOException {
Element docElem;
try {
docElem = new SAXBuilder().build(new StringReader(xml)).getRootElement();
} catch (JDOMException e) {
throw new RuntimeException(String.format("error:\n%s\ninput:\n%s", e, xml));
}
Element textElem = docElem.getChild("TEXT");
StringBuilder text = new StringBuilder();
int offset = 0;
List<CoreMap> sentences = new ArrayList<CoreMap>();
for (Object sentObj: textElem.getChildren("SENT")) {
CoreMap sentence = new ArrayCoreMap();
sentence.set(CoreAnnotations.CharacterOffsetBeginAnnotation.class, offset);
Element sentElem = (Element)sentObj;
Tree tree = Tree.valueOf(sentElem.getText());
List<CoreLabel> tokens = new ArrayList<CoreLabel>();
List<Tree> preTerminals = preTerminals(tree);
for (Tree preTerminal: preTerminals) {
String posTag = preTerminal.value();
for (Tree wordTree: preTerminal.children()) {
String word = wordTree.value();
CoreLabel token = new CoreLabel();
token.set(CoreAnnotations.TextAnnotation.class, word);
token.set(CoreAnnotations.TextAnnotation.class, word);
token.set(CoreAnnotations.PartOfSpeechAnnotation.class, posTag);
token.set(CoreAnnotations.CharacterOffsetBeginAnnotation.class, offset);
offset += word.length();
token.set(CoreAnnotations.CharacterOffsetEndAnnotation.class, offset);
text.append(word);
text.append(' ');
offset += 1;
tokens.add(token);
}
}
if (preTerminals.size() > 0) {
text.setCharAt(text.length() - 1, '\n');
}
sentence.set(CoreAnnotations.CharacterOffsetEndAnnotation.class, offset - 1);
sentence.set(CoreAnnotations.TokensAnnotation.class, tokens);
sentence.set(TreeCoreAnnotations.TreeAnnotation.class, tree);
sentences.add(sentence);
}
String docID = docElem.getAttributeValue("id");
Matcher matcher = datePattern.matcher(docID);
matcher.find();
Calendar docDate = new Timex(matcher.group(1)).getDate();
Annotation document = new Annotation(text.toString());
document.set(CoreAnnotations.DocIDAnnotation.class, docID);
document.set(CoreAnnotations.CalendarAnnotation.class, docDate);
document.set(CoreAnnotations.SentencesAnnotation.class, sentences);
return document;
}
*/
private static Annotation toAnnotation(String xml) throws IOException {
Element docElem;
try {
Builder parser = new Builder();
StringReader in = new StringReader(xml);
docElem = parser.build(in).getRootElement();
} catch (ParsingException e) {
throw new RuntimeException(String.format("error:\n%s\ninput:\n%s", e, xml));
} catch (IOException e) {
throw new RuntimeException(String.format("error:\n%s\ninput:\n%s", e, xml));
}
Element textElem = docElem.getFirstChildElement("TEXT");
StringBuilder text = new StringBuilder();
int offset = 0;
List<CoreMap> sentences = new ArrayList<>();
Elements sentenceElements = textElem.getChildElements("SENT");
for (int crtsent = 0; crtsent < sentenceElements.size(); crtsent++) {
Element sentElem = sentenceElements.get(crtsent);
CoreMap sentence = new ArrayCoreMap();
sentence.set(CoreAnnotations.CharacterOffsetBeginAnnotation.class, offset);
// XXX ms: is this the same as sentElem.getText() in JDOM?
Tree tree = Tree.valueOf(sentElem.getChild(0).getValue());
List<CoreLabel> tokens = new ArrayList<>();
List<Tree> preTerminals = preTerminals(tree);
for (Tree preTerminal : preTerminals) {
String posTag = preTerminal.value();
for (Tree wordTree : preTerminal.children()) {
String word = wordTree.value();
CoreLabel token = new CoreLabel();
token.set(CoreAnnotations.TextAnnotation.class, word);
token.set(CoreAnnotations.TextAnnotation.class, word);
token.set(CoreAnnotations.PartOfSpeechAnnotation.class, posTag);
token.set(CoreAnnotations.CharacterOffsetBeginAnnotation.class, offset);
offset += word.length();
token.set(CoreAnnotations.CharacterOffsetEndAnnotation.class, offset);
text.append(word);
text.append(' ');
offset += 1;
tokens.add(token);
}
}
if (preTerminals.size() > 0) {
text.setCharAt(text.length() - 1, '\n');
}
sentence.set(CoreAnnotations.CharacterOffsetEndAnnotation.class, offset - 1);
sentence.set(CoreAnnotations.TokensAnnotation.class, tokens);
sentence.set(TreeCoreAnnotations.TreeAnnotation.class, tree);
sentences.add(sentence);
}
String docID = docElem.getAttributeValue("id");
Matcher matcher = datePattern.matcher(docID);
matcher.find();
Calendar docDate = new Timex("DATE", matcher.group(1)).getDate();
Annotation document = new Annotation(text.toString());
document.set(CoreAnnotations.DocIDAnnotation.class, docID);
document.set(CoreAnnotations.CalendarAnnotation.class, docDate);
document.set(CoreAnnotations.SentencesAnnotation.class, sentences);
return document;
}
use of nu.xom.Elements in project apn-proxy by apn-proxy.
the class ApnProxyConfigReader method realReadProcess.
@Override
protected void realReadProcess(Element rootElement) {
Elements listenTypeElements = rootElement.getChildElements("listen-type");
if (listenTypeElements.size() == 1) {
String _listenType = listenTypeElements.get(0).getValue();
ApnProxyListenType listenType = ApnProxyListenType.fromString(_listenType);
ApnProxyConfig.getConfig().setListenType(listenType);
if (listenType == ApnProxyListenType.AES) {
Elements keyElements = rootElement.getChildElements("key");
if (keyElements.size() != 1) {
throw new ApnProxyConfigException("Wrong config for: key");
}
String _key = keyElements.get(0).getValue();
Elements ivElements = rootElement.getChildElements("iv");
if (keyElements.size() != 1) {
throw new ApnProxyConfigException("Wrong config for: iv ");
}
String iv = ivElements.get(0).getValue();
ApnProxyConfig.getConfig().setKey(_key.getBytes(Charset.forName("UTF-8")));
ApnProxyConfig.getConfig().setIv(iv.getBytes(Charset.forName("UTF-8")));
}
}
Elements keyStoreElements = rootElement.getChildElements("key-store");
if (keyStoreElements.size() == 1) {
Elements keyStorePathElements = keyStoreElements.get(0).getChildElements("path");
if (keyStorePathElements.size() == 1) {
ApnProxyConfig.getConfig().setKeyStorePath(keyStorePathElements.get(0).getValue());
}
Elements keyStorePasswordElements = keyStoreElements.get(0).getChildElements("password");
if (keyStorePasswordElements.size() == 1) {
ApnProxyConfig.getConfig().setKeyStroePassword(keyStorePasswordElements.get(0).getValue());
}
}
Elements trustStoreElements = rootElement.getChildElements("trust-store");
if (trustStoreElements.size() == 1) {
ApnProxyConfig.getConfig().setUseTrustStore(true);
Elements trustStorePathElements = trustStoreElements.get(0).getChildElements("path");
if (trustStorePathElements.size() == 1) {
ApnProxyConfig.getConfig().setTrustStorePath(trustStorePathElements.get(0).getValue());
}
Elements trustStorePasswordElements = trustStoreElements.get(0).getChildElements("password");
if (trustStorePasswordElements.size() == 1) {
ApnProxyConfig.getConfig().setTrustStorePassword(trustStorePasswordElements.get(0).getValue());
}
}
Elements portElements = rootElement.getChildElements("port");
if (portElements.size() == 1) {
try {
ApnProxyConfig.getConfig().setPort(Integer.parseInt(portElements.get(0).getValue()));
} catch (NumberFormatException nfe) {
throw new ApnProxyConfigException("Invalid format for: port", nfe);
}
}
Elements threadCountElements = rootElement.getChildElements("thread-count");
if (threadCountElements.size() == 1) {
Elements bossElements = threadCountElements.get(0).getChildElements("boss");
if (bossElements.size() == 1) {
try {
ApnProxyConfig.getConfig().setBossThreadCount(Integer.parseInt(bossElements.get(0).getValue()));
} catch (NumberFormatException nfe) {
throw new ApnProxyConfigException("Invalid format for: boss", nfe);
}
}
Elements workerElements = threadCountElements.get(0).getChildElements("worker");
if (workerElements.size() == 1) {
try {
ApnProxyConfig.getConfig().setWorkerThreadCount(Integer.parseInt(workerElements.get(0).getValue()));
} catch (NumberFormatException nfe) {
throw new ApnProxyConfigException("Invalid format for: worker", nfe);
}
}
}
Elements pacHostElements = rootElement.getChildElements("pac-host");
if (pacHostElements.size() == 1) {
ApnProxyConfig.getConfig().setPacHost(pacHostElements.get(0).getValue());
}
Elements useIpv6Elements = rootElement.getChildElements("use-ipv6");
if (useIpv6Elements.size() == 1) {
ApnProxyConfig.getConfig().setUseIpV6(Boolean.parseBoolean(useIpv6Elements.get(0).getValue()));
}
Elements localIpRulesElements = rootElement.getChildElements("local-ip-rules");
if (localIpRulesElements.size() == 1) {
Elements ruleElements = localIpRulesElements.get(0).getChildElements("rule");
for (int i = 0; i < ruleElements.size(); i++) {
ApnProxyLocalIpRule apnProxyLocalIpRule = new ApnProxyLocalIpRule();
Element ruleElement = ruleElements.get(i);
Elements localIpElements = ruleElement.getChildElements("local-ip");
if (localIpElements.size() != 1) {
throw new ApnProxyConfigException("Wrong config for: local-ip");
}
String localIp = localIpElements.get(0).getValue();
apnProxyLocalIpRule.setLocalIp(localIp);
Elements applyListElements = ruleElement.getChildElements("apply-list");
if (applyListElements.size() == 1) {
Elements originalHostElements = applyListElements.get(0).getChildElements("original-host");
List<String> originalHostList = new ArrayList<String>();
for (int j = 0; j < originalHostElements.size(); j++) {
String originalHost = originalHostElements.get(j).getValue();
originalHostList.add(originalHost);
}
apnProxyLocalIpRule.setOriginalHostList(originalHostList);
}
ApnProxyConfig.getConfig().addLocalIpRuleList(apnProxyLocalIpRule);
}
}
}
use of nu.xom.Elements in project apn-proxy by apn-proxy.
the class ApnProxyRemoteRulesConfigReader method realReadProcess.
@Override
protected void realReadProcess(Element rootElement) {
Element remoteRulesElement = rootElement;
Elements ruleElements = remoteRulesElement.getChildElements("rule");
for (int i = 0; i < ruleElements.size(); i++) {
Element ruleElement = ruleElements.get(i);
Elements remoteListenTypeElements = ruleElement.getChildElements("remote-listen-type");
if (remoteListenTypeElements.size() != 1) {
throw new ApnProxyConfigException("Wrong config for: remote-listen-type");
}
String _remoteListenType = remoteListenTypeElements.get(0).getValue();
ApnProxyListenType remoteListenType = ApnProxyListenType.fromString(_remoteListenType);
ApnProxyRemoteRule apnProxyRemoteRule = null;
if (remoteListenType == ApnProxyListenType.AES) {
apnProxyRemoteRule = new ApnProxyAESRemoteRule();
Elements remoteKeyElements = ruleElement.getChildElements("key");
if (remoteKeyElements.size() != 1) {
throw new ApnProxyConfigException("Wrong config for: key of AES remote");
}
String _remoteKey = remoteKeyElements.get(0).getValue();
Elements remoteIVElements = ruleElement.getChildElements("iv");
if (remoteKeyElements.size() != 1) {
throw new ApnProxyConfigException("Wrong config for: iv of AES remote");
}
String _remoteIV = remoteIVElements.get(0).getValue();
((ApnProxyAESRemoteRule) apnProxyRemoteRule).setKey(_remoteKey.getBytes(Charset.forName("UTF-8")));
((ApnProxyAESRemoteRule) apnProxyRemoteRule).setIv(_remoteIV.getBytes(Charset.forName("UTF-8")));
} else {
apnProxyRemoteRule = new ApnProxyRemoteRule();
}
apnProxyRemoteRule.setRemoteListenType(remoteListenType);
Elements remoteHostElements = ruleElement.getChildElements("remote-host");
if (remoteHostElements.size() != 1) {
throw new ApnProxyConfigException("Wrong config for: remote-host");
}
String remoteHost = remoteHostElements.get(0).getValue();
apnProxyRemoteRule.setRemoteHost(remoteHost);
Elements remotePortElements = ruleElement.getChildElements("remote-port");
if (remoteHostElements.size() != 1) {
throw new ApnProxyConfigException("Wrong config for: remote-port");
}
int remotePort = -1;
try {
remotePort = Integer.parseInt(remotePortElements.get(0).getValue());
} catch (NumberFormatException nfe) {
throw new ApnProxyConfigException("Invalid format for: remote-port", nfe);
}
apnProxyRemoteRule.setRemotePort(remotePort);
Elements proxyUserNameElements = ruleElement.getChildElements("proxy-username");
if (proxyUserNameElements.size() == 1) {
String proxyUserName = proxyUserNameElements.get(0).getValue();
apnProxyRemoteRule.setProxyUserName(proxyUserName);
}
Elements proxyPasswordElements = ruleElement.getChildElements("proxy-password");
if (proxyPasswordElements.size() == 1) {
String proxyPassword = proxyPasswordElements.get(0).getValue();
apnProxyRemoteRule.setProxyPassword(proxyPassword);
}
Elements applyListElements = ruleElement.getChildElements("apply-list");
if (applyListElements.size() == 1) {
Elements originalHostElements = applyListElements.get(0).getChildElements("original-host");
List<String> originalHostList = new ArrayList<String>();
for (int j = 0; j < originalHostElements.size(); j++) {
String originalHost = originalHostElements.get(j).getValue();
originalHostList.add(originalHost);
}
apnProxyRemoteRule.setOriginalHostList(originalHostList);
}
ApnProxyConfig.getConfig().addRemoteRule(apnProxyRemoteRule);
}
}
Aggregations