com.crabshue.commons.xml.processinginstructions.ProcessingInstructionsUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-xml Show documentation
Show all versions of commons-xml Show documentation
Library for XML documents operations.
package com.crabshue.commons.xml.processinginstructions;
import java.util.Collection;
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;
import lombok.NonNull;
/**
* 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(@NonNull final Document document) {
final Collection lines = XmlPrintUtils.stringify(document, new XmlPrintUtils.Options().setOmitXmlDeclaration(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(@NonNull final Collection 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);
}
}