Skip to content

Commit

Permalink
Merge branch 'parser' of github.com:Evolveum/midpoint into parser
Browse files Browse the repository at this point in the history
  • Loading branch information
katkav committed Apr 9, 2014
2 parents 45de311 + 45ed00c commit df1dc0b
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class PrismContext {
private DomParser parserDom;
private JaxbDomHack jaxbDomHack;

//region Standard overhead
private PrismContext() {
// empty
}
Expand Down Expand Up @@ -183,6 +184,7 @@ private Parser getParserNotNull(String language) {
}
return parser;
}
//endregion

//region Parsing Prism objects
/**
Expand Down Expand Up @@ -299,6 +301,21 @@ public <T> T parseAtomicValue(String dataString, QName typeName, String language

//endregion

//region Parsing anything (without knowing the definition up-front)
/**
* Parses (almost) anything: either an item with a definition, or an atomic (i.e. property-like) value.
* Does not care for schemaless items!
*
* @param node
* @return either Item or an unmarshalled bean value
* @throws SchemaException
*/
public Object parseAnyData(String dataString, String language) throws SchemaException {
XNode xnode = parseToXNode(dataString, language);
return xnodeProcessor.parseAnyData(xnode);
}
//endregion

//region Parsing to XNode
private XNode parseToXNode(String dataString, String language) throws SchemaException {
Parser parser = getParserNotNull(language);
Expand Down Expand Up @@ -399,6 +416,12 @@ public <C extends Containerable> String serializeContainerValueToString(PrismCon
return parser.serializeToString(xroot);
}

public String serializeAnyData(Object object, String language) throws SchemaException {
Parser parser = getParserNotNull(language);
RootXNode xnode = xnodeProcessor.serializeAnyData(object);
return parser.serializeToString(xnode);
}

// public <T> String serializeAtomicValues(QName elementName, String language, T... values) throws SchemaException {
// Parser parser = getParserNotNull(language);
// PrismPropertyDefinition<T> definition = schemaRegistry.findPropertyDefinitionByElementName(elementName);
Expand Down Expand Up @@ -445,29 +468,4 @@ public RawType toRawType(Item item) throws SchemaException {
return new RawType(rootXNode);
}

/**
* Method used to marshal objects to xml in debug messages.
* @param object
* @return xml as string
*/
// public String silentMarshalObject(Object object, Trace logger) {
// String xml = null;
// try {
// QName fakeQName=new QName(PrismConstants.NS_PREFIX + "debug", "debugPrintObject");
// if (object instanceof Objectable) {
// xml = prismDomProcessor.serializeObjectToString(((Objectable) object).asPrismObject());
// } else if (object instanceof Containerable) {
// Element fakeParent = DOMUtil.createElement(DOMUtil.getDocument(), fakeQName);
// xml = prismDomProcessor.serializeObjectToString(((Containerable) object).asPrismContainerValue(),
// fakeParent);
// } else {
// xml = prismJaxbProcessor.marshalElementToString(new JAXBElement<Object>(fakeQName, Object.class, object));
// }
// } catch (Exception ex) {
// Trace log = logger != null ? logger : LOGGER;
// LoggingUtils.logException(log, "Couldn't marshal element to string {}", ex, object);
// }
// return xml;
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,9 @@ public <T> void encrypt(ProtectedData<T> protectedData) throws EncryptionExcepti
@Override
public String decryptString(ProtectedStringType protectedString) throws EncryptionException {
try {
decrypt(protectedString);
return protectedString.getClearValue();
ProtectedStringType clone = protectedString.clone();
decrypt(clone);
return clone.getClearValue();
} catch (SchemaException ex){
throw new EncryptionException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
*/
package com.evolveum.midpoint.prism.parser;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;

import javax.xml.namespace.QName;

import com.evolveum.midpoint.prism.PrismConstants;
import com.evolveum.prism.xml.ns._public.query_2.SearchFilterType;

import com.evolveum.prism.xml.ns._public.types_2.ObjectReferenceType;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -58,14 +59,11 @@
import com.evolveum.midpoint.prism.xnode.XNode;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.Transformer;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.prism.xml.ns._public.types_2.EncryptedDataType;
import com.evolveum.prism.xml.ns._public.types_2.ItemPathType;
import com.evolveum.prism.xml.ns._public.types_2.PolyStringType;
import com.evolveum.prism.xml.ns._public.types_2.ProtectedByteArrayType;
import com.evolveum.prism.xml.ns._public.types_2.ProtectedDataType;
import com.evolveum.prism.xml.ns._public.types_2.ProtectedStringType;
import com.evolveum.prism.xml.ns._public.types_2.SchemaDefinitionType;

Expand Down Expand Up @@ -222,7 +220,10 @@ public <C extends Containerable> PrismContainer<C> parseContainer(XNode xnode, P

private <C extends Containerable> PrismContainer<C> parseContainerInternal(XNode xnode, QName elementName,
PrismContainerDefinition<C> containerDef) throws SchemaException {
if (xnode instanceof MapXNode) {
if (xnode instanceof RootXNode) {
RootXNode rootXnode = (RootXNode) xnode;
return parseContainerInternal(rootXnode.getSubnode(), rootXnode.getRootElementName(), containerDef); // ignoring elementName from parameters (probably set to be root element name)
} else if (xnode instanceof MapXNode) {
return parseContainerFromMapInternal((MapXNode) xnode, elementName, containerDef, null);
} else if (xnode instanceof ListXNode) {
PrismContainer<C> container = containerDef.instantiate(elementName);
Expand Down Expand Up @@ -390,7 +391,9 @@ public <T> T parsePrismPropertyRealValue(XNode xnode, PrismPropertyDefinition<T>
* (The method name is a bit misleading.)
*/
public <T> T parseAtomicValue(XNode xnode, QName typeName) throws SchemaException {
if (xnode instanceof PrimitiveXNode<?>) {
if (xnode instanceof RootXNode) {
return parseAtomicValue(((RootXNode) xnode).getSubnode(), typeName);
} else if (xnode instanceof PrimitiveXNode<?>) {
return parsePrismPropertyRealValueFromPrimitive((PrimitiveXNode<T>)xnode, typeName);
} else if (xnode instanceof MapXNode) {
return parsePrismPropertyRealValueFromMap((MapXNode)xnode, typeName, null);
Expand Down Expand Up @@ -901,6 +904,7 @@ private <T extends Containerable> ItemDefinition resolveGlobalItemDefinition(
}
//endregion

//region Parsing general items
/**
* This gets definition of an unspecified type. It has to find the right
* method to call. Value elements have the same element name. They may be
Expand All @@ -912,7 +916,7 @@ public <V extends PrismValue> Item<V> parseItem(XNode xnode, QName itemName, Ite
// Assume property in a container with runtime definition
return (Item<V>) parsePrismPropertyRaw(xnode, itemName);
}
if (itemDef instanceof PrismContainerDefinition) {
if (itemDef instanceof PrismContainerDefinition) {
return (Item<V>) parseContainerInternal(xnode, itemName, (PrismContainerDefinition<?>) itemDef);
} else if (itemDef instanceof PrismPropertyDefinition) {
return (Item<V>) parsePrismProperty(xnode, itemName, (PrismPropertyDefinition) itemDef);
Expand All @@ -923,7 +927,63 @@ public <V extends PrismValue> Item<V> parseItem(XNode xnode, QName itemName, Ite
throw new IllegalArgumentException("Attempt to parse unknown definition type " + itemDef.getClass().getName());
}
}


/**
* Parses (almost) anything: either an item with a definition, or an atomic (i.e. property-like) value.
* Does not care for schemaless items!
*
* @param node
* @return either Item or a unmarshalled bean value
* @throws SchemaException
*/
public Object parseAnyData(XNode node) throws SchemaException {
// is the type name explicitly specified?
QName typeName = getExplicitType(node);
if (typeName != null) {
ItemDefinition itemDefinition = getSchemaRegistry().findItemDefinitionByType(typeName);
if (itemDefinition != null) {
return parseItem(node, getElementName(node, itemDefinition), itemDefinition);
} else {
return parseAtomicValue(node, typeName);
}
} else {
// if type name is not known, we have to derive it from the element name
if (!(node instanceof RootXNode)) {
throw new SchemaException("Couldn't parse general object with no type name and no root element name: " + node);
}
QName elementName = ((RootXNode) node).getRootElementName();
ItemDefinition itemDefinition = getSchemaRegistry().findItemDefinitionByElementName(elementName);
if (itemDefinition == null) {
throw new SchemaException("Couldn't parse general object with no type name and unknown element name: " + elementName);
}
return parseItem(((RootXNode) node).getSubnode(), elementName, itemDefinition);
}
}

private QName getElementName(XNode node, ItemDefinition itemDefinition) {
if (node instanceof RootXNode) {
return ((RootXNode) node).getRootElementName();
} else if (itemDefinition != null) {
return itemDefinition.getName();
} else {
return new QName(null, "object");
}
}

private QName getExplicitType(XNode node) {
if (node.getTypeQName() != null) {
return node.getTypeQName();
} else if (node instanceof RootXNode) {
RootXNode rootXNode = (RootXNode) node;
if (rootXNode.getSubnode() != null && rootXNode.getSubnode().getTypeQName() != null) {
return rootXNode.getSubnode().getTypeQName();
}
}
return null;
}

//endregion

//region Serialization
// --------------------------
// -- SERIALIZATION
Expand Down Expand Up @@ -978,5 +1038,13 @@ public <V extends PrismValue> RootXNode serializeItemAsRoot(Item<V> item) throws
public XNodeSerializer createSerializer() {
return new XNodeSerializer(PrismUtil.getBeanConverter(prismContext));
}

public RootXNode serializeAnyData(Object object) throws SchemaException {
if (object instanceof Item) {
return serializeItemAsRoot((Item) object);
} else {
return new RootXNode(new QName(null, "value"), prismContext.getBeanConverter().marshall(object));
}
}
//endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ private RootXNode serializeItemValueAsRootInternal(PrismValue value, QName eleme
}

public <V extends PrismValue> RootXNode serializeItemAsRoot(Item<V> item) throws SchemaException {
Validate.notNull(item.getDefinition(), "Item without a definition");
XNode valueNode = serializeItem(item);
return new RootXNode(item.getDefinition().getName(), valueNode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.xml.bind.annotation.XmlType;
import javax.xml.namespace.QName;

import com.evolveum.midpoint.prism.util.CloneUtil;
import org.apache.xml.security.exceptions.Base64DecodingException;
import org.apache.xml.security.utils.Base64;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -296,24 +297,31 @@ public String toString() {
}

protected void cloneTo(ProtectedDataType<T> cloned) {
for (Object o : getContent()) {
if (o instanceof JAXBElement<?>) {
JAXBElement<?> je = (JAXBElement) o;
Object v = je.getValue();
if (v instanceof EncryptedDataType) {
EncryptedDataType edt = (EncryptedDataType) v;
cloned.addContent(new JAXBElement<EncryptedDataType>(je.getName(), (Class) je.getDeclaredType(), edt.clone()));
} else {
throw new IllegalStateException("Unknown JAXB element "+je+ " in ProtectedDataType");
}
} else if (o instanceof Element) {
cloned.addContent(((Element) o).cloneNode(true));
} else if (o instanceof String) {
cloned.addContent(o); // will this work?
} else {
throw new IllegalStateException("Unknown object of type "+o.getClass()+ " in ProtectedDataType");
}
}
cloned.clearValue = CloneUtil.clone(clearValue);
cloned.encryptedDataType = CloneUtil.clone(encryptedDataType);

// content is virtual, there is no point in copying it

// for (Object o : getContent()) {
// if (o instanceof JAXBElement<?>) {
// JAXBElement<?> je = (JAXBElement) o;
// Object v = je.getValue();
// if (v == null) {
// // do nothing
// } else if (v instanceof EncryptedDataType) {
// EncryptedDataType edt = (EncryptedDataType) v;
// cloned.addContent(new JAXBElement<EncryptedDataType>(je.getName(), (Class) je.getDeclaredType(), edt.clone()));
// } else {
// throw new IllegalStateException("Unknown JAXB element "+je.getName()+" ("+je.getValue().getClass()+") in ProtectedDataType");
// }
// } else if (o instanceof Element) {
// cloned.addContent(((Element) o).cloneNode(true));
// } else if (o instanceof String) {
// cloned.addContent(o); // will this work?
// } else {
// throw new IllegalStateException("Unknown object of type "+o.getClass()+ " in ProtectedDataType");
// }
// }
}

class ContentList implements List<Object>, Serializable {
Expand Down Expand Up @@ -435,6 +443,7 @@ public void clear() {
@Override
public Object get(int index) {
if (index == 0) {
// what if encryptedDataType is null and clearValue is set? [pm]
return toJaxbElement(encryptedDataType);
} else {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,7 @@ protected void assertSteadyResources() {
assertResourceSchemaFetchIncrement(0);
assertResourceSchemaParseCountIncrement(0);
assertConnectorCapabilitiesFetchIncrement(0);
// TODO reenable this test!
// assertConnectorInitializationCountIncrement(0);
assertConnectorInitializationCountIncrement(0);
assertConnectorSchemaParseIncrement(0);
}

Expand Down

0 comments on commit df1dc0b

Please sign in to comment.