/** * Copyright 2011 Oliver Buchtala * * This file is part of ndogen. * * ndogen is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ndogen 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ndogen. If not, see <http://www.gnu.org/licenses/>. */ package org.ndogen.substance; import java.security.NoSuchAlgorithmException; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.Map; import java.util.Queue; import org.ndogen.watch.FileUtils; public class Creator { private String md5; private final String user; private int count; private final Document doc; public Creator(String user, Document doc) { this.user = user; this.doc = doc; String name = doc.getName(); try { md5 = FileUtils.getMD5(user + "@@" + name); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } public Map<String, SubstanceNode> create() { count = 0; Map<String, SubstanceNode> all = new LinkedHashMap<String, SubstanceNode>(); Queue<SubstanceNode> q = new LinkedList<SubstanceNode>(); q.add(doc); while(!q.isEmpty()) { SubstanceNode node = q.poll(); prepareNode(node); all.put(node.get_id(), node); if (node instanceof NestedNode) { NestedNode nested = (NestedNode) node; for (SubstanceNode child : nested) { q.add(child); } } } for (SubstanceNode substanceNode : all.values()) { if (substanceNode instanceof NestedNode) { NestedNode parent = (NestedNode) substanceNode; LinkedList<String> childrenIds = new LinkedList<String>(); for (SubstanceNode child : parent) { childrenIds.add(child.get_id()); } parent.setChildren(childrenIds); } } return all; } private void prepareNode(SubstanceNode node) { node.set_id(getId(node)); if (node instanceof Section) { Section sec = (Section) node; sec.setDocument(doc.get_id()); } else if (node instanceof Text) { Text t = (Text) node; t.setDocument(doc.get_id()); } } public String getId(SubstanceNode node) { String id; if (node instanceof Document) { id = "/document"; } else if (node instanceof Section) { id = "/section"; } else if (node instanceof Code) { id = "/code"; } else if (node instanceof Quote) { id = "/quote"; } else if (node instanceof Text) { id = "/text"; } else { id = "/other"; } id += "/"+user+"/"+md5+"_"+count++; return id; } }