Skip to content

Commit

Permalink
issue705: create CsmElement hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Feb 15, 2017
1 parent 55e5dd9 commit e007d46
Show file tree
Hide file tree
Showing 13 changed files with 538 additions and 364 deletions.

Large diffs are not rendered by default.

Expand Up @@ -33,12 +33,12 @@ public class SourcePrinter {
this.indentation = indentation;
}

SourcePrinter indent() {
public SourcePrinter indent() {
level++;
return this;
}

SourcePrinter unindent() {
public SourcePrinter unindent() {
level--;
return this;
}
Expand Down
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2016 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser 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.
*/

package com.github.javaparser.printer.concretesyntaxmodel;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.printer.ConcreteSyntaxModel;
import com.github.javaparser.printer.SourcePrinter;

public class CsmAttribute implements CsmElement {
private ObservableProperty property;

public CsmAttribute(ObservableProperty property) {
this.property = property;
}

@Override
public void prettyPrint(Node node, SourcePrinter printer) {
Object value = property.singleValueFor(node);
if (value instanceof Enum) {
printer.print(((Enum) value).name().toLowerCase());
} else {
if (value != null) {
printer.print(value.toString());
}
}
}
}
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2016 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser 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.
*/

package com.github.javaparser.printer.concretesyntaxmodel;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.printer.ConcreteSyntaxModel;
import com.github.javaparser.printer.SourcePrinter;


class CsmChild implements CsmElement {
private ObservableProperty property;

public CsmChild(ObservableProperty property) {
this.property = property;
}

@Override
public void prettyPrint(Node node, SourcePrinter printer) {
Node child = property.singlePropertyFor(node);
if (child != null) {
ConcreteSyntaxModel.genericPrettyPrint(child, printer);
}
}
}
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2016 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser 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.
*/

package com.github.javaparser.printer.concretesyntaxmodel;

import com.github.javaparser.ast.Node;
import com.github.javaparser.printer.ConcreteSyntaxModel;
import com.github.javaparser.printer.SourcePrinter;

public class CsmComment implements CsmElement{
@Override
public void prettyPrint(Node node, SourcePrinter printer) {
if (node.hasComment()) {
ConcreteSyntaxModel.genericPrettyPrint(node.getComment(), printer);
}
}
}
@@ -0,0 +1,75 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2016 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser 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.
*/

package com.github.javaparser.printer.concretesyntaxmodel;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.printer.ConcreteSyntaxModel;
import com.github.javaparser.printer.SourcePrinter;

import java.util.function.Predicate;

public class CsmConditional implements CsmElement {
Predicate<Node> predicateCondition;
private ObservableProperty condition;
private CsmElement thenElement;
private CsmElement elseElement;

public CsmConditional(Predicate<Node> condition, CsmElement thenElement, CsmElement elseElement) {
this.predicateCondition = condition;
this.thenElement = thenElement;
this.elseElement = elseElement;
}

public CsmConditional(ObservableProperty condition, CsmElement thenElement, CsmElement elseElement) {
this.condition = condition;
this.thenElement = thenElement;
this.elseElement = elseElement;
}

public CsmConditional(ObservableProperty condition, CsmElement thenElement) {
this.condition = condition;
this.thenElement = thenElement;
this.elseElement = null;
}

@Override
public void prettyPrint(Node node, SourcePrinter printer) {
boolean test;
if (condition != null) {
if (condition.isSingle()) {
test = condition.singlePropertyFor(node) != null;
} else {
test = condition.listValueFor(node) != null && !condition.listValueFor(node).isEmpty();
}
} else {
test = predicateCondition.test(node);
}
if (test) {
thenElement.prettyPrint(node, printer);
} else {
if (elseElement != null) {
elseElement.prettyPrint(node, printer);
}
}
}
}
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2016 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser 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.
*/

package com.github.javaparser.printer.concretesyntaxmodel;

import com.github.javaparser.ast.Node;
import com.github.javaparser.printer.SourcePrinter;


public interface CsmElement {

void prettyPrint(Node node, SourcePrinter printer);
}
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2016 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser 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.
*/

package com.github.javaparser.printer.concretesyntaxmodel;

import com.github.javaparser.ast.Node;
import com.github.javaparser.printer.SourcePrinter;

public class CsmIndent implements CsmElement {

@Override
public void prettyPrint(Node node, SourcePrinter printer) {
printer.indent();
}
}
@@ -0,0 +1,94 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2016 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser 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.
*/

package com.github.javaparser.printer.concretesyntaxmodel;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.printer.ConcreteSyntaxModel;
import com.github.javaparser.printer.SourcePrinter;

import java.util.Collection;
import java.util.Iterator;

public class CsmList implements CsmElement {
private ObservableProperty property;
private CsmElement separator;
private CsmElement preceeding;
private CsmElement following;

public CsmList(ObservableProperty property, CsmElement separator) {
this.property = property;
this.separator = separator;
}

public CsmList(ObservableProperty property) {
this.property = property;
this.separator = null;
}

public CsmList(ObservableProperty property, CsmElement separator, CsmElement preceeding, CsmElement following) {
this.property = property;
this.separator = separator;
this.preceeding = preceeding;
this.following = following;
}

@Override
public void prettyPrint(Node node, SourcePrinter printer) {
if (property.isAboutNodes()) {
NodeList nodeList = property.listValueFor(node);
if (nodeList == null) {
return;
}
if (!nodeList.isEmpty() && preceeding != null) {
preceeding.prettyPrint(node, printer);
}
for (int i = 0; i < nodeList.size(); i++) {
ConcreteSyntaxModel.genericPrettyPrint(nodeList.get(i), printer);
if (separator != null && i != (nodeList.size() - 1)) {
separator.prettyPrint(node, printer);
}
}
if (!nodeList.isEmpty() && following != null) {
following.prettyPrint(node, printer);
}
} else {
Collection<?> values = property.listPropertyFor(node);
if (values == null) {
return;
}
if (!values.isEmpty() && preceeding != null) {
preceeding.prettyPrint(node, printer);
}
for (Iterator it = values.iterator(); it.hasNext(); ) {
printer.print(it.next().toString());
if (separator != null && it.hasNext()) {
separator.prettyPrint(node, printer);
}
}
if (!values.isEmpty() && following != null) {
following.prettyPrint(node, printer);
}
}
}
}

0 comments on commit e007d46

Please sign in to comment.