use of org.apache.xmlbeans.XmlCursor in project arctic-sea by 52North.
the class GetCapabilitiesResponseEncoder method renameContentsElementNames.
private void renameContentsElementNames(final Contents xbContents) {
for (final Offering offering : xbContents.getContents().getOfferingArray()) {
final XmlCursor cursor = offering.getAbstractOffering().newCursor();
cursor.setName(Sos2Constants.QN_OBSERVATION_OFFERING);
cursor.removeAttribute(W3CConstants.QN_XSI_TYPE);
if (cursor.toChild(Sos2Constants.QN_SOS_OBSERVED_AREA)) {
cursor.setName(Sos2Constants.QN_SOS_OBSERVED_AREA);
cursor.toParent();
}
if (cursor.toChild(Sos2Constants.QN_SOS_PHENOMENON_TIME)) {
cursor.setName(Sos2Constants.QN_SOS_PHENOMENON_TIME);
cursor.toParent();
}
if (cursor.toChild(Sos2Constants.QN_SOS_RESULT_TIME)) {
cursor.setName(Sos2Constants.QN_SOS_RESULT_TIME);
cursor.toParent();
}
if (cursor.toChild(Sos2Constants.QN_SOS_RESPONSE_FORMAT)) {
cursor.setName(Sos2Constants.QN_SOS_RESPONSE_FORMAT);
while (cursor.toNextSibling(Sos2Constants.QN_SOS_RESPONSE_FORMAT)) {
cursor.setName(Sos2Constants.QN_SOS_RESPONSE_FORMAT);
}
cursor.toParent();
}
if (cursor.toChild(Sos2Constants.QN_SOS_OBSERVATION_TYPE)) {
cursor.setName(Sos2Constants.QN_SOS_OBSERVATION_TYPE);
while (cursor.toNextSibling(Sos2Constants.QN_SOS_OBSERVATION_TYPE)) {
cursor.setName(Sos2Constants.QN_SOS_OBSERVATION_TYPE);
}
cursor.toParent();
}
if (cursor.toChild(Sos2Constants.QN_SOS_FEATURE_OF_INTEREST_TYPE)) {
cursor.setName(Sos2Constants.QN_SOS_FEATURE_OF_INTEREST_TYPE);
while (cursor.toNextSibling(Sos2Constants.QN_SOS_FEATURE_OF_INTEREST_TYPE)) {
cursor.setName(Sos2Constants.QN_SOS_FEATURE_OF_INTEREST_TYPE);
}
}
cursor.dispose();
}
}
use of org.apache.xmlbeans.XmlCursor in project arctic-sea by 52North.
the class SensorMLEncoderv101 method addAbstractProcessValues.
// TODO refactor/rename
private void addAbstractProcessValues(final AbstractProcessType abstractProcess, final AbstractProcess sosAbstractProcess) throws EncodingException {
if (sosAbstractProcess.isSetGmlID()) {
abstractProcess.setId(sosAbstractProcess.getGmlId());
}
if (sosAbstractProcess.isSetCapabilities()) {
final Capabilities[] existing = abstractProcess.getCapabilitiesArray();
final Set<String> names = Sets.newHashSetWithExpectedSize(existing.length);
for (final Capabilities element : existing) {
if (element.getName() != null) {
names.add(element.getName());
}
}
for (final SmlCapabilities sosCapability : sosAbstractProcess.getCapabilities()) {
final Capabilities c = createCapability(sosCapability);
// replace existing capability with the same name
if (names.contains(c.getName())) {
removeCapability(abstractProcess, c);
}
abstractProcess.addNewCapabilities().set(c);
}
}
// set description
if (sosAbstractProcess.isSetDescription() && !abstractProcess.isSetDescription()) {
abstractProcess.addNewDescription().setStringValue(sosAbstractProcess.getDescription());
}
if (sosAbstractProcess.isSetName() && CollectionHelper.isNullOrEmpty(abstractProcess.getNameArray())) {
// TODO check if override existing names
addNamesToAbstractProcess(abstractProcess, sosAbstractProcess.getNames());
}
// set identification
if (sosAbstractProcess.isSetIdentifications()) {
abstractProcess.setIdentificationArray(createIdentification(sosAbstractProcess.getIdentifications()));
}
// set classification
if (sosAbstractProcess.isSetClassifications()) {
abstractProcess.setClassificationArray(createClassification(sosAbstractProcess.getClassifications()));
}
// set characteristics
if (sosAbstractProcess.isSetCharacteristics()) {
abstractProcess.setCharacteristicsArray(createCharacteristics(sosAbstractProcess.getCharacteristics()));
}
// set documentation
if (sosAbstractProcess.isSetDocumentation() && CollectionHelper.isNullOrEmpty(abstractProcess.getDocumentationArray())) {
abstractProcess.setDocumentationArray(createDocumentationArray(sosAbstractProcess.getDocumentation()));
}
// process
if (sosAbstractProcess.isSetContact() && CollectionHelper.isNullOrEmpty(abstractProcess.getContactArray())) {
ContactList contactList = createContactList(sosAbstractProcess.getContact());
if (contactList != null && contactList.getMemberArray().length > 0) {
abstractProcess.addNewContact().setContactList(contactList);
}
}
// set keywords
if (sosAbstractProcess.isSetKeywords()) {
final List<String> keywords = sosAbstractProcess.getKeywords();
final int length = abstractProcess.getKeywordsArray().length;
for (int i = 0; i < length; ++i) {
abstractProcess.removeKeywords(i);
}
abstractProcess.addNewKeywords().addNewKeywordList().setKeywordArray(keywords.toArray(new String[keywords.size()]));
}
if (sosAbstractProcess.isSetValidTime()) {
if (abstractProcess.isSetValidTime()) {
// remove existing validTime element
final XmlCursor newCursor = abstractProcess.getValidTime().newCursor();
newCursor.removeXml();
newCursor.dispose();
}
final Time time = sosAbstractProcess.getMergedValidTime();
final XmlObject xbtime = encodeObjectToXml(GmlConstants.NS_GML, time);
if (time instanceof TimeInstant) {
abstractProcess.addNewValidTime().addNewTimeInstant().set(xbtime);
} else if (time instanceof TimePeriod) {
abstractProcess.addNewValidTime().addNewTimePeriod().set(xbtime);
}
}
}
use of org.apache.xmlbeans.XmlCursor in project arctic-sea by 52North.
the class SweCommonEncoderv101 method createTimeGeometricPrimitivePropertyType.
private XmlObject createTimeGeometricPrimitivePropertyType(TimePeriod timePeriod) throws EncodingException {
TimeGeometricPrimitivePropertyType xbTimeGeometricPrimitiveProperty = TimeGeometricPrimitivePropertyType.Factory.newInstance(getXmlOptions());
if (timePeriod.isSetStart() && timePeriod.isSetEnd()) {
xbTimeGeometricPrimitiveProperty.addNewTimeGeometricPrimitive().set(encodeObjectToXml(GmlConstants.NS_GML, timePeriod));
}
// TODO check GML 311 rename nodename of geometric primitive to
// gml:timePeriod
XmlCursor timeCursor = xbTimeGeometricPrimitiveProperty.newCursor();
boolean hasTimePrimitive = timeCursor.toChild(new QName(GmlConstants.NS_GML, GmlConstants.EN_ABSTRACT_TIME_GEOM_PRIM));
if (hasTimePrimitive) {
timeCursor.setName(new QName(GmlConstants.NS_GML, GmlConstants.EN_TIME_PERIOD));
}
timeCursor.dispose();
return xbTimeGeometricPrimitiveProperty;
}
use of org.apache.xmlbeans.XmlCursor in project arctic-sea by 52North.
the class AbstractOmV20XmlStreamWriter method writeResult.
/**
* write om:result to stream
*
* @throws XMLStreamException
* If an error occurs when writing to stream
* @throws EncodingException
* If an error occurs when creating elements to be written
*/
protected void writeResult() throws XMLStreamException, EncodingException {
OmObservation observation = getElement();
if (observation.getValue() instanceof AbstractObservationValue<?>) {
((AbstractObservationValue<?>) observation.getValue()).setValuesForResultEncoding(observation);
}
XmlObject createResult = (XmlObject) getEncoder(getEncodeNamespace().orElse(OmConstants.NS_OM_2), observation.getValue()).encode(observation.getValue());
if (createResult != null) {
if (createResult.xmlText().contains(XML_FRAGMENT)) {
XmlObject set = OMObservationType.Factory.newInstance(getXmlOptions()).addNewResult().set(createResult);
writeXmlObject(set, OmConstants.QN_OM_20_RESULT);
} else {
if (checkResult(createResult)) {
QName name = createResult.schemaType().getName();
String prefix = name.getPrefix();
if (Strings.isNullOrEmpty(prefix)) {
XmlCursor newCursor = createResult.newCursor();
prefix = newCursor.prefixForNamespace(name.getNamespaceURI());
newCursor.setAttributeText(W3CConstants.QN_XSI_TYPE, prefix + ":" + name.getLocalPart());
newCursor.dispose();
}
writeXmlObject(createResult, OmConstants.QN_OM_20_RESULT);
} else {
start(OmConstants.QN_OM_20_RESULT);
writeXmlObject(createResult, OmConstants.QN_OM_20_RESULT);
end(OmConstants.QN_OM_20_RESULT);
}
}
} else {
empty(OmConstants.QN_OM_20_RESULT);
}
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method insertChild.
/**
*
* @param curs
* @param xmlToInsert
*/
private void insertChild(XmlCursor curs, Object xmlToInsert) {
if (xmlToInsert == null || xmlToInsert instanceof Undefined) {
// Do nothing
} else if (xmlToInsert instanceof XmlCursor) {
moveSrcToDest((XmlCursor) xmlToInsert, curs, true);
} else if (xmlToInsert instanceof XML) {
XML xmlValue = (XML) xmlToInsert;
// If it's an attribute, then change to text node
if (xmlValue.tokenType() == XmlCursor.TokenType.ATTR) {
insertChild(curs, xmlValue.toString());
} else {
XmlCursor cursToInsert = ((XML) xmlToInsert).newCursor();
moveSrcToDest(cursToInsert, curs, true);
cursToInsert.dispose();
}
} else if (xmlToInsert instanceof XMLList) {
XMLList list = (XMLList) xmlToInsert;
for (int i = 0; i < list.length(); i++) {
insertChild(curs, list.item(i));
}
} else {
// Convert to string and make XML out of it
String xmlStr = ScriptRuntime.toString(xmlToInsert);
// Create an empty document.
XmlObject xo = XmlObject.Factory.newInstance();
XmlCursor sourceCurs = xo.newCursor();
sourceCurs.toNextToken();
// To hold the text.
sourceCurs.insertChars(xmlStr);
sourceCurs.toPrevToken();
// Call us again with the cursor.
moveSrcToDest(sourceCurs, curs, true);
}
}
Aggregations