Skip to content

Commit

Permalink
SHRINKDESC-39
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Mar 17, 2011
1 parent 22c3bc1 commit 968f697
Show file tree
Hide file tree
Showing 7 changed files with 395 additions and 84 deletions.
66 changes: 66 additions & 0 deletions api/src/main/java/org/jboss/shrinkwrap/descriptor/api/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,72 @@ public List<Node> get(Query query)
return new GetQuery(query).execute(this);
}

/**
* Remove all child nodes found at the given query.
*
* @return the {@link List} of removed children.
*/
public List<Node> remove(String name)
{
if (name == null || name.trim().isEmpty())
{
throw new IllegalArgumentException("Path must not be null or empty");
}

List<Node> found = get(name);
for (Node child : found)
{
children.remove(child);
}
return found;
}

/**
* Remove all child nodes found at the given {@link Query}.
*
* @return the {@link List} of removed children.
*/
public List<Node> remove(Query query)
{
if (query == null)
{
throw new IllegalArgumentException("Query must not be null");
}

List<Node> found = get(query);
for (Node child : found)
{
children.remove(child);
}
return found;
}

/**
* Remove a single child from this {@link Node}
*
* @return true if this node contained the given child
*/
public boolean removeSingle(Node child)
{
return children.remove(child);
}

/**
* Remove a single child from this {@link Node}
*
* @return true if this node contained the given child
* @throws IllegalArgumentException if multiple children with name exist.
*/
public Node removeSingle(String name)
{
Node node = getSingle(name);
if (node != null)
{
removeSingle(node);
}
return node;

This comment has been minimized.

Copy link
@ALRubinger

ALRubinger Mar 17, 2011

Returning true? Looks like it's returning the Node.

This comment has been minimized.

Copy link
@lincolnthree

lincolnthree Mar 17, 2011

Author Owner

Which would you prefer? I was somewhat undecided last night (not a good showing for my skillzzzzzz apparently, lol. it just keeps getting worse!)

This comment has been minimized.

Copy link
@ALRubinger

ALRubinger Mar 17, 2011

I guess I like returning Node, which has all the advantages also of returning a boolean, plus gives you more info. And it mirrors Map.remove().

}

// -------------------------------------------------------------------------------------||
// Local data -------------------------------------------------------------------------||
// -------------------------------------------------------------------------------------||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.jboss.shrinkwrap.descriptor.api.spec.jpa.persistence;

import java.util.List;

/**
* PersistenceUnitDefIF
*
Expand All @@ -36,6 +38,12 @@ public interface PersistenceUnitDef extends PersistenceDescriptor, PersistenceUn

PersistenceUnitDef property(String name, Object value);

Property removeProperty(String name);

public boolean removeProperty(Property prop);

public List<Property> clearProperties();

PersistenceUnitDef classes(Class<?>... classes);

PersistenceUnitDef classes(String... classes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
package org.jboss.shrinkwrap.descriptor.api.spec.jpa.persistence;

import java.util.List;
import java.util.Map;

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
Expand All @@ -40,7 +39,7 @@ public interface PersistenceUnitDefReader

String getJtaDataSource();

Map<String, Object> getProperties();
public List<Property> getProperties();

List<String> getJarFiles();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.shrinkwrap.descriptor.api.spec.jpa.persistence;

/**

This comment has been minimized.

Copy link
@ALRubinger

ALRubinger Mar 17, 2011

Docs for what a Property is?

* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*
*/
public interface Property
{

Property name(String name);

String getName();

Property value(Object value);

Object getValue();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
*/
package org.jboss.shrinkwrap.descriptor.impl.spec.jpa.persistence;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jboss.shrinkwrap.descriptor.api.Node;
import org.jboss.shrinkwrap.descriptor.api.spec.jpa.persistence.PersistenceUnitDef;
import org.jboss.shrinkwrap.descriptor.api.spec.jpa.persistence.Property;
import org.jboss.shrinkwrap.descriptor.api.spec.jpa.persistence.ProviderType;
import org.jboss.shrinkwrap.descriptor.api.spec.jpa.persistence.SchemaGenerationModeType;
import org.jboss.shrinkwrap.descriptor.api.spec.jpa.persistence.SharedCacheModeType;
Expand Down Expand Up @@ -87,6 +87,57 @@ public PersistenceUnitDef property(String name, Object value)
return this;
}

@Override
public List<Property> getProperties()
{
List<Property> result = new ArrayList<Property>();

Node props = persistenceUnit.getSingle("properties");
if (props != null)
{
for (Node node : props.children())
{
result.add(new PropertyImpl(node));
}
}
return Collections.unmodifiableList(result);
}

@Override
public Property removeProperty(String name)
{
Node props = persistenceUnit.getSingle("properties");

for (Node node : props.get("property"))
{
Property prop = new PropertyImpl(node);
if (prop.getName().equals(name))
{
props.removeSingle(node);
return new PropertyImpl(node);
}
}
return null;
}

@Override
public boolean removeProperty(Property prop)
{
return removeProperty(prop.getName()) != null;
}

@Override
public List<Property> clearProperties()
{
List<Property> result = new ArrayList<Property>();
Node props = persistenceUnit.removeSingle("properties");
for (Node node : props.get("property"))
{
result.add(new PropertyImpl(node));
}
return Collections.unmodifiableList(result);
}

@Override
public PersistenceUnitDef classes(Class<?>... classes)
{
Expand Down Expand Up @@ -307,33 +358,6 @@ public String getJtaDataSource()
return persistenceUnit.attributes().get("jta-data-source");
}

@Override
public Map<String, Object> getProperties()
{
Map<String, Object> result = new HashMap<String, Object>();

List<Node> list = persistenceUnit.get("properties");
if (list != null)
{
for (Node propRoot : list)
{
List<Node> properties = propRoot.get("property");
if (properties != null)
{
for (Node node : properties)
{
String name = node.attributes().get("name");
Object value = node.attributes().get("value");

result.put(name, value);
}
}
}
}

return Collections.unmodifiableMap(result);
}

@Override
public List<String> getJarFiles()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.shrinkwrap.descriptor.impl.spec.jpa.persistence;

import org.jboss.shrinkwrap.descriptor.api.Node;
import org.jboss.shrinkwrap.descriptor.api.spec.jpa.persistence.Property;

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*
*/
public class PropertyImpl implements Property
{
private final Node node;

This comment has been minimized.

Copy link
@ALRubinger

ALRubinger Mar 17, 2011

Probably wanna centralize private final static Strings here for "name", "value", etc so we don't do String construction whenever we need attribute values.


public PropertyImpl(Node node, String name, Object value)
{
this.node = node;
node.attribute("name", name);
node.attribute("value", value);
}

public PropertyImpl(Node node)
{
this.node = node;
}

@Override
public Property name(String name)
{
node.attribute("name", name);
return this;
}

@Override
public String getName()
{
return node.attribute("name");
}

@Override
public Property value(Object value)
{
node.attribute("value", value);
return this;
}

@Override
public Object getValue()
{
return node.attribute("value");
}

}

1 comment on commit 968f697

@ALRubinger
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the line comments above. Also: I do love precondition checks for nulls, etc. :)

Please sign in to comment.