use of org.xml.sax.ErrorHandler in project tdi-studio-se by Talend.
the class ImportProjectSettings method updateProjectSettings.
public void updateProjectSettings() throws ParserConfigurationException, SAXException, IOException {
if (this.path == null) {
return;
}
File file = new File(path);
org.talend.core.model.properties.Project project = pro.getEmfProject();
final DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
DocumentBuilder analyseur = fabrique.newDocumentBuilder();
analyseur.setErrorHandler(new ErrorHandler() {
@Override
public void error(final SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void fatalError(final SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void warning(final SAXParseException exception) throws SAXException {
throw exception;
}
});
final Document document = analyseur.parse(file);
// PTODO, if need, get the version from the imported file.
// NodeList exportParametersNodes = document.getElementsByTagName("exportParameters");
// String importStudioVersion=null;
//$NON-NLS-1$
final NodeList nodes = document.getElementsByTagName("exportParameter");
List addedComponentSetting = new ArrayList();
List technical = project.getTechnicalStatus();
List documentation = project.getDocumentationStatus();
technical.clear();
documentation.clear();
for (int i = 0; i < nodes.getLength(); i++) {
final Node node = nodes.item(i);
final NamedNodeMap attrMap = node.getAttributes();
//$NON-NLS-1$
final Node typeAttr = attrMap.getNamedItem("type");
if ("technicalStatus".equals(typeAttr.getTextContent())) {
//$NON-NLS-1$
//$NON-NLS-1$
updateStatus(node, attrMap, technical, "technicalStatus");
} else if ("documentationStatus".equals(typeAttr.getTextContent())) {
//$NON-NLS-1$
//$NON-NLS-1$
updateStatus(node, attrMap, documentation, "documentationStatus");
} else if ("security".equals(typeAttr.getTextContent())) {
//$NON-NLS-1$
project.isHidePassword();
project.setHidePassword(Boolean.valueOf(node.getTextContent()));
} else if ("statAndLogs".equals(typeAttr.getTextContent())) {
//$NON-NLS-1$
if (project.getStatAndLogsSettings() == null) {
TalendFileFactory talendF = TalendFileFactory.eINSTANCE;
StatAndLogsSettings stats = PropertiesFactory.eINSTANCE.createStatAndLogsSettings();
project.setStatAndLogsSettings(stats);
stats.setParameters(talendF.createParametersType());
}
List statAndLogs = project.getStatAndLogsSettings().getParameters().getElementParameter();
updateParameters(node, attrMap, statAndLogs);
} else if ("implicitContext".equals(typeAttr.getTextContent())) {
//$NON-NLS-1$
if (project.getImplicitContextSettings() == null) {
TalendFileFactory talendF = TalendFileFactory.eINSTANCE;
ImplicitContextSettings implicit = PropertiesFactory.eINSTANCE.createImplicitContextSettings();
project.setImplicitContextSettings(implicit);
implicit.setParameters(talendF.createParametersType());
}
List implicitContexts = project.getImplicitContextSettings().getParameters().getElementParameter();
updateParameters(node, attrMap, implicitContexts);
} else if ("palette".equals(typeAttr.getTextContent())) {
//$NON-NLS-1$
List componentSettings = project.getComponentsSettings();
boolean existed = false;
//$NON-NLS-1$
String name = attrMap.getNamedItem("name").getTextContent();
//$NON-NLS-1$
final Node familyAttr = attrMap.getNamedItem("family");
Boolean hide = Boolean.valueOf(node.getTextContent());
for (Object obj : componentSettings) {
ComponentSetting setting = (ComponentSetting) obj;
if (setting.getName().equals(name)) {
if (familyAttr != null && familyAttr.getTextContent().equals(setting.getFamily())) {
existed = true;
setting.setHidden(hide);
}
}
}
if (!existed && familyAttr != null) {
ComponentSetting setting = PropertiesFactory.eINSTANCE.createComponentSetting();
setting.setFamily(familyAttr.getTextContent());
setting.setName(name);
setting.setHidden(hide);
addedComponentSetting.add(setting);
}
}
}
project.getComponentsSettings().addAll(addedComponentSetting);
}
use of org.xml.sax.ErrorHandler in project tdi-studio-se by Talend.
the class AutoConvertTypesUtils method save.
public static boolean save(List<AutoConversionType> beans, File file) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
OutputStreamWriter output = null;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new ErrorHandler() {
@Override
public void error(final SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void fatalError(final SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void warning(final SAXParseException exception) throws SAXException {
throw exception;
}
});
Document document = builder.newDocument();
//document.setXmlVersion("1.0");//$NON-NLS-1$
//$NON-NLS-1$
Element root = document.createElement("mapping");
document.appendChild(root);
for (int i = 0; i < beans.size(); i++) {
AutoConversionType bean = beans.get(i);
//$NON-NLS-1$
Element typeNode = document.createElement("conversionType");
//$NON-NLS-1$
typeNode.setAttribute("source", bean.getSourceDataType());
//$NON-NLS-1$
typeNode.setAttribute("target", bean.getTargetDataType());
//$NON-NLS-1$
typeNode.setAttribute("function", bean.getConversionFunction());
root.appendChild(typeNode);
}
// save into file
if (document != null) {
XMLSerializer serializer = new XMLSerializer();
OutputFormat outputFormat = new OutputFormat();
outputFormat.setIndenting(true);
serializer.setOutputFormat(outputFormat);
output = new OutputStreamWriter(new FileOutputStream(file));
serializer.setOutputCharStream(output);
serializer.serialize(document);
// update
beanList = new ArrayList<>();
beanList.addAll(beans);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} finally {
if (output != null) {
output.close();
}
}
return true;
}
use of org.xml.sax.ErrorHandler in project JGroups by belaban.
the class XmlConfigurator method parse.
protected static XmlConfigurator parse(InputStream stream, Boolean validate) throws java.io.IOException {
/**
* CAUTION: crappy code ahead ! I (bela) am not an XML expert, so the code below is pretty
* amateurish... But it seems to work, and it is executed only on startup, so no perf loss
* on the critical path. If somebody wants to improve this, please be my guest.
*/
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
boolean validation = false;
String tmp = Util.getProperty(new String[] { Global.XML_VALIDATION }, null, null, null);
if (tmp != null) {
validation = Boolean.valueOf(tmp);
} else if (validate != null) {
validation = validate;
}
factory.setValidating(validation);
factory.setNamespaceAware(validation);
if (validation) {
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
}
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver((publicId, systemId) -> {
if (systemId != null && systemId.startsWith("http://www.jgroups.org/schema/JGroups-")) {
String schemaName = systemId.substring("http://www.jgroups.org/".length());
InputStream schemaIs = getAsInputStreamFromClassLoader(schemaName);
if (schemaIs == null) {
throw new IOException("Schema not found from classloader: " + schemaName);
}
InputSource source = new InputSource(schemaIs);
source.setPublicId(publicId);
source.setSystemId(systemId);
return source;
}
return null;
});
// Use AtomicReference to allow make variable final, not for atomicity
// We store only last exception
final AtomicReference<SAXParseException> exceptionRef = new AtomicReference<>();
builder.setErrorHandler(new ErrorHandler() {
public void warning(SAXParseException exception) throws SAXException {
log.warn(Util.getMessage("ParseFailure"), exception);
}
public void fatalError(SAXParseException exception) throws SAXException {
exceptionRef.set(exception);
}
public void error(SAXParseException exception) throws SAXException {
exceptionRef.set(exception);
}
});
Document document = builder.parse(stream);
if (exceptionRef.get() != null) {
throw exceptionRef.get();
}
// The root element of the document should be the "config" element,
// but the parser(Element) method checks this so a check is not
// needed here.
Element configElement = document.getDocumentElement();
return parse(configElement);
} catch (Exception x) {
throw new IOException(String.format(Util.getMessage("ParseError"), x.getLocalizedMessage()));
}
}
use of org.xml.sax.ErrorHandler in project liferay-ide by liferay.
the class KaleoUtil method createJSONTitleMap.
public static String createJSONTitleMap(String title, String portalLocale) throws JSONException {
JSONObject jsonTitleMap = new JSONObject();
try {
ErrorHandler errorHandle = new ErrorHandler() {
public void error(SAXParseException exception) throws SAXException {
}
public void fatalError(SAXParseException exception) throws SAXException {
}
public void warning(SAXParseException exception) throws SAXException {
}
};
Document doc = FileUtil.readXML(new ByteArrayInputStream(title.getBytes()), null, errorHandle);
String defaultLocale = doc.getDocumentElement().getAttribute("default-locale");
NodeList titles = doc.getElementsByTagName("Title");
for (int i = 0; i < titles.getLength(); i++) {
Node titleNode = titles.item(i);
String titleValue = titleNode.getTextContent();
NamedNodeMap nameNodeMap = titleNode.getAttributes();
Node node = nameNodeMap.getNamedItem("language-id");
String languageId = node.getNodeValue();
if (languageId.equals(defaultLocale)) {
jsonTitleMap.put(languageId, titleValue);
break;
}
}
} catch (Exception e) {
jsonTitleMap.put(portalLocale, title);
}
return jsonTitleMap.toString();
}
use of org.xml.sax.ErrorHandler in project freemarker by apache.
the class DOMConvenienceStaticsTest method toDOM.
private Document toDOM(String content) throws SAXException, IOException, ParserConfigurationException {
DocumentBuilder builder = NodeModel.getDocumentBuilderFactory().newDocumentBuilder();
ErrorHandler errorHandler = NodeModel.getErrorHandler();
if (errorHandler != null)
builder.setErrorHandler(errorHandler);
return builder.parse(toInputSource(content));
}
Aggregations