Skip to content

Commit

Permalink
some repo fixes..improved bean converter, some schema improvements, e…
Browse files Browse the repository at this point in the history
…tc..
  • Loading branch information
katkav committed Feb 26, 2014
1 parent 1a20eeb commit 5978a7b
Show file tree
Hide file tree
Showing 57 changed files with 1,805 additions and 984 deletions.
Expand Up @@ -87,18 +87,22 @@ public PrismContainerValue(OriginType type, Objectable source, PrismContainerabl
/**
* Returns a set of items that the property container contains. The items may be properties or inner property containers.
* <p/>
* The set must not be null. In case there are no properties an empty set is
* The set may be null. In case there are no properties an empty set is
* returned.
* <p/>
* Returned set is mutable. Live object is returned.
*
* @return set of items that the property container contains.
*/

public List<Item<?>> getItems() {
return items;
}

public Item<?> getNextItem(Item<?> referenceItem) {
if (items == null){
return null;
}
Iterator<Item<?>> iterator = items.iterator();
while (iterator.hasNext()) {
Item<?> item = iterator.next();
Expand All @@ -114,6 +118,9 @@ public Item<?> getNextItem(Item<?> referenceItem) {
}

public Item<?> getPreviousItem(Item<?> referenceItem) {
if (items == null){
return null;
}
Item<?> lastItem = null;
Iterator<Item<?>> iterator = items.iterator();
while (iterator.hasNext()) {
Expand All @@ -139,6 +146,9 @@ public Item<?> getPreviousItem(Item<?> referenceItem) {
*/
public Set<PrismProperty<?>> getProperties() {
Set<PrismProperty<?>> properties = new HashSet<PrismProperty<?>>();
if (items == null){
return null;
}
for (Item<?> item : getItems()) {
if (item instanceof PrismProperty) {
properties.add((PrismProperty<?>) item);
Expand Down Expand Up @@ -323,7 +333,7 @@ public <V extends PrismValue> boolean substract(Item<V> item) throws SchemaExcep
*/
public void addReplaceExisting(Item<?> item) throws SchemaException {
Item<?> existingItem = findItem(item.getElementName(), Item.class);
if (existingItem != null) {
if (existingItem != null && items != null) {
items.remove(existingItem);
existingItem.setParent(null);
}
Expand All @@ -334,13 +344,16 @@ public void remove(Item<?> item) {
Validate.notNull(item, "Item must not be null.");

Item<?> existingItem = findItem(item.getElementName(), Item.class);
if (existingItem != null) {
if (existingItem != null && items != null) {
items.remove(existingItem);
existingItem.setParent(null);
}
}

public void removeAll() {
if (items == null){
return;
}
Iterator<Item<?>> iterator = items.iterator();
while (iterator.hasNext()) {
Item<?> item = iterator.next();
Expand Down Expand Up @@ -370,7 +383,7 @@ public void addAllReplaceExisting(Collection<? extends Item<?>> itemsToAdd) thro
// Check for conflicts, remove conflicting values
for (Item<?> item : itemsToAdd) {
Item<?> existingItem = findItem(item.getElementName(), Item.class);
if (existingItem != null) {
if (existingItem != null && items != null) {
items.remove(existingItem);
}
}
Expand Down Expand Up @@ -478,6 +491,9 @@ public PrismReference findReference(QName elementName) {
}

public PrismReference findReferenceByCompositeObjectElementName(QName elementName) {
if (items == null){
return null;
}
for (Item item: items) {
if (item instanceof PrismReference) {
PrismReference ref = (PrismReference)item;
Expand Down Expand Up @@ -751,7 +767,10 @@ public void removeContainer(ItemPath itemPath) {

// Expects that "self" path is NOT present in propPath
<I extends Item<?>> void removeItem(ItemPath propPath, Class<I> itemType) {
ItemPathSegment first = propPath.first();
if (items == null){
return;
}
ItemPathSegment first = propPath.first();
if (!(first instanceof NameItemPathSegment)) {
throw new IllegalArgumentException("Attempt to remove item using a non-name path "+propPath+" in "+this);
}
Expand Down Expand Up @@ -797,8 +816,10 @@ public void recompute(PrismContext prismContext) {
@Override
public void accept(Visitor visitor) {
super.accept(visitor);
for (Item<?> item: getItems()) {
item.accept(visitor);
if (items != null) {
for (Item<?> item : getItems()) {
item.accept(visitor);
}
}
}

Expand All @@ -818,20 +839,24 @@ public void accept(Visitor visitor, ItemPath path, boolean recursive) {
}
QName subName = ((NameItemPathSegment)first).getName();
ItemPath rest = path.rest();
for (Item<?> item : items) {
if (first.isWildcard() || subName.equals(item.getElementName())) {
item.accept(visitor, rest, recursive);
}
}
if (items != null) {
for (Item<?> item : items) {
if (first.isWildcard() || subName.equals(item.getElementName())) {
item.accept(visitor, rest, recursive);
}
}
}
}
}

public boolean hasCompleteDefinition() {
for (Item<?> item: getItems()) {
if (!item.hasCompleteDefinition()) {
return false;
}
}
if (items != null) {
for (Item<?> item : getItems()) {
if (!item.hasCompleteDefinition()) {
return false;
}
}
}
return true;
}

Expand Down Expand Up @@ -1161,14 +1186,16 @@ public PrismContainerValue<T> clone() {
protected void copyValues(PrismContainerValue<T> clone) {
super.copyValues(clone);
clone.id = this.id;
for (Item<?> item: this.items) {
Item<?> clonedItem = item.clone();
clonedItem.setParent(clone);
if (clone.items == null) {
clone.items = new ArrayList<>(this.items.size());
}
clone.items.add(clonedItem);
}
if (this.items != null) {
for (Item<?> item : this.items) {
Item<?> clonedItem = item.clone();
clonedItem.setParent(clone);
if (clone.items == null) {
clone.items = new ArrayList<>(this.items.size());
}
clone.items.add(clonedItem);
}
}
// TODO: deep clonning?
clone.rawXNode = this.rawXNode;
clone.rawElements = this.rawElements;
Expand Down
Expand Up @@ -17,7 +17,10 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -200,6 +203,12 @@ public <T extends Objectable> PrismObject<T> parseObject(Element objectElement)
* @throws IOException
*/
public <T extends Objectable> PrismObject<T> parseObject(File file) throws SchemaException, IOException {
Parser parser = findParser(file);
XNode xnode = parser.parse(file);
return xnodeProcessor.parseObject(xnode);
}

private Parser findParser(File file) throws IOException{
Parser parser = null;
for (Entry<String,Parser> entry: parserMap.entrySet()) {
Parser aParser = entry.getValue();
Expand All @@ -211,8 +220,7 @@ public <T extends Objectable> PrismObject<T> parseObject(File file) throws Schem
if (parser == null) {
throw new SystemException("No parser for file '"+file+"' (autodetect)");
}
XNode xnode = parser.parse(file);
return xnodeProcessor.parseObject(xnode);
return parser;
}
/**
* Parses a file and creates a prism from it.
Expand All @@ -228,19 +236,24 @@ public <T extends Objectable> PrismObject<T> parseObject(File file, String langu
* Used mostly for testing, but can also be used for built-in editors, etc.
*/
public <T extends Objectable> PrismObject<T> parseObject(String dataString) throws SchemaException {
Parser parser = findParser(dataString);
XNode xnode = parser.parse(dataString);
return xnodeProcessor.parseObject(xnode);
}

private Parser findParser(String data){
Parser parser = null;
for (Entry<String,Parser> entry: parserMap.entrySet()) {
Parser aParser = entry.getValue();
if (aParser.canParse(dataString)) {
if (aParser.canParse(data)) {
parser = aParser;
break;
}
}
if (parser == null) {
throw new SystemException("No parser for data '"+DebugUtil.excerpt(dataString,16)+"' (autodetect)");
throw new SystemException("No parser for data '"+DebugUtil.excerpt(data,16)+"' (autodetect)");
}
XNode xnode = parser.parse(dataString);
return xnodeProcessor.parseObject(xnode);
return parser;
}

/**
Expand Down Expand Up @@ -312,8 +325,17 @@ public <C extends Containerable, O extends Objectable> void adopt(PrismContainer
getSchemaRegistry().applyDefinition(prismContainerValue, typeName, path, false);
}

public List<PrismObject<? extends Objectable>> parseObjects(File file) {
throw new UnsupportedOperationException();
public List<PrismObject<? extends Objectable>> parseObjects(File file) throws SchemaException, IOException {
Parser parser = findParser(file);
Collection<XNode> nodes = parser.parseCollection(file);
Iterator<XNode> nodesIterator = nodes.iterator();
List<PrismObject<? extends Objectable>> objects = new ArrayList<PrismObject<? extends Objectable>>();
while (nodesIterator.hasNext()){
XNode node = nodesIterator.next();
PrismObject object = xnodeProcessor.parseObject(node);
objects.add(object);
}
return objects;
}

public <O extends Objectable> String serializeObjectToString(PrismObject<O> object, String language) throws SchemaException {
Expand Down
Expand Up @@ -22,6 +22,7 @@
import com.evolveum.midpoint.prism.schema.SchemaRegistry;
import com.evolveum.midpoint.prism.util.CloneUtil;
import com.evolveum.midpoint.prism.util.PrismUtil;
import com.evolveum.midpoint.prism.xnode.MapXNode;
import com.evolveum.midpoint.prism.xnode.PrimitiveXNode;
import com.evolveum.midpoint.prism.xnode.XNode;
import com.evolveum.midpoint.util.DOMUtil;
Expand Down
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Map.Entry;
Expand Down Expand Up @@ -54,6 +55,16 @@ public abstract class AbstractParser implements Parser {
protected abstract JsonParser createParser(File file) throws SchemaException, IOException;
public abstract JsonGenerator createGenerator(StringWriter out) throws SchemaException;

@Override
public Collection<XNode> parseCollection(File file) throws SchemaException, IOException {
throw new UnsupportedOperationException("Parse objects not supported for json and yaml.");
}

@Override
public Collection<XNode> parseCollection(String dataString) throws SchemaException {
throw new UnsupportedOperationException("Parse objects not supported for json and yaml.");
}

@Override
public XNode parse(File file) throws SchemaException, IOException {
JsonParser parser = createParser(file);
Expand Down
Expand Up @@ -18,6 +18,7 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -28,6 +29,7 @@
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.evolveum.midpoint.prism.PrismConstants;
import com.evolveum.midpoint.prism.PrismContainerValue;
Expand All @@ -44,6 +46,7 @@
import com.evolveum.midpoint.prism.xnode.XNode;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.SchemaException;

public class DomParser implements Parser {
Expand All @@ -56,7 +59,27 @@ public DomParser(SchemaRegistry schemaRegistry) {
super();
this.schemaRegistry = schemaRegistry;
}

@Override
public Collection<XNode> parseCollection(File file) throws SchemaException, IOException {
Document document = DOMUtil.parseFile(file);
Element root = DOMUtil.getFirstChildElement(document);
// TODO: maybe some check if this is a collection of other objects???
List<Element> children = DOMUtil.listChildElements(root);
Collection<XNode> nodes = new ArrayList<XNode>();
for (Element child : children){
RootXNode xroot = parse(child);
nodes.add(xroot);
}
return nodes;
}

@Override
public Collection<XNode> parseCollection(String dataString) throws SchemaException {
// TODO Auto-generated method stub
return null;
}

@Override
public XNode parse(File file) throws SchemaException {
Document document = DOMUtil.parseFile(file);
Expand All @@ -71,6 +94,11 @@ public XNode parse(String dataString) throws SchemaException {

public RootXNode parse(Document document) throws SchemaException {
Element rootElement = DOMUtil.getFirstChildElement(document);
RootXNode xroot = parse(rootElement);
return xroot;
}

private RootXNode parse(Element rootElement) throws SchemaException{
RootXNode xroot = new RootXNode(DOMUtil.getQName(rootElement));
extractCommonMetadata(rootElement, xroot);
XNode xnode = parseElementContent(rootElement);
Expand Down Expand Up @@ -355,4 +383,6 @@ public Element serializeToElement(RootXNode xroot) throws SchemaException {
DomSerializer serializer = new DomSerializer(this, schemaRegistry);
return serializer.serialize(xroot);
}


}
Expand Up @@ -140,6 +140,9 @@ private void serializeSubnode(XNode xsubnode, Element parentElement, QName eleme
}
if (xsubnode instanceof MapXNode) {
Element element = createElement(elementName);
if (xsubnode.isExplicitTypeDeclaration() && xsubnode.getTypeQName() != null){
DOMUtil.setXsiType(element, xsubnode.getTypeQName());
}
parentElement.appendChild(element);
serializeMap((MapXNode)xsubnode, element);
} else if (xsubnode instanceof PrimitiveXNode<?>) {
Expand Down
Expand Up @@ -17,6 +17,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Collection;

import javax.xml.namespace.QName;

Expand All @@ -34,6 +35,10 @@ public interface Parser {

XNode parse(String dataString) throws SchemaException;

Collection<XNode> parseCollection(File file) throws SchemaException, IOException;

Collection<XNode> parseCollection(String dataString) throws SchemaException;

boolean canParse(File file) throws IOException;

boolean canParse(String dataString);
Expand Down

0 comments on commit 5978a7b

Please sign in to comment.