<commit_before>/*
 * Copyright 1999-2010 <NAME> (l.garulli--at--orientechnologies.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orientechnologies.orient.server.config;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.core.config.OGlobalConfiguration;

public class OServerConfigurationLoaderXml {
	private Class<? extends OServerConfiguration>	rootClass;
	private JAXBContext														context;
	private String																configurationText;
	private File																	file;

	public OServerConfigurationLoaderXml(Class<? extends OServerConfiguration> iRootClass, String iConfigurationText) {
		rootClass = iRootClass;
		configurationText = iConfigurationText;
	}

	public OServerConfigurationLoaderXml(Class<? extends OServerConfiguration> iRootClass, File iFile) {
		rootClass = iRootClass;
		file = iFile;
	}

	public OServerConfiguration load() throws IOException {
		try {
			context = JAXBContext.newInstance(rootClass);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			unmarshaller.setSchema(null);

			final OServerConfiguration obj;

			if (file != null) {
				if (file.exists())
					obj = rootClass.cast(unmarshaller.unmarshal(file));
				else
					return rootClass.getConstructor(OServerConfigurationLoaderXml.class, String.class).newInstance(this, file);
				obj.location = file.getAbsolutePath();
			} else {
				obj = rootClass.cast(unmarshaller.unmarshal(new StringReader(configurationText)));
				obj.location = "memory";
			}

			// AUTO CONFIGURE SYSTEM CONFIGURATION
			OGlobalConfiguration config;
			if (obj.properties != null)
				for (OServerEntryConfiguration prop : obj.properties) {
					try {
						config = OGlobalConfiguration.findByKey(prop.name);
						if (config != null) {
							config.setValue(prop.value);
						}
					} catch (Exception e) {
					}
				}

			return obj;
		} catch (Exception e) {
			// SYNTAX ERROR? PRINT AN EXAMPLE
			OLogManager.instance().error(this, "Invalid syntax. Below an example of how it should be:", e);

			try {
				context = JAXBContext.newInstance(rootClass);
				Marshaller marshaller = context.createMarshaller();
				marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
				Object example = rootClass.getConstructor(OServerConfigurationLoaderXml.class).newInstance(this);
				marshaller.marshal(example, System.out);
			} catch (Exception ex) {
			}

			throw new IOException(e);
		}
	}

	public void save(final OServerConfiguration iRootObject) throws IOException {
		try {
			context = JAXBContext.newInstance(rootClass);
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			marshaller.marshal(iRootObject, new FileWriter(file));
		} catch (JAXBException e) {
			throw new IOException(e);
		}
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}
}<commit_msg>Server: fixed bug on embedded starting of server reported in ML<commit_after>/*
 * Copyright 1999-2010 <NAME> (l.garulli--at--orientechnologies.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orientechnologies.orient.server.config;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.core.config.OGlobalConfiguration;

public class OServerConfigurationLoaderXml {
	private Class<? extends OServerConfiguration>	rootClass;
	private JAXBContext														context;
	private String																configurationText;
	private File																	file;

	public OServerConfigurationLoaderXml(Class<? extends OServerConfiguration> iRootClass, String iConfigurationText) {
		rootClass = iRootClass;
		configurationText = iConfigurationText;
	}

	public OServerConfigurationLoaderXml(Class<? extends OServerConfiguration> iRootClass, File iFile) {
		rootClass = iRootClass;
		file = iFile;
	}

	public OServerConfiguration load() throws IOException {
		try {
			context = JAXBContext.newInstance(rootClass);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			unmarshaller.setSchema(null);

			final OServerConfiguration obj;

			if (file != null) {
				if (file.exists())
					obj = rootClass.cast(unmarshaller.unmarshal(file));
				else
					return rootClass.getConstructor(OServerConfigurationLoaderXml.class).newInstance(this);
				obj.location = file.getAbsolutePath();
			} else {
				obj = rootClass.cast(unmarshaller.unmarshal(new StringReader(configurationText)));
				obj.location = "memory";
			}

			// AUTO CONFIGURE SYSTEM CONFIGURATION
			OGlobalConfiguration config;
			if (obj.properties != null)
				for (OServerEntryConfiguration prop : obj.properties) {
					try {
						config = OGlobalConfiguration.findByKey(prop.name);
						if (config != null) {
							config.setValue(prop.value);
						}
					} catch (Exception e) {
					}
				}

			return obj;
		} catch (Exception e) {
			// SYNTAX ERROR? PRINT AN EXAMPLE
			OLogManager.instance().error(this, "Invalid syntax. Below an example of how it should be:", e);

			try {
				context = JAXBContext.newInstance(rootClass);
				Marshaller marshaller = context.createMarshaller();
				marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
				Object example = rootClass.getConstructor(OServerConfigurationLoaderXml.class).newInstance(this);
				marshaller.marshal(example, System.out);
			} catch (Exception ex) {
			}

			throw new IOException(e);
		}
	}

	public void save(final OServerConfiguration iRootObject) throws IOException {
		if (file != null)
			try {
				context = JAXBContext.newInstance(rootClass);
				Marshaller marshaller = context.createMarshaller();
				marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
				marshaller.marshal(iRootObject, new FileWriter(file));
			} catch (JAXBException e) {
				throw new IOException(e);
			}
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}
}
{'fraction_non_alphanumeric': 0.09436469962785753, 'fraction_numerical': 0.003189792663476874, 'mean_word_length': 4.057123655913978, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6725770', 'n_tokens_mistral': 2541, 'n_tokens_neox': 2182, 'n_words': 567}