Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dmitry konturov container #34

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions OWNER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Контуров Дмитрий Алексеевич
69 changes: 63 additions & 6 deletions src/main/java/arhangel/dim/container/BeanGraph.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package arhangel.dim.container;

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

/**
*
Expand All @@ -13,38 +15,93 @@ public class BeanGraph {

/**
* Добавить вершину в граф
*
* @param value - объект, привязанный к вершине
*/
public BeanVertex addVertex(Bean value) {
return null;
BeanVertex newVertex = new BeanVertex(value);
vertices.put(newVertex, new LinkedList<>());
return newVertex;
}

/**
* Соединить вершины ребром
*
* @param from из какой вершины
* @param to в какую вершину
* @param to в какую вершину
*/
public void addEdge(BeanVertex from ,BeanVertex to) {
public void addEdge(BeanVertex from, BeanVertex to) {
vertices.get(from).add(to);
}

/**
* Проверяем, связаны ли вершины
*/
public boolean isConnected(BeanVertex v1, BeanVertex v2) {
return false;
return vertices.get(v1).contains(v2);
}

/**
* Получить список вершин, с которыми связана vertex
*/
public List<BeanVertex> getLinked(BeanVertex vertex) {
return null;
return new ArrayList<>(vertices.get(vertex));
Copy link
Owner

Choose a reason for hiding this comment

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

get() уже вернет List

}

/**
* Количество вершин в графе
*/
public int size() {
return 0;
return vertices.size();
}

public List<BeanVertex> sort() throws CycleReferenceException {
return new GraphSorter().sort();
}

private enum VertexColor {
WHITE, // not visited
GREY, // entered, but not finished
BLACK // finished
}

private class GraphSorter {

Map<BeanVertex, VertexColor> vertexColor = new HashMap<>();
List<BeanVertex> sortedVertices = new LinkedList<>();

private void init() {
vertexColor.clear();
sortedVertices.clear();
for (BeanVertex vertex : vertices.keySet()) {
vertexColor.put(vertex, VertexColor.WHITE);
}
}

private List<BeanVertex> sort() throws CycleReferenceException {
init();
for (BeanVertex vertex : vertices.keySet()) {
if (vertexColor.get(vertex) == VertexColor.WHITE) {
dfs(vertex);
}
}
return new LinkedList<>(sortedVertices);
}

private void dfs(BeanVertex vertex) throws CycleReferenceException {
vertexColor.put(vertex, VertexColor.GREY);
for (BeanVertex childVertex : getLinked(vertex)) {
if (vertexColor.get(childVertex) == VertexColor.GREY) {
throw new CycleReferenceException("Graph has cycles");
} else if (vertexColor.get(childVertex) == VertexColor.WHITE) {
dfs(childVertex);
}
}
vertexColor.put(vertex, VertexColor.BLACK);
sortedVertices.add(vertex);
}


}

}
98 changes: 93 additions & 5 deletions src/main/java/arhangel/dim/container/BeanXmlReader.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
package arhangel.dim.container;


//import org.w3c.dom.*;


import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import java.io.File;

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

/**
*
*/
public class BeanXmlReader {
private static final String TAG_BEAN = "bean";
private static final String TAG_PROPERTY = "property";
Expand All @@ -14,8 +28,82 @@ public class BeanXmlReader {
private static final String ATTR_BEAN_ID = "id";
private static final String ATTR_BEAN_CLASS = "class";

public List<Bean> parseBeans(String pathToFile) {
return null;
public List<Bean> parseBeans(String pathToFile) throws InvalidConfigurationException {

List<Bean> returnList = new ArrayList<>();
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
File inputFile = new File(pathToFile);
Document document = docBuilder.parse(inputFile);
document.getDocumentElement().normalize();
NodeList beanList = document.getDocumentElement().getChildNodes();
for (int i = 0; i < beanList.getLength(); ++i) {
Node beanNode = beanList.item(i);
if (!beanNode.getNodeName().equals(TAG_BEAN)) { // not a bean
continue;
}
if (beanNode.getNodeType() == Node.ELEMENT_NODE) {
NamedNodeMap attributes = beanNode.getAttributes();
String beanName = attributes.getNamedItem(ATTR_BEAN_ID).getNodeValue();
String beanClass = attributes.getNamedItem(ATTR_BEAN_CLASS).getNodeValue();
Map<String, Property> beanProperties = parseBeanProperties(beanNode);

Bean bean = new Bean(beanName, beanClass, beanProperties);
returnList.add(bean);
} else {
throw new Exception("Bean is not ELEMENT_NODE");
}
}
} catch (Exception e) {
throw new InvalidConfigurationException(e);
}
return returnList;
}

private Map<String, Property> parseBeanProperties(Node beanNode) throws Exception {
Map<String, Property> propertyMap = new HashMap<>();

if (beanNode.getNodeType() == Node.ELEMENT_NODE) {
NodeList propertyList = beanNode.getChildNodes();
for (int i = 0; i < propertyList.getLength(); ++i) {
Node propertyNode = propertyList.item(i);
if (!propertyNode.getNodeName().equals(TAG_PROPERTY)) { // not a property
continue;
}
if (propertyNode.hasChildNodes()) {
throw new Exception("Property has children");
}
if (propertyNode.getNodeType() == Node.ELEMENT_NODE) {
NamedNodeMap attributes = propertyNode.getAttributes();
String propertyName = attributes.getNamedItem(ATTR_NAME).getNodeValue();
ValueType propertyValueType = ValueType.REF;

Node propertyValueNode = attributes.getNamedItem(ATTR_REF);

if (propertyValueNode == null) {
propertyValueNode = attributes.getNamedItem(ATTR_VALUE);
propertyValueType = ValueType.VAL;
}

if (propertyValueNode == null) {
throw new Exception("No value in property tag");
}

String propertyValue = propertyValueNode.getNodeValue();

Property property = new Property(propertyName, propertyValue, propertyValueType);

propertyMap.put(propertyName, property);
} else {
throw new Exception("Property is not ELEMENT_NODE");
}

}
} else {
throw new Exception("Bean is not ELEMENT_NODE");
}
return propertyMap;
}

}
Loading