use of com.google.cloud.language.v1.Document in project ldapchai by ldapchai.
the class NmasResponseSet method parseNmasPolicyXML.
static List<Challenge> parseNmasPolicyXML(final String str, final Locale locale) throws IOException, JDOMException {
final List<Challenge> returnList = new ArrayList<Challenge>();
final Reader xmlreader = new StringReader(str);
final SAXBuilder builder = new SAXBuilder();
final Document doc = builder.build(xmlreader);
final boolean required = doc.getRootElement().getName().equals("RequiredQuestions");
for (final Iterator questionIterator = doc.getDescendants(new ElementFilter("Question")); questionIterator.hasNext(); ) {
final Element loopQ = (Element) questionIterator.next();
final int maxLength = StringHelper.convertStrToInt(loopQ.getAttributeValue("MaxLength"), 255);
final int minLength = StringHelper.convertStrToInt(loopQ.getAttributeValue("MinLength"), 1);
final String challengeText = readDisplayString(loopQ, locale);
final Challenge challenge = new ChaiChallenge(required, challengeText, minLength, maxLength, true, 0, false);
returnList.add(challenge);
}
for (Iterator iter = doc.getDescendants(new ElementFilter("UserDefined")); iter.hasNext(); ) {
final Element loopQ = (Element) iter.next();
final int maxLength = StringHelper.convertStrToInt(loopQ.getAttributeValue("MaxLength"), 255);
final int minLength = StringHelper.convertStrToInt(loopQ.getAttributeValue("MinLength"), 1);
final Challenge challenge = new ChaiChallenge(required, null, minLength, maxLength, false, 0, false);
returnList.add(challenge);
}
return returnList;
}
use of com.google.cloud.language.v1.Document in project jspwiki by apache.
the class XmlUtil method parse.
/**
* Parses the given stream and returns the requested nodes. If there's an error accessing or parsing the stream, an
* empty list is returned.
*
* @param xmlStream stream to parse.
* @param requestedNodes requestd nodes on the xml stream.
* @return the requested nodes of the XML stream.
*/
public static List<Element> parse(InputStream xmlStream, String requestedNodes) {
if (xmlStream != null && StringUtils.isNotEmpty(requestedNodes)) {
SAXBuilder builder = new SAXBuilder();
try {
Document doc = builder.build(xmlStream);
XPathFactory xpfac = XPathFactory.instance();
XPathExpression<Element> xp = xpfac.compile(requestedNodes, Filters.element());
return xp.evaluate(doc);
} catch (IOException ioe) {
log.error("Couldn't load all " + xmlStream + " resources", ioe);
} catch (JDOMException jdome) {
log.error("error parsing " + xmlStream + " resources", jdome);
}
}
return Collections.<Element>emptyList();
}
use of com.google.cloud.language.v1.Document in project pwm by pwm-project.
the class StoredConfigurationImpl method fromXml.
public static StoredConfigurationImpl fromXml(final InputStream xmlData) throws PwmUnrecoverableException {
final Instant startTime = Instant.now();
// validateXmlSchema(xmlData);
final Document inputDocument = XmlUtil.parseXml(xmlData);
final StoredConfigurationImpl newConfiguration = StoredConfigurationImpl.newStoredConfiguration();
try {
newConfiguration.document = inputDocument;
// verify create time;
newConfiguration.createTime();
ConfigurationCleaner.cleanup(newConfiguration);
} catch (Exception e) {
final String errorMsg = "error reading configuration file format, error=" + e.getMessage();
final ErrorInformation errorInfo = new ErrorInformation(PwmError.CONFIG_FORMAT_ERROR, null, new String[] { errorMsg });
throw new PwmUnrecoverableException(errorInfo);
}
checkIfXmlRequiresUpdate(newConfiguration);
LOGGER.debug("successfully loaded configuration (" + TimeDuration.compactFromCurrent(startTime) + ")");
return newConfiguration;
}
use of com.google.cloud.language.v1.Document in project pwm by pwm-project.
the class XmlUtil method parseXml.
public static Document parseXml(final Reader inputStream) throws PwmUnrecoverableException {
final SAXBuilder builder = getBuilder();
final Document inputDocument;
try {
inputDocument = builder.build(inputStream);
} catch (Exception e) {
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.CONFIG_FORMAT_ERROR, null, new String[] { "error parsing xml data: " + e.getMessage() }));
}
return inputDocument;
}
use of com.google.cloud.language.v1.Document in project pwm by pwm-project.
the class PwmSettingXml method readXml.
private static Document readXml() {
final Document docRefCopy = xmlDocCache;
if (docRefCopy == null) {
// validateXmlSchema();
final InputStream inputStream = PwmSetting.class.getClassLoader().getResourceAsStream(SETTING_XML_FILENAME);
final SAXBuilder builder = new SAXBuilder();
try {
final Document newDoc = builder.build(inputStream);
xmlDocCache = newDoc;
// clear cached dom after 30 seconds.
final Thread t = new Thread("PwmSettingXml static cache clear thread") {
@Override
public void run() {
JavaHelper.pause(30 * 1000);
xmlDocCache = null;
}
};
t.setDaemon(true);
t.start();
return newDoc;
} catch (JDOMException e) {
throw new IllegalStateException("error parsing " + SETTING_XML_FILENAME + ": " + e.getMessage());
} catch (IOException e) {
throw new IllegalStateException("unable to load " + SETTING_XML_FILENAME + ": " + e.getMessage());
}
}
return docRefCopy;
}
Aggregations