com.crabshue.commons.xml.processinginstructions.ProcessingInstructionsUtils Maven / Gradle / Ivy
package com.crabshue.commons.xml.processinginstructions;
import java.util.Collection;
import org.apache.commons.lang3.Validate;
import org.w3c.dom.Document;
import com.crabshue.commons.exceptions.ApplicationException;
import com.crabshue.commons.xml.XmlPrintUtils;
import com.crabshue.commons.xml.exceptions.XmlErrorContext;
import com.crabshue.commons.xml.exceptions.XmlErrorType;
/**
* Utility class for processing instructions operations.
*/
public class ProcessingInstructionsUtils {
protected ProcessingInstructionsUtils() {
}
/**
* Retrieve the first line with a processing instruction in an {@link Document XML document}.
*
* @param document the document
* @return the first line with a processing instruction.
*/
public static String retrieveFirstNonProcessingInstructionLine(final Document document) {
Validate.notNull(document);
final Collection lines = XmlPrintUtils.stringify(document, new XmlPrintUtils.Options().omitXmlDeclaration(true));
return retrieveFirstNonProcessingInstructionLine(lines);
}
/**
* Retrieve among a collection of {@link String} the first one that is not a processing instruction.
*
* @param lines the lines.
* @return the matching line.
*/
public static String retrieveFirstNonProcessingInstructionLine(final Collection lines) {
Validate.notNull(lines);
for (String line : lines) {
if (line.matches(ProcessingInstructionsConstants.PROCESSING_INSTRUCTION_START)) {
continue;
}
return line;
}
throw new ApplicationException(XmlErrorType.XML_NO_NON_PI_LINE, "Found no non-processing-instruction line")
.addContextValue(XmlErrorContext.LINES, lines);
}
}